#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*/ SEEK_SET;
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ info(int, char *);
int upgrade_st(int, int /*<<< orphan*/ *);
int upgrade_zensys(int, int /*<<< orphan*/ *);
int handle_upgrade(int devfd, char *firmware) {
  FILE *f;
  char buf[64];
  int ret;
  f = fopen(firmware, "r");
  if (f == NULL)
    die("error: can't open '%s'\n", firmware);
  if (fgets(buf, 64, f) == NULL || buf[0] != ':')
    die("error: bad firmware image\n");
  fseek(f, 0, SEEK_SET);
  if (buf[7] == '0' && buf[8] == '0')
    ret = upgrade_zensys(devfd, f);
  else
    ret = upgrade_st(devfd, f);
  fclose(f);
  if (ret == 0)
    info(1, "Operation was successful.  "
            "Please reboot the VRC0P.\n");
  else
    info(0, "Operation completed with warnings.\n");
  return ret;
}


x86 assembly (clang, -O0):
<handle_upgrade>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <handle_upgrade+0x40>
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1090 <die@plt>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11e9 <handle_upgrade+0x69>
movsbl -0x60(%rbp),%eax
cmp    $0x3a,%eax
je     11f7 <handle_upgrade+0x77>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1090 <die@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
movsbl -0x59(%rbp),%eax
cmp    $0x30,%eax
jne    1239 <handle_upgrade+0xb9>
movsbl -0x58(%rbp),%eax
cmp    $0x30,%eax
jne    1239 <handle_upgrade+0xb9>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <upgrade_zensys@plt>
mov    %eax,-0x64(%rbp)
jmp    1248 <handle_upgrade+0xc8>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <upgrade_st@plt>
mov    %eax,-0x64(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x64(%rbp)
jne    1271 <handle_upgrade+0xf1>
mov    $0x1,%edi
lea    0xdce(%rip),%rsi
call   10a0 <info@plt>
jmp    127f <handle_upgrade+0xff>
xor    %edi,%edi
lea    0xdf0(%rip),%rsi
call   10a0 <info@plt>
mov    -0x64(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_upgrade>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0xe6b(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b6 <handle_upgrade+0x36>
lea    0xe56(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1090 <die@plt>
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d1 <handle_upgrade+0x51>
cmpb   $0x3a,(%rsp)
je     11df <handle_upgrade+0x5f>
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1090 <die@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
cmpb   $0x30,0x7(%rsp)
jne    120d <handle_upgrade+0x8d>
cmpb   $0x30,0x8(%rsp)
jne    120d <handle_upgrade+0x8d>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <upgrade_zensys@plt>
jmp    1218 <handle_upgrade+0x98>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <upgrade_st@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %ebp,%ebp
je     1231 <handle_upgrade+0xb1>
lea    0xe3d(%rip),%rsi
xor    %edi,%edi
jmp    123d <handle_upgrade+0xbd>
lea    0xdfd(%rip),%rsi
mov    $0x1,%edi
call   10a0 <info@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"error: can't open '%s'\0A\00"
@.str.2 = [27 x i8] c"error: bad firmware image\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [53 x i8] c"Operation was successful. Please reboot the VRC0P.\0A\00"
@.str.4 = [36 x i8] c"Operation completed with warnings.\0A\00"
define i32 @handle_upgrade(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [64 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %12, %2
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%17 = load i32*, i32** %5
%18 = call i32* @fgets(i8* %16, i32 64, i32* %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 58
br i1 %24, label %25, label %27
25: ; preds = %20, %15
%26 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32*, i32** %5
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 7
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 48
br i1 %34, label %35, label %44
35: ; preds = %27
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 8
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 48
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i32, i32* %3
%42 = load i32*, i32** %5
%43 = call i32 @upgrade_zensys(i32 %41, i32* %42)
store i32 %43, i32* %7
br label %48
44: ; preds = %35, %27
%45 = load i32, i32* %3
%46 = load i32*, i32** %5
%47 = call i32 @upgrade_st(i32 %45, i32* %46)
store i32 %47, i32* %7
br label %48
48: ; preds = %44, %40
%49 = load i32*, i32** %5
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = call i32 @info(i32 1, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %57
55: ; preds = %48
%56 = call i32 @info(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55, %53
%58 = load i32, i32* %7
ret i32 %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"error: can't open '%s'\0A\00"
@.str.2 = [27 x i8] c"error: bad firmware image\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [53 x i8] c"Operation was successful. Please reboot the VRC0P.\0A\00"
@.str.4 = [36 x i8] c"Operation completed with warnings.\0A\00"
define i32 @handle_upgrade(i32 %0, i8* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %9
9: ; preds = %7, %2
%10 = call i32* @fgets(i8* %4, i32 64, i32* %5)
%11 = icmp eq i32* %10, null
%12 = load i8, i8* %4
%13 = icmp ne i8 %12, 58
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %9, %15
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %5, i32 0, i32 %18)
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 7
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 48
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 8
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 48
%26 = select i1 %22, i1 %25, i1 false
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @upgrade_zensys(i32 %0, i32* %5)
br label %31
29: ; preds = %17
%30 = call i32 @upgrade_st(i32 %0, i32* %5)
br label %31
31: ; preds = %29, %27
%32 = phi i32 [ %28, %27 ], [ %30, %29 ]
%33 = call i32 @fclose(i32* %5)
%34 = icmp eq i32 %32, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @info(i32 1, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %39
37: ; preds = %31
%38 = call i32 @info(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %35
ret i32 %32
}