#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int getDbVersion();
int /*<<< orphan*/ logMsg(int, char *, int, ...);
int strToInt(char *, int);
int upgradeTo(int /*<<< orphan*/);
int doUpgrade(FILE *file, int argc, char **argv) {
  int requestLevel;
  if (argc == 0) {
    requestLevel = -1;
  } else {
    requestLevel = strToInt(argv[0], -1);
  }
  int status;
  int currentLevel = getDbVersion();
  if (requestLevel == -1) {
    logMsg(2,
           "Please specify a valid database level for the upgrade, the "
           "database is currently at level %d.",
           currentLevel);
    status = 0;
  } else if (requestLevel < currentLevel) {
    logMsg(2,
           "Cannot upgrade database to level %d, the database is already at "
           "level %d.",
           requestLevel, currentLevel);
    status = 0;
  } else if (requestLevel == currentLevel) {
    logMsg(2, "Database is already at level %d, nothing to do.", requestLevel);
    status = 0;
  } else if (requestLevel > 7) {
    logMsg(2,
           "Cannot upgrade database to level %d, the maximum available upgrade "
           "level is %d.",
           requestLevel, 7);
    status = 0;
  } else {
    assert(currentLevel < requestLevel);
    int level = currentLevel + 1;
    while (level <= requestLevel) {
      status = upgradeTo(level++);
      if (status == 0) {
        break;
      }
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<doUpgrade>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1179 <doUpgrade+0x29>
movl   $0xffffffff,-0x1c(%rbp)
jmp    118d <doUpgrade+0x3d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    $0xffffffff,%esi
call   1030 <strToInt@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getDbVersion@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c3 <doUpgrade+0x73>
mov    -0x24(%rbp),%edx
mov    $0x2,%edi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1050 <logMsg@plt>
movl   $0x0,-0x20(%rbp)
jmp    12b6 <doUpgrade+0x166>
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11f4 <doUpgrade+0xa4>
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    $0x2,%edi
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1050 <logMsg@plt>
movl   $0x0,-0x20(%rbp)
jmp    12b1 <doUpgrade+0x161>
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    1222 <doUpgrade+0xd2>
mov    -0x1c(%rbp),%edx
mov    $0x2,%edi
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1050 <logMsg@plt>
movl   $0x0,-0x20(%rbp)
jmp    12ac <doUpgrade+0x15c>
cmpl   $0x7,-0x1c(%rbp)
jle    1253 <doUpgrade+0x103>
mov    -0x1c(%rbp),%edx
mov    $0x2,%edi
lea    0xe9d(%rip),%rsi
mov    $0x7,%ecx
mov    $0x0,%al
call   1050 <logMsg@plt>
movl   $0x0,-0x20(%rbp)
jmp    12a7 <doUpgrade+0x157>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     12a2 <doUpgrade+0x152>
mov    -0x28(%rbp),%edi
mov    %edi,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
call   1070 <upgradeTo@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    129d <doUpgrade+0x14d>
jmp    12a2 <doUpgrade+0x152>
jmp    126f <doUpgrade+0x11f>
jmp    12a7 <doUpgrade+0x157>
jmp    12ac <doUpgrade+0x15c>
jmp    12b1 <doUpgrade+0x161>
jmp    12b6 <doUpgrade+0x166>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doUpgrade>:
push   %rbp
push   %r14
push   %rbx
test   %esi,%esi
je     1198 <doUpgrade+0x48>
mov    (%rdx),%rdi
mov    $0xffffffff,%esi
call   1030 <strToInt@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1040 <getDbVersion@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%r14d
je     11a1 <doUpgrade+0x51>
cmp    %ebx,%r14d
jge    11bf <doUpgrade+0x6f>
lea    0xedb(%rip),%rsi
xor    %ebp,%ebp
mov    $0x2,%edi
mov    %r14d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <logMsg@plt>
jmp    11b8 <doUpgrade+0x68>
xor    %eax,%eax
call   1040 <getDbVersion@plt>
mov    %eax,%ebx
lea    0xe58(%rip),%rsi
xor    %ebp,%ebp
mov    $0x2,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <logMsg@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
jne    11d4 <doUpgrade+0x84>
lea    0xee0(%rip),%rsi
xor    %ebp,%ebp
mov    $0x2,%edi
mov    %r14d,%edx
jmp    11b1 <doUpgrade+0x61>
cmp    $0x8,%r14d
jl     11f2 <doUpgrade+0xa2>
lea    0xef7(%rip),%rsi
xor    %ebp,%ebp
mov    $0x2,%edi
mov    %r14d,%edx
mov    $0x7,%ecx
jmp    118f <doUpgrade+0x3f>
xor    %edi,%edi
cmp    %r14d,%ebx
setl   %dil
call   1060 <assert@plt>
cmp    %r14d,%ebx
jge    11b8 <doUpgrade+0x68>
neg    %r14d
add    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1070 <upgradeTo@plt>
test   %eax,%eax
je     1232 <doUpgrade+0xe2>
mov    %eax,%ebp
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    1210 <doUpgrade+0xc0>
jmp    11b8 <doUpgrade+0x68>
xor    %ebp,%ebp
jmp    11b8 <doUpgrade+0x68>

LLVM IR (-O0):
@.str = [94 x i8] c"Please specify a valid database level for the upgrade, the database is currently at level %d.\00"
@.str.1 = [74 x i8] c"Cannot upgrade database to level %d, the database is already at level %d.\00"
@.str.2 = [48 x i8] c"Database is already at level %d, nothing to do.\00"
@.str.3 = [80 x i8] c"Cannot upgrade database to level %d, the maximum available upgrade level is %d.\00"
define i32 @doUpgrade(i32* %0, i32 %1, i8** %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %7
br label %19
14: ; preds = %3
%15 = load i8**, i8*** %6
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @strToInt(i8* %17, i32 -1)
store i32 %18, i32* %7
br label %19
19: ; preds = %14, %13
%20 = call i32 (...) @getDbVersion()
store i32 %20, i32* %9
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %9
%25 = call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 0, i32* %8
br label %71
26: ; preds = %19
%27 = load i32, i32* %7
%28 = load i32, i32* %9
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* %7
%32 = load i32, i32* %9
%33 = call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %32)
store i32 0, i32* %8
br label %70
34: ; preds = %26
%35 = load i32, i32* %7
%36 = load i32, i32* %9
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* %7
%40 = call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %39)
store i32 0, i32* %8
br label %69
41: ; preds = %34
%42 = load i32, i32* %7
%43 = icmp sgt i32 %42, 7
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* %7
%46 = call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 7)
store i32 0, i32* %8
br label %68
47: ; preds = %41
%48 = load i32, i32* %9
%49 = load i32, i32* %7
%50 = icmp slt i32 %48, %49
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %55
55: ; preds = %66, %47
%56 = load i32, i32* %10
%57 = load i32, i32* %7
%58 = icmp sle i32 %56, %57
br i1 %58, label %59, label %67
59: ; preds = %55
%60 = load i32, i32* %10
%61 = add nsw i32 %60, 1
store i32 %61, i32* %10
%62 = call i32 @upgradeTo(i32 %60)
store i32 %62, i32* %8
%63 = load i32, i32* %8
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %59
br label %67
66: ; preds = %59
br label %55
67: ; preds = %65, %55
br label %68
68: ; preds = %67, %44
br label %69
69: ; preds = %68, %38
br label %70
70: ; preds = %69, %30
br label %71
71: ; preds = %70, %23
%72 = load i32, i32* %8
ret i32 %72
}

