The quest code is scattered between "quests_side.c", "blaze_dialog.c", "Toff Oremans_dialog.c", "Bernardo Gamboa_dialog.c" and "Tanneken Oremans_dialog.c".
Normally it should be pretty linear. But if you get killed on the ship, there's provision for you to return. That way, if you were resurrected to the tavern, you get to try again.
Normally it will be Bernardo Gamboa who challenges you. If you visited the governor first then Corentin Lucay, Gamboa's sidekick, turns against him and fights for you, so the only way you can be killed is by Gamboa. But if you did not visit the governor first then you'll be fighting both of them. If you now kill Bernardo Gamboa and Corentin Lucay kills you, and you are resurrected, you get to return, and now it will be Corentin Lucay who challenges you.
I don't know the code in detail but tracing through it, I can think of exactly one extremely unlikely scenario in which you could return to the ship and find nobody. You kill Bernardo Gamboa, Corentin Lucay kills you, then in the moments before resurrection starts, one of your officers kills Corentin Lucay; or perhaps you killed Corentin Lucay and the officer killed Bernardo Gamboa. In that case the trigger to return to the ship will have been reset as usual but with both Bernardo Gamboa and Corentin Lucay dead, there's nobody left to challenge you. But in that case you wouldn't have been alive to talk to Tanneken Oremans, return to the tavern and collect the sword.