#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 */

/* Variables and functions */
int /*<<< orphan*/ Info_ValueForKey(char const *, char *);
int /*<<< orphan*/ UI_GetBestScore(int, int *, int *);
int UI_GetNumSPTiers();
char *UI_GetSpecialArenaInfo(char *);
int atoi(int /*<<< orphan*/);
int UI_TierCompleted(int levelWon) {
  int level;
  int n;
  int tier;
  int score;
  int skill;
  const char *info;
  tier = levelWon / 4;
  level = tier * 4;
  if (tier == UI_GetNumSPTiers()) {
    info = UI_GetSpecialArenaInfo("training");
    if (levelWon == atoi(Info_ValueForKey(info, "num"))) {
      return 0;
    }
    info = UI_GetSpecialArenaInfo("final");
    if (!info || levelWon == atoi(Info_ValueForKey(info, "num"))) {
      return tier + 1;
    }
    return -1;
  }
  for (n = 0; n < 4; n++, level++) {
    UI_GetBestScore(level, &score, &skill);
    if (score != 1) {
      return -1;
    }
  }
  return tier + 1;
}


x86 assembly (clang, -O0):
<UI_TierCompleted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <UI_GetNumSPTiers@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jne    1231 <UI_TierCompleted+0xe1>
lea    0xe6d(%rip),%rdi
call   1050 <UI_GetSpecialArenaInfo@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1040 <Info_ValueForKey@plt>
mov    %eax,%edi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jne    11d2 <UI_TierCompleted+0x82>
movl   $0x0,-0x4(%rbp)
jmp    128d <UI_TierCompleted+0x13d>
lea    0xe34(%rip),%rdi
call   1050 <UI_GetSpecialArenaInfo@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1217 <UI_TierCompleted+0xc7>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1040 <Info_ValueForKey@plt>
mov    %eax,%edi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jne    1225 <UI_TierCompleted+0xd5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    128d <UI_TierCompleted+0x13d>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <UI_TierCompleted+0x13d>
movl   $0x0,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
jge    1284 <UI_TierCompleted+0x134>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <UI_GetBestScore@plt>
cmpl   $0x1,-0x18(%rbp)
je     1268 <UI_TierCompleted+0x118>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <UI_TierCompleted+0x13d>
jmp    126d <UI_TierCompleted+0x11d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1238 <UI_TierCompleted+0xe8>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UI_TierCompleted>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x3(%rbx),%ebp
test   %edi,%edi
cmovns %edi,%ebp
sar    $0x2,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <UI_GetNumSPTiers@plt>
cmp    %eax,%ebp
jne    11dd <UI_TierCompleted+0x8d>
lea    0xe86(%rip),%rdi
call   1050 <UI_GetSpecialArenaInfo@plt>
lea    0xe83(%rip),%rsi
mov    %rax,%rdi
call   1040 <Info_ValueForKey@plt>
mov    %eax,%edi
call   1060 <atoi@plt>
cmp    %ebx,%eax
je     125e <UI_TierCompleted+0x10e>
lea    0xe69(%rip),%rdi
call   1050 <UI_GetSpecialArenaInfo@plt>
test   %rax,%rax
je     11d2 <UI_TierCompleted+0x82>
lea    0xe54(%rip),%rsi
mov    %rax,%rdi
call   1040 <Info_ValueForKey@plt>
mov    %eax,%edi
call   1060 <atoi@plt>
mov    $0xffffffff,%r14d
cmp    %ebx,%eax
jne    125e <UI_TierCompleted+0x10e>
add    $0x1,%ebp
mov    %ebp,%r14d
jmp    125e <UI_TierCompleted+0x10e>
lea    0x0(,%rbp,4),%ebx
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1070 <UI_GetBestScore@plt>
mov    $0xffffffff,%r14d
cmpl   $0x1,0x8(%rsp)
jne    125e <UI_TierCompleted+0x10e>
mov    %ebx,%edi
or     $0x1,%edi
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1070 <UI_GetBestScore@plt>
cmpl   $0x1,0x8(%rsp)
jne    125e <UI_TierCompleted+0x10e>
mov    %ebx,%edi
or     $0x2,%edi
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1070 <UI_GetBestScore@plt>
cmpl   $0x1,0x8(%rsp)
jne    125e <UI_TierCompleted+0x10e>
or     $0x3,%ebx
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1070 <UI_GetBestScore@plt>
add    $0x1,%ebp
cmpl   $0x1,0x8(%rsp)
mov    $0xffffffff,%r14d
cmove  %ebp,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"training\00"
@.str.1 = [4 x i8] c"num\00"
@.str.2 = [6 x i8] c"final\00"
define i32 @UI_TierCompleted(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %3
%10 = load i32, i32* %3
%11 = sdiv i32 %10, 4
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = mul nsw i32 %12, 4
store i32 %13, i32* %4
%14 = load i32, i32* %6
%15 = call i32 (...) @UI_GetNumSPTiers()
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %39
17: ; preds = %1
%18 = call i8* @UI_GetSpecialArenaInfo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %9
%19 = load i32, i32* %3
%20 = load i8*, i8** %9
%21 = call i32 @Info_ValueForKey(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @atoi(i32 %21)
%23 = icmp eq i32 %19, %22
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 0, i32* %2
br label %58
25: ; preds = %17
%26 = call i8* @UI_GetSpecialArenaInfo(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* %26, i8** %9
%27 = load i8*, i8** %9
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* %3
%31 = load i8*, i8** %9
%32 = call i32 @Info_ValueForKey(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @atoi(i32 %32)
%34 = icmp eq i32 %30, %33
br i1 %34, label %35, label %38
35: ; preds = %29, %25
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %2
br label %58
38: ; preds = %29
store i32 -1, i32* %2
br label %58
39: ; preds = %1
store i32 0, i32* %5
br label %40
40: ; preds = %50, %39
%41 = load i32, i32* %5
%42 = icmp slt i32 %41, 4
br i1 %42, label %43, label %55
43: ; preds = %40
%44 = load i32, i32* %4
%45 = call i32 @UI_GetBestScore(i32 %44, i32* %7, i32* %8)
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 1
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 -1, i32* %2
br label %58
49: ; preds = %43
br label %50
50: ; preds = %49
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
store i32 %52, i32* %5
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
store i32 %54, i32* %4
br label %40
55: ; preds = %40
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
store i32 %57, i32* %2
br label %58
58: ; preds = %55, %48, %38, %35, %24
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
@.str = [9 x i8] c"training\00"
@.str.1 = [4 x i8] c"num\00"
@.str.2 = [6 x i8] c"final\00"
define i32 @UI_TierCompleted(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = sdiv i32 %0, 4
%7 = tail call i32 (...) @UI_GetNumSPTiers()
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %23
9: ; preds = %1
%10 = tail call i8* @UI_GetSpecialArenaInfo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @Info_ValueForKey(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @atoi(i32 %11)
%13 = icmp eq i32 %12, %0
br i1 %13, label %45, label %14
14: ; preds = %9
%15 = tail call i8* @UI_GetSpecialArenaInfo(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = tail call i32 @Info_ValueForKey(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @atoi(i32 %18)
%20 = icmp eq i32 %19, %0
br i1 %20, label %21, label %45
21: ; preds = %17, %14
%22 = add nsw i32 %6, 1
br label %45
23: ; preds = %1
%24 = shl nsw i32 %6, 2
%25 = call i32 @UI_GetBestScore(i32 %24, i32* %2, i32* %3)
%26 = load i32, i32* %2
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %45
28: ; preds = %23
%29 = or i32 %24, 1
%30 = call i32 @UI_GetBestScore(i32 %29, i32* %2, i32* %3)
%31 = load i32, i32* %2
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %45
33: ; preds = %28
%34 = or i32 %24, 2
%35 = call i32 @UI_GetBestScore(i32 %34, i32* %2, i32* %3)
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %45
38: ; preds = %33
%39 = or i32 %24, 3
%40 = call i32 @UI_GetBestScore(i32 %39, i32* %2, i32* %3)
%41 = load i32, i32* %2
%42 = icmp eq i32 %41, 1
%43 = add nsw i32 %6, 1
%44 = select i1 %42, i32 %43, i32 -1
br label %45
45: ; preds = %38, %23, %28, %33, %17, %9, %21
%46 = phi i32 [ %22, %21 ], [ 0, %9 ], [ -1, %17 ], [ -1, %33 ], [ -1, %28 ], [ -1, %23 ], [ %44, %38 ]
ret i32 %46
}