#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*/ do_console_mode(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open_serial_port(char const *);
int /*<<< orphan*/ ping_target(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_binary_file(char const *, unsigned char **,
                                    unsigned int *);
int /*<<< orphan*/ read_hex_file(char const *, unsigned char **,
                                 unsigned int *);
int /*<<< orphan*/ send_execute_command(int);
int /*<<< orphan*/ send_file(int, int, unsigned char *, unsigned int);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  unsigned char *program_data;
  unsigned int program_length;
  unsigned char *ramdisk_data = NULL;
  unsigned int ramdisk_length = 0;
  int serial_fd;
  if (argc < 3) {
    fprintf(stderr, "USAGE:\n    serial_boot <serial port name> <hex file> "
                    "[<ramdisk image>]\n");
    return 1;
  }
  if (!read_hex_file(argv[2], &program_data, &program_length))
    return 1;
  if (argc == 4) {
    if (!read_binary_file(argv[3], &ramdisk_data, &ramdisk_length))
      return 1;
  }
  serial_fd = open_serial_port(argv[1]);
  if (serial_fd < 0)
    return 1;
  if (!ping_target(serial_fd))
    return 1;
  printf("Program is %d bytes\n", program_length);
  if (!send_file(serial_fd, 0, program_data, program_length))
    return 1;
  if (ramdisk_data) {
    printf("\nRamdisk is %d bytes\n", ramdisk_length);
    if (!send_file(serial_fd, 0x4000000, ramdisk_data, ramdisk_length))
      return 1;
  }
  if (!send_execute_command(serial_fd))
    return 1;
  printf("\nProgram running, entering console mode\n");
  do_console_mode(serial_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11e0 <main+0x50>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <read_hex_file@plt>
cmp    $0x0,%eax
jne    120a <main+0x7a>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
cmpl   $0x4,-0x8(%rbp)
jne    1243 <main+0xb3>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <read_binary_file@plt>
cmp    $0x0,%eax
jne    123e <main+0xae>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
jmp    1243 <main+0xb3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <open_serial_port@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1269 <main+0xd9>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
mov    -0x30(%rbp),%edi
call   1030 <ping_target@plt>
cmp    $0x0,%eax
jne    1286 <main+0xf6>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
mov    -0x1c(%rbp),%esi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
xor    %esi,%esi
call   10a0 <send_file@plt>
cmp    $0x0,%eax
jne    12bd <main+0x12d>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
cmpq   $0x0,-0x28(%rbp)
je     1307 <main+0x177>
mov    -0x2c(%rbp),%esi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x4000000,%esi
call   10a0 <send_file@plt>
cmp    $0x0,%eax
jne    1302 <main+0x172>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
jmp    1307 <main+0x177>
mov    -0x30(%rbp),%edi
call   1040 <send_execute_command@plt>
cmp    $0x0,%eax
jne    1324 <main+0x194>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1b1>
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%edi
call   1090 <do_console_mode@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x0,0x8(%rsp)
cmp    $0x2,%edi
jg     11de <main+0x3e>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%r14d
jmp    12be <main+0x11e>
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x10(%rsi),%rdi
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1090 <read_hex_file@plt>
mov    $0x1,%r14d
test   %eax,%eax
je     12be <main+0x11e>
cmp    $0x4,%ebx
jne    1224 <main+0x84>
mov    0x18(%rbp),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1060 <read_binary_file@plt>
test   %eax,%eax
je     12be <main+0x11e>
mov    0x8(%rbp),%rdi
call   10c0 <open_serial_port@plt>
test   %eax,%eax
js     12be <main+0x11e>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <ping_target@plt>
test   %eax,%eax
je     12be <main+0x11e>
mov    0xc(%rsp),%esi
lea    0xdfb(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x18(%rsp),%rdx
mov    0xc(%rsp),%ecx
mov    %ebp,%edi
xor    %esi,%esi
call   10b0 <send_file@plt>
test   %eax,%eax
je     12be <main+0x11e>
cmpq   $0x0,0x10(%rsp)
je     129d <main+0xfd>
mov    0x8(%rsp),%esi
lea    0xde0(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%ecx
mov    %ebp,%edi
mov    $0x4000000,%esi
call   10b0 <send_file@plt>
test   %eax,%eax
je     12be <main+0x11e>
mov    %ebp,%edi
call   1050 <send_execute_command@plt>
test   %eax,%eax
je     12be <main+0x11e>
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   10a0 <do_console_mode@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [72 x i8] c"USAGE:\0A serial_boot <serial port name> <hex file> [<ramdisk image>]\0A\00"
@.str.1 = [21 x i8] c"Program is %d bytes\0A\00"
@.str.2 = [22 x i8] c"\0ARamdisk is %d bytes\0A\00"
@.str.3 = [41 x i8] c"\0AProgram running, entering console mode\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %8
store i32 0, i32* %9
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %78
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i32 @read_hex_file(i8* %19, i8** %6, i32* %7)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %16
store i32 1, i32* %3
br label %78
23: ; preds = %16
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 4
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 3
%29 = load i8*, i8** %28
%30 = call i32 @read_binary_file(i8* %29, i8** %8, i32* %9)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %26
store i32 1, i32* %3
br label %78
33: ; preds = %26
br label %34
34: ; preds = %33, %23
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @open_serial_port(i8* %37)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %34
store i32 1, i32* %3
br label %78
42: ; preds = %34
%43 = load i32, i32* %10
%44 = call i32 @ping_target(i32 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %42
store i32 1, i32* %3
br label %78
47: ; preds = %42
%48 = load i32, i32* %7
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %10
%51 = load i8*, i8** %6
%52 = load i32, i32* %7
%53 = call i32 @send_file(i32 %50, i32 0, i8* %51, i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %47
store i32 1, i32* %3
br label %78
56: ; preds = %47
%57 = load i8*, i8** %8
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %69
59: ; preds = %56
%60 = load i32, i32* %9
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %10
%63 = load i8*, i8** %8
%64 = load i32, i32* %9
%65 = call i32 @send_file(i32 %62, i32 67108864, i8* %63, i32 %64)
%66 = icmp ne i32 %65, 0
br i1 %66, label %68, label %67
67: ; preds = %59
store i32 1, i32* %3
br label %78
68: ; preds = %59
br label %69
69: ; preds = %68, %56
%70 = load i32, i32* %10
%71 = call i32 @send_execute_command(i32 %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %74, label %73
73: ; preds = %69
store i32 1, i32* %3
br label %78
74: ; preds = %69
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%76 = load i32, i32* %10
%77 = call i32 @do_console_mode(i32 %76)
store i32 0, i32* %3
br label %78
78: ; preds = %74, %73, %67, %55, %46, %41, %32, %22, %13
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [72 x i8] c"USAGE:\0A serial_boot <serial port name> <hex file> [<ramdisk image>]\0A\00"
@.str.1 = [21 x i8] c"Program is %d bytes\0A\00"
@.str.2 = [22 x i8] c"\0ARamdisk is %d bytes\0A\00"
@str = [40 x i8] c"\0AProgram running, entering console mode\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = bitcast i8** %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%10 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%11 = icmp slt i32 %0, 3
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
br label %58
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = call i32 @read_hex_file(i8* %17, i8** %3, i32* %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %58, label %20
20: ; preds = %15
%21 = icmp eq i32 %0, 4
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = getelementptr inbounds i8*, i8** %1, i64 3
%24 = load i8*, i8** %23
%25 = call i32 @read_binary_file(i8* %24, i8** %5, i32* %6)
%26 = icmp eq i32 %25, 0
br i1 %26, label %58, label %27
27: ; preds = %22, %20
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @open_serial_port(i8* %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %58, label %32
32: ; preds = %27
%33 = call i32 @ping_target(i32 %30)
%34 = icmp eq i32 %33, 0
br i1 %34, label %58, label %35
35: ; preds = %32
%36 = load i32, i32* %4
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %3
%39 = load i32, i32* %4
%40 = call i32 @send_file(i32 %30, i32 0, i8* %38, i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %58, label %42
42: ; preds = %35
%43 = load i8*, i8** %5
%44 = icmp eq i8* %43, null
br i1 %44, label %52, label %45
45: ; preds = %42
%46 = load i32, i32* %6
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %46)
%48 = load i8*, i8** %5
%49 = load i32, i32* %6
%50 = call i32 @send_file(i32 %30, i32 67108864, i8* %48, i32 %49)
%51 = icmp eq i32 %50, 0
br i1 %51, label %58, label %52
52: ; preds = %45, %42
%53 = call i32 @send_execute_command(i32 %30)
%54 = icmp eq i32 %53, 0
br i1 %54, label %58, label %55
55: ; preds = %52
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%57 = call i32 @do_console_mode(i32 %30)
br label %58
58: ; preds = %52, %45, %35, %32, %27, %22, %15, %55, %12
%59 = phi i32 [ 1, %12 ], [ 0, %55 ], [ 1, %15 ], [ 1, %22 ], [ 1, %27 ], [ 1, %32 ], [ 1, %35 ], [ 1, %45 ], [ 1, %52 ]
ret i32 %59
}