#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int rtk_firmware_update(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int cmd_firmware_upgrade(int index, int argc, char *argv[]) {
  int len, ret, count;
  char filename[50];
  char *data;
  int fd, rc;
  struct stat ffstat;
  int flen = 0;
  sprintf(filename, "%s", argv[2]);
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("Cannot Open file %s!\n", filename);
    return -1;
  }
  fstat(fd, &ffstat);
  flen = ffstat.st_size;
  printf("flen = %d \n", flen);
  if ((data = (char *)malloc(flen)) == NULL) {
    printf("data buffer allocation failed!\n");
    return -1;
  }
  rc = read(fd, data, flen);
  if (rc != flen) {
    printf("Reading error\n");
    free(data);
    return -1;
  }
  close(fd);
  ret = rtk_firmware_update(data, flen);
  if (ret < 0)
    printf("rtk_firmware_update fail \n");
  else
    printf("rtk_firmware_update ok \n");
  free(data);
  return ret;
}


x86 assembly (clang, -O0):
<cmd_firmware_upgrade>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x7c(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe41(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jge    1201 <cmd_firmware_upgrade+0x71>
lea    -0x60(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <cmd_firmware_upgrade+0x156>
mov    -0x6c(%rbp),%edi
lea    -0x78(%rbp),%rsi
call   10b0 <fstat@plt>
mov    -0x78(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x7c(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x7c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jne    1254 <cmd_firmware_upgrade+0xc4>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <cmd_firmware_upgrade+0x156>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    -0x7c(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x70(%rbp)
mov    -0x70(%rbp),%eax
cmp    -0x7c(%rbp),%eax
je     1295 <cmd_firmware_upgrade+0x105>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <cmd_firmware_upgrade+0x156>
mov    -0x6c(%rbp),%edi
call   1060 <close@plt>
mov    -0x68(%rbp),%rdi
mov    -0x7c(%rbp),%esi
call   1040 <rtk_firmware_update@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12c9 <cmd_firmware_upgrade+0x139>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d7 <cmd_firmware_upgrade+0x147>
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_firmware_upgrade>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    0x10(%rdx),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strcpy@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1245 <cmd_firmware_upgrade+0xa5>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
mov    0x8(%rsp),%ebx
lea    0xe2f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     125a <cmd_firmware_upgrade+0xba>
mov    %rax,%r14
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1090 <read@plt>
cmp    %ebx,%eax
jne    1268 <cmd_firmware_upgrade+0xc8>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <rtk_firmware_update@plt>
mov    %eax,%ebx
test   %eax,%eax
lea    0xe0f(%rip),%rax
lea    0xdf0(%rip),%rdi
cmovs  %rax,%rdi
call   1060 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1281 <cmd_firmware_upgrade+0xe1>
lea    0xdb4(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    127c <cmd_firmware_upgrade+0xdc>
lea    0xe01(%rip),%rdi
call   1060 <puts@plt>
jmp    127c <cmd_firmware_upgrade+0xdc>
lea    0xde5(%rip),%rdi
call   1060 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Cannot Open file %s!\0A\00"
@.str.2 = [12 x i8] c"flen = %d \0A\00"
@.str.3 = [32 x i8] c"data buffer allocation failed!\0A\00"
@.str.4 = [15 x i8] c"Reading error\0A\00"
@.str.5 = [27 x i8] c"rtk_firmware_update fail \0A\00"
@.str.6 = [25 x i8] c"rtk_firmware_update ok \0A\00"
define i32 @cmd_firmware_upgrade(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [50 x i8]
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.stat
%16 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32 0, i32* %16
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%18 = load i8**, i8*** %7
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %3
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 -1, i32* %4
br label %71
30: ; preds = %3
%31 = load i32, i32* %13
%32 = call i32 @fstat(i32 %31, %struct.stat* %15)
%33 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%34 = load i32, i32* %33
store i32 %34, i32* %16
%35 = load i32, i32* %16
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %16
%38 = call i64 @malloc(i32 %37)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %12
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %30
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %71
43: ; preds = %30
%44 = load i32, i32* %13
%45 = load i8*, i8** %12
%46 = load i32, i32* %16
%47 = call i32 @read(i32 %44, i8* %45, i32 %46)
store i32 %47, i32* %14
%48 = load i32, i32* %14
%49 = load i32, i32* %16
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %55
51: ; preds = %43
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%53 = load i8*, i8** %12
%54 = call i32 @free(i8* %53)
store i32 -1, i32* %4
br label %71
55: ; preds = %43
%56 = load i32, i32* %13
%57 = call i32 @close(i32 %56)
%58 = load i8*, i8** %12
%59 = load i32, i32* %16
%60 = call i32 @rtk_firmware_update(i8* %58, i32 %59)
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %55
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %67
65: ; preds = %55
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %67
67: ; preds = %65, %63
%68 = load i8*, i8** %12
%69 = call i32 @free(i8* %68)
%70 = load i32, i32* %9
store i32 %70, i32* %4
br label %71
71: ; preds = %67, %51, %41, %27
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Cannot Open file %s!\0A\00"
@.str.2 = [12 x i8] c"flen = %d \0A\00"
@str = [24 x i8] c"rtk_firmware_update ok \00"
@str.7 = [26 x i8] c"rtk_firmware_update fail \00"
@str.8 = [14 x i8] c"Reading error\00"
@str.9 = [31 x i8] c"data buffer allocation failed!\00"
define i32 @cmd_firmware_upgrade(i32 %0, i32 %1, i8** nocapture %2) {
%4 = alloca [50 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = getelementptr inbounds i8*, i8** %2, i64 2
%9 = load i8*, i8** %8
%10 = call i8* @strcpy(i8* %6, i8* dereferenceable(1) %9)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %6, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %39
16: ; preds = %3
%17 = call i32 @fstat(i32 %12, %struct.stat* %5)
%18 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i64 @malloc(i32 %19)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.9, i64 0, i64 0))
br label %39
26: ; preds = %16
%27 = call i32 @read(i32 %12, i8* %22, i32 %19)
%28 = icmp eq i32 %27, %19
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%31 = call i32 @free(i8* %22)
br label %39
32: ; preds = %26
%33 = call i32 @close(i32 %12)
%34 = call i32 @rtk_firmware_update(i8* %22, i32 %19)
%35 = icmp slt i32 %34, 0
%36 = select i1 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0)
%37 = call i32 @puts(i8* dereferenceable(1) %36)
%38 = call i32 @free(i8* %22)
br label %39
39: ; preds = %32, %29, %24, %14
%40 = phi i32 [ -1, %14 ], [ -1, %24 ], [ -1, %29 ], [ %34, %32 ]
ret i32 %40
}