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

/* Variables and functions */
int /*<<< orphan*/ errlog(char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ goodlog(char *, ...);
int is_switch_ok(int);
int /*<<< orphan*/ *load_check_firmware(char *, int *, int);
int send_upgrade_cmd(int);
int write_firmware(int, int, int /*<<< orphan*/ *, int, int, int);
int upgrade_firmware(int fd, char *path, int start, int range_len,
                     int page_size, int sector_size) {
  int ret = 0;
  uint8_t *buf = NULL;
  int file_size = 0;
  buf = load_check_firmware(path, &file_size, range_len);
  if (buf == NULL) {
    errlog("\n>>> load firmware failed\n\n");
    return -1;
  }
  goodlog("\n>>> 1. load firmware [%s] success.\n\n", path);
  ret = send_upgrade_cmd(fd);
  if (ret) {
    errlog("\n>>> send_upgrade_cmd failed\n\n");
    ret = -2;
    goto L_END;
  }
  goodlog("\n>>> 2. send upgrade command to bridge success.\n\n");
  ret = is_switch_ok(fd);
  if (ret) {
    errlog("\n>>> switch check failed.\n\n");
    ret = -3;
    goto L_END;
  }
  goodlog("\n>>> 3. switch check success\n\n");
  ret = write_firmware(fd, start, buf, file_size, sector_size, page_size);
  if (ret) {
    errlog("\n>>> write_firmware failed.\n\n");
    ret = -4;
    goto L_END;
  }
  goodlog("\n>>> 4. write firmware to flash success.\n\n");
L_END:
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<upgrade_firmware>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    -0x34(%rbp),%rsi
call   1050 <load_check_firmware@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11da <upgrade_firmware+0x6a>
lea    0xe37(%rip),%rdi
call   1040 <errlog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <upgrade_firmware+0x14e>
mov    -0x10(%rbp),%rsi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1080 <goodlog@plt>
mov    -0x8(%rbp),%edi
call   1060 <send_upgrade_cmd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1219 <upgrade_firmware+0xa9>
lea    0xe3a(%rip),%rdi
call   1040 <errlog@plt>
movl   $0xfffffffe,-0x24(%rbp)
jmp    12af <upgrade_firmware+0x13f>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1080 <goodlog@plt>
mov    -0x8(%rbp),%edi
call   1070 <is_switch_ok@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1254 <upgrade_firmware+0xe4>
lea    0xe50(%rip),%rdi
call   1040 <errlog@plt>
movl   $0xfffffffd,-0x24(%rbp)
jmp    12af <upgrade_firmware+0x13f>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1080 <goodlog@plt>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
call   1090 <write_firmware@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a1 <upgrade_firmware+0x131>
lea    0xe3e(%rip),%rdi
call   1040 <errlog@plt>
movl   $0xfffffffc,-0x24(%rbp)
jmp    12af <upgrade_firmware+0x13f>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1080 <goodlog@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upgrade_firmware>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8d,%r15d
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
mov    %ecx,%edx
call   1050 <load_check_firmware@plt>
test   %rax,%rax
je     11da <upgrade_firmware+0x6a>
mov    %rax,%r13
lea    0xe6d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <goodlog@plt>
mov    %ebp,%edi
call   1060 <send_upgrade_cmd@plt>
test   %eax,%eax
je     11f0 <upgrade_firmware+0x80>
lea    0xe77(%rip),%rdi
call   1040 <errlog@plt>
mov    $0xfffffffe,%ebx
jmp    1268 <upgrade_firmware+0xf8>
lea    0xe1f(%rip),%rdi
call   1040 <errlog@plt>
mov    $0xffffffff,%ebx
jmp    1270 <upgrade_firmware+0x100>
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
call   1080 <goodlog@plt>
mov    %ebp,%edi
call   1070 <is_switch_ok@plt>
test   %eax,%eax
je     121c <upgrade_firmware+0xac>
lea    0xe83(%rip),%rdi
call   1040 <errlog@plt>
mov    $0xfffffffd,%ebx
jmp    1268 <upgrade_firmware+0xf8>
lea    0xe8c(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <goodlog@plt>
mov    0x4(%rsp),%ecx
mov    %ebp,%edi
mov    %r12d,%esi
mov    %r13,%rdx
mov    %r14d,%r8d
mov    %r15d,%r9d
call   1090 <write_firmware@plt>
test   %eax,%eax
je     125a <upgrade_firmware+0xea>
lea    0xe80(%rip),%rdi
call   1040 <errlog@plt>
mov    $0xfffffffc,%ebx
jmp    1268 <upgrade_firmware+0xf8>
lea    0xe8b(%rip),%rdi
xor    %eax,%eax
call   1080 <goodlog@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"\0A>>> load firmware failed\0A\0A\00"
@.str.1 = [38 x i8] c"\0A>>> 1. load firmware [%s] success.\0A\0A\00"
@.str.2 = [31 x i8] c"\0A>>> send_upgrade_cmd failed\0A\0A\00"
@.str.3 = [50 x i8] c"\0A>>> 2. send upgrade command to bridge success.\0A\0A\00"
@.str.4 = [28 x i8] c"\0A>>> switch check failed.\0A\0A\00"
@.str.5 = [31 x i8] c"\0A>>> 3. switch check success\0A\0A\00"
@.str.6 = [30 x i8] c"\0A>>> write_firmware failed.\0A\0A\00"
@.str.7 = [43 x i8] c"\0A>>> 4. write firmware to flash success.\0A\0A\00"
define i32 @upgrade_firmware(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
%16 = alloca i32
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %14
store i32* null, i32** %15
store i32 0, i32* %16
%17 = load i8*, i8** %9
%18 = load i32, i32* %11
%19 = call i32* @load_check_firmware(i8* %17, i32* %16, i32 %18)
store i32* %19, i32** %15
%20 = load i32*, i32** %15
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %6
%23 = call i32 @errlog(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %7
br label %60
24: ; preds = %6
%25 = load i8*, i8** %9
%26 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %8
%28 = call i32 @send_upgrade_cmd(i32 %27)
store i32 %28, i32* %14
%29 = load i32, i32* %14
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @errlog(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %14
br label %56
33: ; preds = %24
%34 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* %8
%36 = call i32 @is_switch_ok(i32 %35)
store i32 %36, i32* %14
%37 = load i32, i32* %14
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @errlog(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 -3, i32* %14
br label %56
41: ; preds = %33
%42 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%43 = load i32, i32* %8
%44 = load i32, i32* %10
%45 = load i32*, i32** %15
%46 = load i32, i32* %16
%47 = load i32, i32* %13
%48 = load i32, i32* %12
%49 = call i32 @write_firmware(i32 %43, i32 %44, i32* %45, i32 %46, i32 %47, i32 %48)
store i32 %49, i32* %14
%50 = load i32, i32* %14
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %41
%53 = call i32 @errlog(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
store i32 -4, i32* %14
br label %56
54: ; preds = %41
%55 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0))
br label %56
56: ; preds = %54, %52, %39, %31
%57 = load i32*, i32** %15
%58 = call i32 @free(i32* %57)
%59 = load i32, i32* %14
store i32 %59, i32* %7
br label %60
60: ; preds = %56, %22
%61 = load i32, i32* %7
ret i32 %61
}

LLVM IR (-O2):
@.str = [28 x i8] c"\0A>>> load firmware failed\0A\0A\00"
@.str.1 = [38 x i8] c"\0A>>> 1. load firmware [%s] success.\0A\0A\00"
@.str.2 = [31 x i8] c"\0A>>> send_upgrade_cmd failed\0A\0A\00"
@.str.3 = [50 x i8] c"\0A>>> 2. send upgrade command to bridge success.\0A\0A\00"
@.str.4 = [28 x i8] c"\0A>>> switch check failed.\0A\0A\00"
@.str.5 = [31 x i8] c"\0A>>> 3. switch check success\0A\0A\00"
@.str.6 = [30 x i8] c"\0A>>> write_firmware failed.\0A\0A\00"
@.str.7 = [43 x i8] c"\0A>>> 4. write firmware to flash success.\0A\0A\00"
define i32 @upgrade_firmware(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%9 = call i32* @load_check_firmware(i8* %1, i32* %7, i32 %3)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i32 @errlog(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %37
13: ; preds = %6
%14 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%15 = call i32 @send_upgrade_cmd(i32 %0)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = call i32 @errlog(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %34
19: ; preds = %13
%20 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @is_switch_ok(i32 %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = call i32 @errlog(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %34
25: ; preds = %19
%26 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%27 = load i32, i32* %7
%28 = call i32 @write_firmware(i32 %0, i32 %2, i32* %9, i32 %27, i32 %5, i32 %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = call i32 @errlog(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %34
32: ; preds = %25
%33 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0))
br label %34
34: ; preds = %32, %30, %23, %17
%35 = phi i32 [ -2, %17 ], [ -3, %23 ], [ -4, %30 ], [ 0, %32 ]
%36 = call i32 @free(i32* %9)
br label %37
37: ; preds = %34, %11
%38 = phi i32 [ -1, %11 ], [ %35, %34 ]
ret i32 %38
}