#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*/ upgfile;
struct stat {
  int dummy;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ RB_AUTOBOOT;
int /*<<< orphan*/ check_wget_ok(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ print_usage_exit(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ reboot(int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  pid_t sid;
  char upgfile[512];
  int tPid;
  struct stat stat_buf;
  int use_storage_firmware = 0;
  if (argc < 2) {
    print_usage_exit(1);
  }
  if (argv[1][0] == '-') {
    switch (argv[1][1]) {
    case 'b':
      strcpy(upgfile, "/tmp/mnt/USB/firmware.bin");
      if (stat(upgfile, &stat_buf)) {
        printf("There is no \"firmware.bin\" in USB or USB is not inserted\n");
        print_usage_exit(1);
      } else {
        use_storage_firmware = 1;
        printf("Using \"firmware.bin\" in USB to upgrade.\n");
      }
      strcpy(upgfile, "/usr/sbin/ezpup /tmp/mnt/USB/firmware.bin");
      break;
    case 's':
      strcpy(upgfile, "/tmp/mnt/SD/firmware.bin");
      if (stat(upgfile, &stat_buf)) {
        printf(
            "There is no \"firmware.bin\" in SD card or SD card is inserted\n");
        print_usage_exit(1);
      } else {
        use_storage_firmware = 2;
        printf("Using \"firmware.bin\" in SD card to upgrade.\n");
      }
      strcpy(upgfile, "/usr/sbin/ezpup /tmp/mnt/SD/firmware.bin");
      break;
    case 'a':
      strcpy(upgfile, "/tmp/mnt/SD/firmware.bin");
      if (stat(upgfile, &stat_buf)) {
        printf(
            "There is no \"firmware.bin\" in SD card or SD card is inserted\n");
        strcpy(upgfile, "/tmp/mnt/USB/firmware.bin");
        if (stat(upgfile, &stat_buf)) {
          printf(
              "There is no \"firmware.bin\" in USB or USB is not inserted\n");
          print_usage_exit(1);
        } else {
          use_storage_firmware = 1;
          printf("Using \"firmware.bin\" in USB to upgrade.\n");
          strcpy(upgfile, "/usr/sbin/ezpup /tmp/mnt/USB/firmware.bin");
        }
      } else {
        use_storage_firmware = 2;
        printf("Using \"firmware.bin\" in SD card to upgrade.\n");
        strcpy(upgfile, "/usr/sbin/ezpup /tmp/mnt/SD/firmware.bin");
      }
      break;
    case 'u':
      if (argc == 3) {
        snprintf(upgfile, sizeof(upgfile),
                 "wget %s -O /tmp/firmware 2>&1 | cat > /tmp/fw_log.txt",
                 argv[2]);
        system(upgfile);
        if (!check_wget_ok("/tmp/fw_log.txt")) {
          printf("Unable to get URL %s\n", argv[2]);
          print_usage_exit(1);
        }
        snprintf(upgfile, sizeof(upgfile), "/usr/sbin/ezpup /tmp/firmware");
        printf("Get firmware from %s successfully, using /tmp/firmware to "
               "upgrade.\n",
               argv[2]);
      } else {
        print_usage_exit(1);
      }
      break;
    }
  } else {
    snprintf(upgfile, sizeof(upgfile), "/usr/sbin/ezpup %s", argv[1]);
  }
  tPid = fork();
  if (tPid < 0) {
    exit(1);
  } else if (tPid > 0) {
    exit(0);
  } else {
    FILE *fp;
    sid = setsid();
    if (sid < 0)
      exit(EXIT_FAILURE);
    system(upgfile);
    system("/tmp/ezp-i2c gauge upgrade finish");
    fp = fopen("/tmp/fw_incorrect", "r");
    if (fp) {
      printf("Upgrade failed, firmware incorrect\n");
      fclose(fp);
    } else {
      printf("Upgrade successfully\n");
    }
    fp = fopen("/tmp/fw_correct", "w+");
    if (fp) {
      fprintf(fp, "success\n");
      fclose(fp);
    }
    sleep(5);
    reboot(RB_AUTOBOOT);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x22c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1227 <main+0x37>
mov    $0x1,%edi
call   10f0 <print_usage_exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    150a <main+0x31a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl 0x1(%rax),%eax
add    $0xffffff9f,%eax
mov    %eax,%ecx
mov    %rcx,-0x240(%rbp)
sub    $0x14,%eax
ja     1505 <main+0x315>
mov    -0x240(%rbp),%rax
lea    0xd96(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    -0x220(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%eax
je     12bf <main+0xcf>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <print_usage_exit@plt>
jmp    12d7 <main+0xe7>
movl   $0x1,-0x22c(%rbp)
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x220(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1505 <main+0x315>
lea    -0x220(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%eax
je     133b <main+0x14b>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <print_usage_exit@plt>
jmp    1353 <main+0x163>
movl   $0x2,-0x22c(%rbp)
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x220(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1505 <main+0x315>
lea    -0x220(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%eax
je     1424 <main+0x234>
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x220(%rbp),%rdi
lea    0xd11(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%eax
je     13f4 <main+0x204>
lea    0xc76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <print_usage_exit@plt>
jmp    141f <main+0x22f>
movl   $0x1,-0x22c(%rbp)
lea    0xc89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x220(%rbp),%rdi
lea    0xc9d(%rip),%rsi
call   1030 <strcpy@plt>
jmp    144f <main+0x25f>
movl   $0x2,-0x22c(%rbp)
lea    0xcea(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x220(%rbp),%rdi
lea    0xd02(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1505 <main+0x315>
cmpl   $0x3,-0x8(%rbp)
jne    14f6 <main+0x306>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
mov    $0x200,%esi
lea    0xcfc(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x220(%rbp),%rdi
call   1050 <system@plt>
lea    0xd08(%rip),%rdi
call   1090 <check_wget_ok@plt>
cmp    $0x0,%eax
jne    14c1 <main+0x2d1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <print_usage_exit@plt>
lea    -0x220(%rbp),%rdi
mov    $0x200,%esi
lea    0xced(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xcf5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1500 <main+0x310>
mov    $0x1,%edi
call   10f0 <print_usage_exit@plt>
jmp    1505 <main+0x315>
jmp    152c <main+0x33c>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x200,%esi
lea    0xcfe(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x0,-0x224(%rbp)
jge    1550 <main+0x360>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x224(%rbp)
jle    1564 <main+0x374>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1588 <main+0x398>
mov    0x2a5f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x220(%rbp),%rdi
call   1050 <system@plt>
lea    0xc9b(%rip),%rdi
call   1050 <system@plt>
lea    0xcb1(%rip),%rdi
lea    0xcbc(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     15e7 <main+0x3f7>
lea    0xc9d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x238(%rbp),%rdi
call   1040 <fclose@plt>
jmp    15f5 <main+0x405>
lea    0xca2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xcaa(%rip),%rdi
lea    0xcb3(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     163c <main+0x44c>
mov    -0x238(%rbp),%rdi
lea    0xc8e(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x238(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x5,%edi
call   1100 <sleep@plt>
mov    0x298b(%rip),%rax
mov    (%rax),%edi
call   10e0 <reboot@plt>
jmp    1659 <main+0x469>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x1,%edi
jg     121d <main+0x1d>
mov    $0x1,%edi
call   1100 <print_usage_exit@plt>
mov    0x8(%rbx),%rcx
cmpb   $0x2d,(%rcx)
jne    12b9 <main+0xb9>
movsbl 0x1(%rcx),%eax
add    $0xffffff9f,%eax
cmp    $0x14,%eax
ja     1427 <main+0x227>
lea    0xdbf(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe3d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
je     13f5 <main+0x1f5>
lea    0xfa4(%rip),%rdi
call   1040 <puts@plt>
lea    0xdde(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
je     140a <main+0x20a>
lea    0xfd4(%rip),%rdi
call   1040 <puts@plt>
jmp    13e9 <main+0x1e9>
lea    0xe95(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1080 <snprintf@plt>
jmp    1427 <main+0x227>
lea    0xd87(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
je     140a <main+0x20a>
lea    0xf7d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1100 <print_usage_exit@plt>
jmp    1416 <main+0x216>
lea    0xd6d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
je     13f5 <main+0x1f5>
lea    0xed4(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1100 <print_usage_exit@plt>
lea    0xd1e(%rip),%rsi
jmp    141d <main+0x21d>
cmp    $0x3,%ebp
jne    13e9 <main+0x1e9>
mov    0x10(%rbx),%rcx
lea    0xd32(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    %rbp,%rdi
call   1060 <system@plt>
lea    0xd35(%rip),%rdi
call   10a0 <check_wget_ok@plt>
test   %eax,%eax
jne    13bd <main+0x1bd>
mov    0x10(%rbx),%rsi
lea    0xd31(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1100 <print_usage_exit@plt>
lea    0xd2f(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    0x10(%rbx),%rsi
lea    0xd31(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1427 <main+0x227>
mov    $0x1,%edi
call   1100 <print_usage_exit@plt>
jmp    1427 <main+0x227>
lea    0xdf6(%rip),%rdi
call   1040 <puts@plt>
lea    0xc76(%rip),%rsi
jmp    141d <main+0x21d>
lea    0xe4a(%rip),%rdi
call   1040 <puts@plt>
lea    0xc37(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
call   1120 <fork@plt>
test   %eax,%eax
js     14ff <main+0x2ff>
jne    1509 <main+0x309>
xor    %eax,%eax
call   1090 <setsid@plt>
test   %rax,%rax
js     1510 <main+0x310>
lea    0x10(%rsp),%rdi
call   1060 <system@plt>
lea    0xd0b(%rip),%rdi
call   1060 <system@plt>
lea    0xd21(%rip),%rdi
lea    0xd2c(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1493 <main+0x293>
mov    %rax,%rbx
lea    0xd4b(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    149f <main+0x29f>
lea    0xd20(%rip),%rdi
call   1040 <puts@plt>
lea    0xcf8(%rip),%rdi
lea    0xd01(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     14db <main+0x2db>
mov    %rax,%rbx
lea    0xcf0(%rip),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x5,%edi
call   1110 <sleep@plt>
mov    0x2aec(%rip),%rax
mov    (%rax),%edi
call   10f0 <reboot@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    0x2ac9(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [26 x i8] c"/tmp/mnt/USB/firmware.bin\00"
@.str.1 = [58 x i8] c"There is no \22firmware.bin\22 in USB or USB is not inserted\0A\00"
@.str.2 = [41 x i8] c"Using \22firmware.bin\22 in USB to upgrade.\0A\00"
@.str.3 = [42 x i8] c"/usr/sbin/ezpup /tmp/mnt/USB/firmware.bin\00"
@.str.4 = [25 x i8] c"/tmp/mnt/SD/firmware.bin\00"
@.str.5 = [62 x i8] c"There is no \22firmware.bin\22 in SD card or SD card is inserted\0A\00"
@.str.6 = [45 x i8] c"Using \22firmware.bin\22 in SD card to upgrade.\0A\00"
@.str.7 = [41 x i8] c"/usr/sbin/ezpup /tmp/mnt/SD/firmware.bin\00"
@.str.8 = [54 x i8] c"wget %s -O /tmp/firmware 2>&1 | cat > /tmp/fw_log.txt\00"
@.str.9 = [16 x i8] c"/tmp/fw_log.txt\00"
@.str.10 = [22 x i8] c"Unable to get URL %s\0A\00"
@.str.11 = [30 x i8] c"/usr/sbin/ezpup /tmp/firmware\00"
@.str.12 = [68 x i8] c"Get firmware from %s successfully, using /tmp/firmware to upgrade.\0A\00"
@.str.13 = [19 x i8] c"/usr/sbin/ezpup %s\00"
@EXIT_FAILURE = global i32 0
@.str.14 = [34 x i8] c"/tmp/ezp-i2c gauge upgrade finish\00"
@.str.15 = [18 x i8] c"/tmp/fw_incorrect\00"
@.str.16 = [2 x i8] c"r\00"
@.str.17 = [36 x i8] c"Upgrade failed, firmware incorrect\0A\00"
@.str.18 = [22 x i8] c"Upgrade successfully\0A\00"
@.str.19 = [16 x i8] c"/tmp/fw_correct\00"
@.str.20 = [3 x i8] c"w+\00"
@.str.21 = [9 x i8] c"success\0A\00"
@RB_AUTOBOOT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [512 x i8]
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @print_usage_exit(i32 1)
br label %16
16: ; preds = %14, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 45
br i1 %23, label %24, label %115
24: ; preds = %16
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
switch i32 %30, label %114 [
i32 98, label %31
i32 115, label %45
i32 97, label %59
i32 117, label %85
]
31: ; preds = %24
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%35 = call i32 @stat(i8* %34, %struct.stat* %9)
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @print_usage_exit(i32 1)
br label %42
40: ; preds = %31
store i32 1, i32* %10
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%44 = call i32 @strcpy(i8* %43, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %114
45: ; preds = %24
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%47 = call i32 @strcpy(i8* %46, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%49 = call i32 @stat(i8* %48, %struct.stat* %9)
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 @print_usage_exit(i32 1)
br label %56
54: ; preds = %45
store i32 2, i32* %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%58 = call i32 @strcpy(i8* %57, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
br label %114
59: ; preds = %24
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%61 = call i32 @strcpy(i8* %60, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%63 = call i32 @stat(i8* %62, %struct.stat* %9)
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %80
65: ; preds = %59
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.5, i64 0, i64 0))
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%68 = call i32 @strcpy(i8* %67, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%70 = call i32 @stat(i8* %69, %struct.stat* %9)
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %65
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%74 = call i32 @print_usage_exit(i32 1)
br label %79
75: ; preds = %65
store i32 1, i32* %10
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%77 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%78 = call i32 @strcpy(i8* %77, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %79
79: ; preds = %75, %72
br label %84
80: ; preds = %59
store i32 2, i32* %10
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%83 = call i32 @strcpy(i8* %82, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
br label %84
84: ; preds = %80, %79
br label %114
85: ; preds = %24
%86 = load i32, i32* %4
%87 = icmp eq i32 %86, 3
br i1 %87, label %88, label %111
88: ; preds = %85
%89 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%90 = load i8**, i8*** %5
%91 = getelementptr inbounds i8*, i8** %90, i64 2
%92 = load i8*, i8** %91
%93 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %89, i32 512, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0), i8* %92)
%94 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%95 = call i32 @system(i8* %94)
%96 = call i32 @check_wget_ok(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%97 = icmp ne i32 %96, 0
br i1 %97, label %104, label %98
98: ; preds = %88
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 2
%101 = load i8*, i8** %100
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i8* %101)
%103 = call i32 @print_usage_exit(i32 1)
br label %104
104: ; preds = %98, %88
%105 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%106 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %105, i32 512, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0))
%107 = load i8**, i8*** %5
%108 = getelementptr inbounds i8*, i8** %107, i64 2
%109 = load i8*, i8** %108
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.12, i64 0, i64 0), i8* %109)
br label %113
111: ; preds = %85
%112 = call i32 @print_usage_exit(i32 1)
br label %113
113: ; preds = %111, %104
br label %114
114: ; preds = %24, %113, %84, %56, %42
br label %121
115: ; preds = %16
%116 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%117 = load i8**, i8*** %5
%118 = getelementptr inbounds i8*, i8** %117, i64 1
%119 = load i8*, i8** %118
%120 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %116, i32 512, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.13, i64 0, i64 0), i8* %119)
br label %121
121: ; preds = %115, %114
%122 = call i32 (...) @fork()
store i32 %122, i32* %8
%123 = load i32, i32* %8
%124 = icmp slt i32 %123, 0
br i1 %124, label %125, label %127
125: ; preds = %121
%126 = call i32 @exit(i32 1)
unreachable
127: ; preds = %121
%128 = load i32, i32* %8
%129 = icmp sgt i32 %128, 0
br i1 %129, label %130, label %132
130: ; preds = %127
%131 = call i32 @exit(i32 0)
unreachable
132: ; preds = %127
%133 = call i64 (...) @setsid()
store i64 %133, i64* %6
%134 = load i64, i64* %6
%135 = icmp slt i64 %134, 0
br i1 %135, label %136, label %139
136: ; preds = %132
%137 = load i32, i32* @EXIT_FAILURE
%138 = call i32 @exit(i32 %137)
unreachable
139: ; preds = %132
%140 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%141 = call i32 @system(i8* %140)
%142 = call i32 @system(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.14, i64 0, i64 0))
%143 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.15, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.16, i64 0, i64 0))
store i32* %143, i32** %11
%144 = load i32*, i32** %11
%145 = icmp ne i32* %144, null
br i1 %145, label %146, label %150
146: ; preds = %139
%147 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.17, i64 0, i64 0))
%148 = load i32*, i32** %11
%149 = call i32 @fclose(i32* %148)
br label %152
150: ; preds = %139
%151 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.18, i64 0, i64 0))
br label %152
152: ; preds = %150, %146
%153 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.19, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.20, i64 0, i64 0))
store i32* %153, i32** %11
%154 = load i32*, i32** %11
%155 = icmp ne i32* %154, null
br i1 %155, label %156, label %161
156: ; preds = %152
%157 = load i32*, i32** %11
%158 = call i32 @fprintf(i32* %157, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.21, i64 0, i64 0))
%159 = load i32*, i32** %11
%160 = call i32 @fclose(i32* %159)
br label %161
161: ; preds = %156, %152
%162 = call i32 @sleep(i32 5)
%163 = load i32, i32* @RB_AUTOBOOT
%164 = call i32 @reboot(i32 %163)
br label %165
165: ; preds = %161
br label %166
166: ; preds = %165
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [26 x i8] c"/tmp/mnt/USB/firmware.bin\00"
@.str.3 = [42 x i8] c"/usr/sbin/ezpup /tmp/mnt/USB/firmware.bin\00"
@.str.4 = [25 x i8] c"/tmp/mnt/SD/firmware.bin\00"
@.str.7 = [41 x i8] c"/usr/sbin/ezpup /tmp/mnt/SD/firmware.bin\00"
@.str.8 = [54 x i8] c"wget %s -O /tmp/firmware 2>&1 | cat > /tmp/fw_log.txt\00"
@.str.9 = [16 x i8] c"/tmp/fw_log.txt\00"
@.str.10 = [22 x i8] c"Unable to get URL %s\0A\00"
@.str.11 = [30 x i8] c"/usr/sbin/ezpup /tmp/firmware\00"
@.str.12 = [68 x i8] c"Get firmware from %s successfully, using /tmp/firmware to upgrade.\0A\00"
@.str.13 = [19 x i8] c"/usr/sbin/ezpup %s\00"
@EXIT_FAILURE = global i32 0
@.str.14 = [34 x i8] c"/tmp/ezp-i2c gauge upgrade finish\00"
@.str.15 = [18 x i8] c"/tmp/fw_incorrect\00"
@.str.16 = [2 x i8] c"r\00"
@.str.19 = [16 x i8] c"/tmp/fw_correct\00"
@.str.20 = [3 x i8] c"w+\00"
@.str.21 = [9 x i8] c"success\0A\00"
@RB_AUTOBOOT = global i32 0
@str = [21 x i8] c"Upgrade successfully\00"
@str.22 = [35 x i8] c"Upgrade failed, firmware incorrect\00"
@str.27 = [44 x i8] c"Using \22firmware.bin\22 in SD card to upgrade.\00"
@str.28 = [61 x i8] c"There is no \22firmware.bin\22 in SD card or SD card is inserted\00"
@str.29 = [40 x i8] c"Using \22firmware.bin\22 in USB to upgrade.\00"
@str.30 = [57 x i8] c"There is no \22firmware.bin\22 in USB or USB is not inserted\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @print_usage_exit(i32 1)
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 45
br i1 %14, label %15, label %78
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %12, i64 1
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
switch i32 %18, label %80 [
i32 98, label %19
i32 115, label %30
i32 97, label %41
i32 117, label %59
]
19: ; preds = %15
%20 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @stat(i8* %5, %struct.stat* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.30, i64 0, i64 0))
%25 = call i32 @print_usage_exit(i32 1)
br label %28
26: ; preds = %19
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.29, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %80
30: ; preds = %15
%31 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @stat(i8* %5, %struct.stat* %4)
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.28, i64 0, i64 0))
%36 = call i32 @print_usage_exit(i32 1)
br label %39
37: ; preds = %30
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.27, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
br label %80
41: ; preds = %15
%42 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @stat(i8* %5, %struct.stat* %4)
%44 = icmp eq i32 %43, 0
br i1 %44, label %56, label %45
45: ; preds = %41
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.28, i64 0, i64 0))
%47 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%48 = call i32 @stat(i8* %5, %struct.stat* %4)
%49 = icmp eq i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %45
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.30, i64 0, i64 0))
%52 = call i32 @print_usage_exit(i32 1)
br label %80
53: ; preds = %45
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.29, i64 0, i64 0))
%55 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %80
56: ; preds = %41
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.27, i64 0, i64 0))
%58 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
br label %80
59: ; preds = %15
%60 = icmp eq i32 %0, 3
br i1 %60, label %61, label %76
61: ; preds = %59
%62 = getelementptr inbounds i8*, i8** %1, i64 2
%63 = load i8*, i8** %62
%64 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0), i8* %63)
%65 = call i32 @system(i8* %5)
%66 = call i32 @check_wget_ok(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %61
%69 = load i8*, i8** %62
%70 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i8* %69)
%71 = call i32 @print_usage_exit(i32 1)
br label %72
72: ; preds = %68, %61
%73 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0))
%74 = load i8*, i8** %62
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @.str.12, i64 0, i64 0), i8* %74)
br label %80
76: ; preds = %59
%77 = tail call i32 @print_usage_exit(i32 1)
br label %80
78: ; preds = %10
%79 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.13, i64 0, i64 0), i8* %12)
br label %80
80: ; preds = %28, %39, %15, %50, %53, %56, %76, %72, %78
%81 = call i32 (...) @fork()
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %80
%86 = icmp eq i32 %81, 0
br i1 %86, label %89, label %87
87: ; preds = %85
%88 = call i32 @exit(i32 0)
unreachable
89: ; preds = %85
%90 = call i64 (...) @setsid()
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32, i32* @EXIT_FAILURE
%94 = call i32 @exit(i32 %93)
unreachable
95: ; preds = %89
%96 = call i32 @system(i8* %5)
%97 = call i32 @system(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.14, i64 0, i64 0))
%98 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.15, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.16, i64 0, i64 0))
%99 = icmp eq i32* %98, null
br i1 %99, label %103, label %100
100: ; preds = %95
%101 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.22, i64 0, i64 0))
%102 = call i32 @fclose(i32* %98)
br label %105
103: ; preds = %95
%104 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %105
105: ; preds = %103, %100
%106 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.19, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.20, i64 0, i64 0))
%107 = icmp eq i32* %106, null
br i1 %107, label %111, label %108
108: ; preds = %105
%109 = call i64 @fwrite(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.21, i64 0, i64 0), i64 8, i64 1, i32* %106)
%110 = call i32 @fclose(i32* %106)
br label %111
111: ; preds = %108, %105
%112 = call i32 @sleep(i32 5)
%113 = load i32, i32* @RB_AUTOBOOT
%114 = call i32 @reboot(i32 %113)
ret i32 0
}