LLVM IR (-O2):
@.str = [94 x i8] c"Please specify a valid database level for the upgrade, the database is currently at level %d.\00"
@.str.1 = [74 x i8] c"Cannot upgrade database to level %d, the database is already at level %d.\00"
@.str.2 = [48 x i8] c"Database is already at level %d, nothing to do.\00"
@.str.3 = [80 x i8] c"Cannot upgrade database to level %d, the maximum available upgrade level is %d.\00"
define i32 @doUpgrade(i32* nocapture readnone %0, i32 %1, i8** nocapture %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 (...) @getDbVersion()
br label %12
7: ; preds = %3
%8 = load i8*, i8** %2
%9 = tail call i32 @strToInt(i8* %8, i32 -1)
%10 = tail call i32 (...) @getDbVersion()
%11 = icmp eq i32 %9, -1
br i1 %11, label %12, label %15
12: ; preds = %5, %7
%13 = phi i32 [ %6, %5 ], [ %10, %7 ]
%14 = tail call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %38
15: ; preds = %7
%16 = icmp slt i32 %9, %10
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %10)
br label %38
19: ; preds = %15
%20 = icmp eq i32 %9, %10
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %38
23: ; preds = %19
%24 = icmp sgt i32 %9, 7
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = tail call i32 (i32, i8*, i32, ...) @logMsg(i32 2, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 7)
br label %38
27: ; preds = %23
%28 = icmp slt i32 %10, %9
%29 = zext i1 %28 to i32
%30 = tail call i32 @assert(i32 %29)
br i1 %28, label %33, label %38
31: ; preds = %33
%32 = icmp eq i32 %35, %9
br i1 %32, label %38, label %33
33: ; preds = %27, %31
%34 = phi i32 [ %35, %31 ], [ %10, %27 ]
%35 = add nsw i32 %34, 1
%36 = tail call i32 @upgradeTo(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %31
38: ; preds = %33, %31, %27, %17, %25, %21, %12
%39 = phi i32 [ 0, %12 ], [ 0, %17 ], [ 0, %21 ], [ 0, %25 ], [ undef, %27 ], [ 0, %33 ], [ %36, %31 ]
ret i32 %39
}