#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*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
unsigned int read(int, unsigned char *, int);
int /*<<< orphan*/ system(char *);
unsigned int write(int, unsigned char *, int);
int main(int argc, char const *argv[]) {
  int fw_fd = -1;
  int dev_fd = -1;
  unsigned char buff[4096];
  fw_fd = open("/tmp/fwupgrade", O_RDONLY);
  if (fw_fd < 0) {
    printf("open %s failed.\n", "/tmp/fwupgrade");
    return -1;
  }
  dev_fd = open("/dev/mmcblk0", O_RDWR);
  if (dev_fd < 0) {
    printf("open %s failed.\n", "/dev/mmcblk0");
    close(fw_fd);
    return -1;
  }
  unsigned int kernel_len = (5 * 1024 * 1024);
  unsigned int write_size;
  unsigned int read_size;
  lseek(dev_fd, (3 * 1024 * 1024), SEEK_SET);
  while (kernel_len > 0) {
    read_size = read(fw_fd, buff, 4096);
    if (read_size != 4096) {
      system("nvram_cfg set boot_flag=3");
      goto error_close;
    }
    write_size = write(dev_fd, buff, 4096);
    if (write_size != 4096) {
      system("nvram_cfg set boot_flag=3");
      goto error_close;
    }
    kernel_len = kernel_len - 4096;
  }
  unsigned int rootfs_len = (48 * 1024 * 1024);
  lseek(dev_fd, (13 * 1024 * 1024), SEEK_SET);
  while (rootfs_len > 0) {
    read_size = read(fw_fd, buff, 4096);
    if (read_size != 4096) {
      system("nvram_cfg set boot_flag=3");
      goto error_close;
    }
    write_size = write(dev_fd, buff, 4096);
    if (write_size != 4096) {
      system("nvram_cfg set boot_flag=3");
      goto error_close;
    }
    rootfs_len = rootfs_len - 4096;
  }
  system("sync");
  system("sync");
  system("sync");
  close(fw_fd);
  close(dev_fd);
  return 0;
error_close:
  close(fw_fd);
  close(dev_fd);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d8 <main+0x68>
lea    0xe4f(%rip),%rdi
lea    0xe39(%rip),%rsi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <main+0x27a>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe38(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1221 <main+0xb1>
lea    0xe0e(%rip),%rdi
lea    0xe18(%rip),%rsi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <main+0x27a>
movl   $0x500000,-0x1024(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    $0x300000,%esi
call   1060 <lseek@plt>
cmpl   $0x0,-0x1024(%rbp)
jbe    12da <main+0x16a>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x1000,-0x102c(%rbp)
je     1289 <main+0x119>
lea    0xdae(%rip),%rdi
call   1040 <system@plt>
jmp    13d3 <main+0x263>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x1000,-0x1028(%rbp)
je     12c4 <main+0x154>
lea    0xd73(%rip),%rdi
call   1040 <system@plt>
jmp    13d3 <main+0x263>
mov    -0x1024(%rbp),%eax
sub    $0x1000,%eax
mov    %eax,-0x1024(%rbp)
jmp    1241 <main+0xd1>
movl   $0x3000000,-0x1030(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2ce2(%rip),%rax
mov    (%rax),%edx
mov    $0xd00000,%esi
call   1060 <lseek@plt>
cmpl   $0x0,-0x1030(%rbp)
jbe    1393 <main+0x223>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x1000,-0x102c(%rbp)
je     1342 <main+0x1d2>
lea    0xcf5(%rip),%rdi
call   1040 <system@plt>
jmp    13d3 <main+0x263>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x1000,-0x1028(%rbp)
je     137d <main+0x20d>
lea    0xcba(%rip),%rdi
call   1040 <system@plt>
jmp    13d3 <main+0x263>
mov    -0x1030(%rbp),%eax
sub    $0x1000,%eax
mov    %eax,-0x1030(%rbp)
jmp    12fa <main+0x18a>
lea    0xcad(%rip),%rdi
call   1040 <system@plt>
lea    0xca1(%rip),%rdi
call   1040 <system@plt>
lea    0xc95(%rip),%rdi
call   1040 <system@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ea <main+0x27a>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12a9 <main+0x139>
mov    %eax,%r14d
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12d7 <main+0x167>
mov    %eax,%r12d
mov    0x2e0a(%rip),%r15
mov    (%r15),%edx
mov    %eax,%edi
mov    $0x300000,%esi
call   1060 <lseek@plt>
mov    $0xffb00000,%ebp
mov    %rsp,%rbx
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
cmp    $0x1000,%eax
jne    12be <main+0x14e>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x1000,%eax
jne    12be <main+0x14e>
add    $0x1000,%ebp
jne    11e0 <main+0x70>
mov    (%r15),%edx
mov    %r12d,%edi
mov    $0xd00000,%esi
call   1060 <lseek@plt>
mov    $0xfd000000,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
cmp    $0x1000,%eax
jne    12be <main+0x14e>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x1000,%eax
jne    12be <main+0x14e>
add    $0x1000,%ebp
jne    1240 <main+0xd0>
lea    0xdca(%rip),%rbx
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    12f7 <main+0x187>
lea    0xd5f(%rip),%rdi
lea    0xd49(%rip),%rsi
call   1050 <printf@plt>
jmp    12f2 <main+0x182>
lea    0xd68(%rip),%rdi
call   1040 <system@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r12d,%edi
jmp    12ed <main+0x17d>
lea    0xd31(%rip),%rdi
lea    0xd3b(%rip),%rsi
call   1050 <printf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/tmp/fwupgrade\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"open %s failed.\0A\00"
@.str.2 = [13 x i8] c"/dev/mmcblk0\00"
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [26 x i8] c"nvram_cfg set boot_flag=3\00"
@.str.4 = [5 x i8] c"sync\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %93
19: ; preds = %2
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %93
28: ; preds = %19
store i32 5242880, i32* %9
%29 = load i32, i32* %7
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %29, i32 3145728, i32 %30)
br label %32
32: ; preds = %51, %28
%33 = load i32, i32* %9
%34 = icmp ugt i32 %33, 0
br i1 %34, label %35, label %54
35: ; preds = %32
%36 = load i32, i32* %6
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 4096)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp ne i32 %39, 4096
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @system(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %88
43: ; preds = %35
%44 = load i32, i32* %7
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%46 = call i32 @write(i32 %44, i8* %45, i32 4096)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp ne i32 %47, 4096
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @system(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %88
51: ; preds = %43
%52 = load i32, i32* %9
%53 = sub i32 %52, 4096
store i32 %53, i32* %9
br label %32
54: ; preds = %32
store i32 50331648, i32* %12
%55 = load i32, i32* %7
%56 = load i32, i32* @SEEK_SET
%57 = call i32 @lseek(i32 %55, i32 13631488, i32 %56)
br label %58
58: ; preds = %77, %54
%59 = load i32, i32* %12
%60 = icmp ugt i32 %59, 0
br i1 %60, label %61, label %80
61: ; preds = %58
%62 = load i32, i32* %6
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%64 = call i32 @read(i32 %62, i8* %63, i32 4096)
store i32 %64, i32* %11
%65 = load i32, i32* %11
%66 = icmp ne i32 %65, 4096
br i1 %66, label %67, label %69
67: ; preds = %61
%68 = call i32 @system(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %88
69: ; preds = %61
%70 = load i32, i32* %7
%71 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%72 = call i32 @write(i32 %70, i8* %71, i32 4096)
store i32 %72, i32* %10
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, 4096
br i1 %74, label %75, label %77
75: ; preds = %69
%76 = call i32 @system(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %88
77: ; preds = %69
%78 = load i32, i32* %12
%79 = sub i32 %78, 4096
store i32 %79, i32* %12
br label %58
80: ; preds = %58
%81 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%82 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%83 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = load i32, i32* %7
%87 = call i32 @close(i32 %86)
store i32 0, i32* %3
br label %93
88: ; preds = %75, %67, %49, %41
%89 = load i32, i32* %6
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %7
%92 = call i32 @close(i32 %91)
store i32 -1, i32* %3
br label %93
93: ; preds = %88, %80, %24, %17
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str = [15 x i8] c"/tmp/fwupgrade\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"open %s failed.\0A\00"
@.str.2 = [13 x i8] c"/dev/mmcblk0\00"
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [26 x i8] c"nvram_cfg set boot_flag=3\00"
@.str.4 = [5 x i8] c"sync\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %53
10: ; preds = %2
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @close(i32 %6)
br label %53
17: ; preds = %10
%18 = load i32, i32* @SEEK_SET
%19 = tail call i32 @lseek(i32 %12, i32 3145728, i32 %18)
br label %22
20: ; preds = %26
%21 = icmp eq i32 %29, 0
br i1 %21, label %30, label %22
22: ; preds = %17, %20
%23 = phi i32 [ 5242880, %17 ], [ %29, %20 ]
%24 = call i32 @read(i32 %6, i8* %4, i32 4096)
%25 = icmp eq i32 %24, 4096
br i1 %25, label %26, label %49
26: ; preds = %22
%27 = call i32 @write(i32 %12, i8* %4, i32 4096)
%28 = icmp eq i32 %27, 4096
%29 = add nsw i32 %23, -4096
br i1 %28, label %20, label %49
30: ; preds = %20
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @lseek(i32 %12, i32 13631488, i32 %31)
br label %35
33: ; preds = %39
%34 = icmp eq i32 %42, 0
br i1 %34, label %43, label %35
35: ; preds = %30, %33
%36 = phi i32 [ 50331648, %30 ], [ %42, %33 ]
%37 = call i32 @read(i32 %6, i8* %4, i32 4096)
%38 = icmp eq i32 %37, 4096
br i1 %38, label %39, label %49
39: ; preds = %35
%40 = call i32 @write(i32 %12, i8* %4, i32 4096)
%41 = icmp eq i32 %40, 4096
%42 = add nsw i32 %36, -4096
br i1 %41, label %33, label %49
43: ; preds = %33
%44 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%45 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%46 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%47 = tail call i32 @close(i32 %6)
%48 = tail call i32 @close(i32 %12)
br label %53
49: ; preds = %26, %22, %39, %35
%50 = tail call i32 @system(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%51 = tail call i32 @close(i32 %6)
%52 = tail call i32 @close(i32 %12)
br label %53
53: ; preds = %43, %49, %14, %8
%54 = phi i32 [ -1, %8 ], [ -1, %14 ], [ -1, %49 ], [ 0, %43 ]
ret i32 %54
}