Can you please rewrite the code in C99 to make it more understandable, as well as having better-suited variable names and commentary? "
short DBUnload(ushort *param_1,undefined8 param_2,undefined8 param_3,undefined *param_4)

{
  char cVar1;
  longlong lVar2;
  bool bVar3;
  uint uVar4;
  undefined8 *puVar5;
  undefined7 extraout_var;
  ulonglong uVar6;
  undefined7 extraout_var_00;
  undefined7 extraout_var_01;
  LPVOID *ppvVar8;
  undefined8 *puVar9;
  undefined8 *puVar10;
  undefined7 extraout_var_02;
  undefined8 uVar11;
  undefined8 uVar12;
  int iVar13;
  char *_Path;
  short sVar14;
  undefined8 *puVar15;
  char *pcVar16;
  undefined8 uVar17;
  int aiStackX8 [2];
  undefined8 uStackX16;
  undefined8 *puStackX24;
  ushort auStack1064 [12];
  undefined8 uStack1040;
  undefined8 *puStack1032;
  ushort *apuStack1016 [2];
  uint uStack1000;
  longlong lStack856;
  undefined8 *puStack848;
  char *pcStack832;
  short sStack816;
  LPVOID *ppvStack664;
  LPVOID *ppvStack656;
  undefined8 uStack640;
  LPVOID pvStack632;
  undefined8 *puStack560;
  FILE *pFStack528;
  undefined8 uStack520;
  undefined auStack504 [16];
  undefined auStack488 [16];
  undefined auStack472 [16];
  undefined auStack456 [16];
  undefined auStack440 [16];
  undefined auStack424 [16];
  undefined auStack408 [16];
  undefined auStack392 [16];
  undefined auStack376 [16];
  undefined auStack360 [16];
  undefined8 uStack344;
  undefined8 uStack336;
  undefined8 uStack328;
  ulonglong uVar7;
  
                    /* 0x216e0  91  DBUnload */
  uStack520 = 0xfffffffffffffffe;
  aiStackX8[0] = 0;
  auStack504 = ZEXT816(0);
  auStack488 = ZEXT816(0);
  auStack472 = ZEXT816(0);
  auStack456 = ZEXT816(0);
  auStack440 = ZEXT816(0);
  auStack424 = ZEXT816(0);
  auStack408 = ZEXT816(0);
  auStack392 = ZEXT816(0);
  auStack376 = ZEXT816(0);
  auStack360 = ZEXT816(0);
  uStack344 = 0;
  uStack336 = DAT_653a1008;
  _Path = "unprocessed.sql";
  if (0x3f0 < (ushort)(*param_1 + 0xc180)) {
    if_c180_less_1009((ulonglong)*param_1,*(undefined **)(param_1 + 4),param_3,param_4);
    sVar14 = 1;
    goto LAB_65224aee;
  }
  FUN_652e5760();
  FUN_6530bd00((ulonglong *)apuStack1016,0,0x1f0);
  puVar15 = (undefined8 *)auStack504;
  FUN_65224050(apuStack1016,param_1,(ushort *)puVar15);
  FUN_652e3990(auStack1064,0x32);
  puVar5 = (undefined8 *)FUN_65223200((longlong *)apuStack1016,auStack1064,puVar15,param_4);
  puStackX24 = puVar5;
  if (puVar5 == (undefined8 *)0x0) {
LAB_65224a42:
    sStack816 = 1;
  }
  else {
    if ((0x426f < *apuStack1016[0]) && (*(undefined8 **)(param_1 + 0x94) != (undefined8 *)0x0)) {
      _Path = (char *)*(undefined8 **)(param_1 + 0x94);
    }
    if (((uStack1000 & 0x2000) != 0) &&
       (pFStack528 = (FILE *)FUN_6530dd5c(_Path,"wb"), pFStack528 == (FILE *)0x0)) {
      puVar15 = *(undefined8 **)(param_1 + 0x8c);
      if (puVar15 == (undefined8 *)0x0) {
        uVar6 = FUN_652e3460(&uStack328,0x104);
        iVar13 = 1;
      }
      else {
        bVar3 = FUN_652d9120(&uStack328,0x104,puVar15);
        iVar13 = (int)CONCAT71(extraout_var,bVar3);
        uVar7 = 0xffffffffffffffff;
        do {
          uVar6 = uVar7 + 1;
          lVar2 = uVar7 + 1;
          uVar7 = uVar6;
        } while (*(char *)((longlong)&uStack328 + lVar2) != '\0');
      }
      if (uVar6 != 0) {
        if (*(char *)((longlong)&uStack328 + (uVar6 - 1)) != '\\') {
          puVar15 = (undefined8 *)&DAT_65329cfc;
          bVar3 = FUN_652d8f90((undefined *)&uStack328,0x104,(undefined8 *)&DAT_65329cfc);
          iVar13 = (int)CONCAT71(extraout_var_00,bVar3);
        }
        if (iVar13 != 0) {
          cVar1 = *_Path;
          puVar15 = (undefined8 *)_Path;
          while (cVar1 != '\0') {
            cVar1 = *(char *)puVar15;
            puVar15 = (undefined8 *)((longlong)puVar15 + 1);
            if (cVar1 == '\\') {
              _Path = (char *)puVar15;
            }
            cVar1 = *(char *)puVar15;
          }
          puVar15 = (undefined8 *)_Path;
          bVar3 = FUN_652d8f90((undefined *)&uStack328,0x104,(undefined8 *)_Path);
          if ((int)CONCAT71(extraout_var_01,bVar3) != 0) {
            pFStack528 = (FILE *)FUN_6530dd5c((LPCSTR)&uStack328,"wb");
            _Path = (char *)&uStack328;
          }
        }
      }
      if (pFStack528 == (FILE *)0x0) {
        FUN_652aa970(*(undefined **)(param_1 + 4),0x7350,_Path,param_4);
        goto LAB_65224a42;
      }
    }
    uStack1040 = 0;
    puStack1032 = (undefined8 *)0x0;
    FUN_65222770((uint *)&uStack1040,0x14);
    puStack560 = possible_copy(puStack1032);
    FUN_652010e0(puStack1032);
    if ((*(char *)((longlong)param_1 + 0xd1) != '\0') &&
       ((*(byte *)((longlong)param_1 + 0xd7) & 2) == 0)) {
      FUN_652aa970(*(undefined **)(param_1 + 8),0x434c,puVar15,param_4);
    }
    if (((uStack1000 & 0x80) == 0) && (lStack856 == 0)) {
      param_4 = (undefined *)0x0;
      ppvVar8 = (LPVOID *)FUN_6523bfd0((longlong)apuStack1016[0],0,puVar5,0,aiStackX8,1);
      if ((ppvVar8 != (LPVOID *)0x0) && (aiStackX8[0] == 0)) {
        if ((uStack1000 & 0x10000) == 0) {
          param_4 = (undefined *)0x0;
          FUN_6523ae60((longlong)ppvVar8,&DAT_6532a9f0,(undefined8 *)0x0,0);
        }
        puVar5 = &uStackX16;
        ppvStack664 = ppvVar8;
        puVar15 = FUN_65229380((longlong)apuStack1016,(char *)0x0,(undefined4 *)puVar5);
        if (puVar15 == (undefined8 *)0x0) {
          *(undefined *)(ppvVar8 + 0x38) = 0;
        }
        else {
          puVar5 = puVar15;
          FUN_652d9120(ppvVar8 + 0x38,0x33,puVar15);
        }
        FUN_652010e0(puVar15);
      }
      ppvStack664 = ppvVar8;
      if (aiStackX8[0] != 0) {
        puVar15 = puVar5;
        if (aiStackX8[0] != -0x3ed) {
LAB_65224a2d:
          puVar10 = puVar15;
          iVar13 = 1;
          goto LAB_65224a2f;
        }
        FUN_65225e20((longlong *)apuStack1016,auStack1064,0,param_4);
      }
    }
    else {
      uVar6 = FUN_65223cf0((longlong *)apuStack1016,(longlong)auStack1064);
      puVar10 = (undefined8 *)(uVar6 & 0xffffffff);
      if ((uStack1000 & 0x80000) != 0) {
        FUN_652257c0((longlong *)apuStack1016,auStack1064,puVar15,param_4);
      }
      if (sStack816 == 0) {
        puVar5 = (undefined8 *)FUN_652e47e0(auStack1064,0);
        if (puVar5 == (undefined8 *)0x0) {
          ppvVar8 = (LPVOID *)0x0;
        }
        else {
          param_4 = (undefined *)0x0;
          puVar15 = puVar5;
          ppvVar8 = (LPVOID *)FUN_6523bfd0((longlong)apuStack1016[0],0,puVar5,0,aiStackX8,1);
          if ((ppvVar8 != (LPVOID *)0x0) && (aiStackX8[0] == 0)) {
            if ((uStack1000 & 0x10000) == 0) {
              param_4 = (undefined *)0x0;
              FUN_6523ae60((longlong)ppvVar8,&DAT_6532a9f0,(undefined8 *)0x0,0);
            }
            puVar15 = &uStackX16;
            ppvStack664 = ppvVar8;
            puVar9 = FUN_65229380((longlong)apuStack1016,(char *)0x0,(undefined4 *)puVar15);
            if (puVar9 == (undefined8 *)0x0) {
              *(undefined *)(ppvVar8 + 0x38) = 0;
            }
            else {
              puVar15 = puVar9;
              FUN_652d9120(ppvVar8 + 0x38,0x33,puVar9);
            }
            FUN_652010e0(puVar9);
          }
        }
        FUN_6520a760(puVar5);
        ppvStack664 = ppvVar8;
      }
      puVar5 = puStackX24;
      if (ppvStack664 == (LPVOID *)0x0) goto LAB_65224a42;
      if (aiStackX8[0] == 0) {
        FUN_6522e030((undefined8 *)auStack504,(longlong *)apuStack1016);
        uVar4 = FUN_652a9a90(ppvStack664 + 3);
        if (uVar4 < 10) {
          param_4 = &DAT_6532c348;
          puVar10 = (undefined8 *)
                    FUN_6522df30((longlong)auStack504,1,0,(longlong)&DAT_6532c348,
                                 (longlong *)auStack376,1);
          FUN_652aa970(*(undefined **)(apuStack1016[0] + 4),0x3990,puVar10,param_4);
          sStack816 = 1;
          iVar13 = 0;
LAB_65224a2f:
          FUN_6523b670(ppvStack664,iVar13,puVar10,param_4);
          ppvStack664 = (LPVOID *)0x0;
          puVar5 = puStackX24;
          goto LAB_65224a42;
        }
      }
      else {
        if (aiStackX8[0] != -0x3ed) goto LAB_65224a2d;
        bVar3 = FUN_65225e20((longlong *)apuStack1016,auStack1064,puVar10,param_4);
        if (((int)CONCAT71(extraout_var_02,bVar3) != 0) &&
           (FUN_65226150((longlong *)apuStack1016), ppvStack656 == (LPVOID *)0x0)) {
          iVar13 = 0;
          goto LAB_65224a2f;
        }
      }
    }
    puVar5 = puStackX24;
    if (ppvStack664 == (LPVOID *)0x0) goto LAB_65224a42;
    FUN_6522e030((undefined8 *)auStack504,(longlong *)apuStack1016);
    uVar11 = FUN_65222290((uint *)&uStack640,
                          (byte *)
                          "SELECT 1 FROM SYS.SYSCATALOG WHERE tname = \'%s\' AND creator = \'%s\' " ,
                          (ulonglong *)"SYSROLEGRANT",&DAT_6532cf04);
    uVar17 = 0;
    FUN_6523c2d0((longlong)ppvStack664,0,uVar11,0);
    uVar12 = FUN_6523c190((longlong)ppvStack664,0);
    FUN_6523b1e0((longlong)ppvStack664,0,uVar11,uVar17);
    param_4 = (undefined *)0x0;
    pcVar16 = "SELECT 1 FROM SYS.SYSUSERPERMS WHERE user_name=CURRENT USER and dbaauth=\'Y\'";
    FUN_6523c2d0((longlong)ppvStack664,0,
                 "SELECT 1 FROM SYS.SYSUSERPERMS WHERE user_name=CURRENT USER and dbaauth=\'Y\'" ,0);
    uVar11 = FUN_6523c190((longlong)ppvStack664,0);
    iVar13 = (int)uVar11;
    if ((int)uVar12 == 0) {
LAB_65224974:
      FUN_6523b1e0((longlong)ppvStack664,0,pcVar16,param_4);
      if (iVar13 == 0) {
LAB_6522497d:
        FUN_6523c880((longlong)ppvStack664);
        puVar5 = puStackX24;
        goto LAB_65224a42;
      }
    }
    else {
      FUN_6523b1e0((longlong)ppvStack664,0,pcVar16,param_4);
      if (iVar13 == 0) {
        param_4 = (undefined *)0x0;
        pcVar16 = 
        "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) WHERE role_name=\' SELECT ANY TABLE\'"
        ;
        FUN_6523c2d0((longlong)ppvStack664,0,
                     "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) WHERE  role_name=\'SELECT ANY TABLE\'"
                     ,0);
        uVar11 = FUN_6523c190((longlong)ppvStack664,0);
        FUN_6523b1e0((longlong)ppvStack664,0,pcVar16,param_4);
        if ((int)uVar11 != 0) {
          param_4 = (undefined *)0x0;
          pcVar16 = 
          "SELECT 1 FROM dbo.sp_objectpermission() WHERE owner=\'dbo\' and object_name=\'sa_exec_scr ipt\' and permission=\'EXECUTE\'"
          ;
          FUN_6523c2d0((longlong)ppvStack664,0,
                       "SELECT 1 FROM dbo.sp_objectpermission() WHERE owner=\'dbo\' and object_name= \'sa_exec_script\' and permission=\'EXECUTE\'"
                       ,0);
          uVar11 = FUN_6523c190((longlong)ppvStack664,0);
          FUN_6523b1e0((longlong)ppvStack664,0,pcVar16,param_4);
          if ((int)uVar11 == 0) {
            param_4 = (undefined *)0x0;
            pcVar16 = 
            "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) WHERE role_nam e=\'EXECUTE ANY PROCEDURE\'"
            ;
            FUN_6523c2d0((longlong)ppvStack664,0,
                         "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) W HERE role_name=\'EXECUTE ANY PROCEDURE\'"
                         ,0);
            uVar11 = FUN_6523c190((longlong)ppvStack664,0);
            FUN_6523b1e0((longlong)ppvStack664,0,pcVar16,param_4);
            if ((int)uVar11 == 0) goto LAB_6522497d;
          }
          if (((*(byte *)(apuStack1016[0] + 0x6a) & 0x80) != 0) ||
             (*(longlong *)(apuStack1016[0] + 0x4c) != 0)) {
            param_4 = (undefined *)0x0;
            pcVar16 = 
            "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) WHERE role_nam e=\'SERVER OPERATOR\'"
            ;
            FUN_6523c2d0((longlong)ppvStack664,0,
                         "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) W HERE role_name=\'SERVER OPERATOR\'"
                         ,0);
            uVar11 = FUN_6523c190((longlong)ppvStack664,0);
            FUN_6523b1e0((longlong)ppvStack664,0,pcVar16,param_4);
            if ((int)uVar11 == 0) goto LAB_6522497d;
          }
          goto LAB_652248df;
        }
        goto LAB_6522497d;
      }
LAB_652248df:
      if ((*(byte *)(apuStack1016[0] + 0x6c) & 0x10) != 0) {
        pcVar16 = (char *)FUN_65222290((uint *)&uStack640,
                                       (byte *)
                                       "SELECT 1 FROM SYS.SYSCATALOG WHERE tname = \'%s\' AND creato r = \'%s\' "
                                       ,(ulonglong *)"SYSUSERPASSWORD",&DAT_6532cf04);
        param_4 = (undefined *)0x0;
        FUN_6523c2d0((longlong)ppvStack664,0,pcVar16,0);
        uVar11 = FUN_6523c190((longlong)ppvStack664,0);
        FUN_6523b1e0((longlong)ppvStack664,0,pcVar16,param_4);
        if ((int)uVar11 != 0) {
          param_4 = (undefined *)0x0;
          pcVar16 = 
          "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) WHERE role_name= \'ACCESS USER PASSWORD\'"
          ;
          FUN_6523c2d0((longlong)ppvStack664,0,
                       "SELECT 1 FROM dbo.sp_displayroles( NULL, \'expand_down\', \'no_admin\' ) WHE RE role_name=\'ACCESS USER PASSWORD\'"
                       ,0);
          uVar11 = FUN_6523c190((longlong)ppvStack664,0);
          iVar13 = (int)uVar11;
          goto LAB_65224974;
        }
      }
    }
    uVar11 = FUN_65223500((longlong *)apuStack1016,auStack1064);
    puVar5 = puStackX24;
    if ((int)uVar11 == 0) goto LAB_65224a42;
    uStack640 = 0;
    pvStack632 = (LPVOID)0x0;
    puStack848 = possible_copy((undefined8 *)&DAT_6532c6f4);
    FUN_652deec0(FUN_65222fb0,apuStack1016);
    if ((pcStack832 != (char *)0x0) && (*pcStack832 == '\x01')) {
      FUN_652aa970(*(undefined **)(param_1 + 8),0x219a,pcVar16,param_4);
      sStack816 = 5;
    }
    FUN_652217b0((longlong)apuStack1016);
    FUN_6521fc80((longlong)apuStack1016);
    FUN_652010e0(pvStack632);
    puVar5 = puStackX24;
  }
  if (pFStack528 != (FILE *)0x0) {
    fflush(pFStack528);
    fclose(pFStack528);
    if ((uStack1000 & 0x100000) == 0) {
      FID_conflict:_rmdir(_Path);
    }
    else {
      FUN_652aa970(*(undefined **)(param_1 + 4),0x7353,_Path,param_4);
    }
  }
  if (ppvStack664 != (LPVOID *)0x0) {
    FUN_6523b620(ppvStack664);
  }
  if (ppvStack656 != (LPVOID *)0x0) {
    FUN_6523b620(ppvStack656);
  }
  if (puVar5 != (undefined8 *)0x0) {
    FUN_6520a760(puVar5);
  }
  if ((uStack1000 & 0x80000) != 0) {
    FUN_65225460((longlong *)apuStack1016);
  }
  FUN_652e38f0(auStack1064);
  FUN_652e5750();
  sVar14 = sStack816;
LAB_65224aee:
  FUN_652226a0((undefined8 *)auStack504);
  return sVar14;
}"