#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 write_firmware(int, int, int /*<<< orphan*/ *, int, int, int);
int upgrade_firmware_flash_only(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 = 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_flash_only>:
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    11ca <upgrade_firmware_flash_only+0x6a>
lea    0xe47(%rip),%rdi
call   1040 <errlog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <upgrade_firmware_flash_only+0x113>
mov    -0x10(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1070 <goodlog@plt>
mov    -0x8(%rbp),%edi
call   1060 <is_switch_ok@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1209 <upgrade_firmware_flash_only+0xa9>
lea    0xe4a(%rip),%rdi
call   1040 <errlog@plt>
movl   $0xfffffffd,-0x24(%rbp)
jmp    1264 <upgrade_firmware_flash_only+0x104>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1070 <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   1080 <write_firmware@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1256 <upgrade_firmware_flash_only+0xf6>
lea    0xe38(%rip),%rdi
call   1040 <errlog@plt>
movl   $0xfffffffc,-0x24(%rbp)
jmp    1264 <upgrade_firmware_flash_only+0x104>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1070 <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_flash_only>:
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,%rbp
mov    %edi,%r13d
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbp,%rdi
mov    %ecx,%edx
call   1050 <load_check_firmware@plt>
test   %rax,%rax
je     11c9 <upgrade_firmware_flash_only+0x69>
mov    %rax,%rbx
lea    0xe7c(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <goodlog@plt>
mov    %r13d,%edi
call   1060 <is_switch_ok@plt>
test   %eax,%eax
je     11dc <upgrade_firmware_flash_only+0x7c>
lea    0xe85(%rip),%rdi
call   1040 <errlog@plt>
mov    $0xfffffffd,%ebp
jmp    1229 <upgrade_firmware_flash_only+0xc9>
lea    0xe30(%rip),%rdi
call   1040 <errlog@plt>
mov    $0xffffffff,%ebp
jmp    1231 <upgrade_firmware_flash_only+0xd1>
lea    0xe7b(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <goodlog@plt>
mov    0x4(%rsp),%ecx
mov    %r13d,%edi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %r14d,%r8d
mov    %r15d,%r9d
call   1080 <write_firmware@plt>
test   %eax,%eax
je     121b <upgrade_firmware_flash_only+0xbb>
lea    0xe6e(%rip),%rdi
call   1040 <errlog@plt>
mov    $0xfffffffc,%ebp
jmp    1229 <upgrade_firmware_flash_only+0xc9>
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1070 <goodlog@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%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 = [28 x i8] c"\0A>>> switch check failed.\0A\0A\00"
@.str.3 = [31 x i8] c"\0A>>> 3. switch check success\0A\0A\00"
@.str.4 = [30 x i8] c"\0A>>> write_firmware failed.\0A\0A\00"
@.str.5 = [43 x i8] c"\0A>>> 4. write firmware to flash success.\0A\0A\00"
define i32 @upgrade_firmware_flash_only(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 %52
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 @is_switch_ok(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 ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %14
br label %48
33: ; preds = %24
%34 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* %8
%36 = load i32, i32* %10
%37 = load i32*, i32** %15
%38 = load i32, i32* %16
%39 = load i32, i32* %13
%40 = load i32, i32* %12
%41 = call i32 @write_firmware(i32 %35, i32 %36, i32* %37, i32 %38, i32 %39, i32 %40)
store i32 %41, i32* %14
%42 = load i32, i32* %14
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = call i32 @errlog(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 -4, i32* %14
br label %48
46: ; preds = %33
%47 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %44, %31
%49 = load i32*, i32** %15
%50 = call i32 @free(i32* %49)
%51 = load i32, i32* %14
store i32 %51, i32* %7
br label %52
52: ; preds = %48, %22
%53 = load i32, i32* %7
ret i32 %53
}

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 = [28 x i8] c"\0A>>> switch check failed.\0A\0A\00"
@.str.3 = [31 x i8] c"\0A>>> 3. switch check success\0A\0A\00"
@.str.4 = [30 x i8] c"\0A>>> write_firmware failed.\0A\0A\00"
@.str.5 = [43 x i8] c"\0A>>> 4. write firmware to flash success.\0A\0A\00"
define i32 @upgrade_firmware_flash_only(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 %31
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 @is_switch_ok(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 ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %28
19: ; preds = %13
%20 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* %7
%22 = call i32 @write_firmware(i32 %0, i32 %2, i32* %9, i32 %21, i32 %5, i32 %4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = call i32 @errlog(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %28
26: ; preds = %19
%27 = call i32 (i8*, ...) @goodlog(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %26, %24, %17
%29 = phi i32 [ -3, %17 ], [ -4, %24 ], [ 0, %26 ]
%30 = call i32 @free(i32* %9)
br label %31
31: ; preds = %28, %11
%32 = phi i32 [ -1, %11 ], [ %29, %28 ]
ret i32 %32
}