#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 segment {
  int dummy;
};

/* 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 *);
struct segment *read_hex_file(char const *);
int /*<<< orphan*/ send_execute_command(int);
int /*<<< orphan*/ send_segment(int, int, unsigned char *, unsigned int);
int /*<<< orphan*/ send_segments(int, struct segment *);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  struct segment *program_data;
  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;
  }
  program_data = read_hex_file(argv[2]);
  if (program_data == NULL)
    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;
  if (!send_segments(serial_fd, program_data))
    return 1;
  if (ramdisk_data) {
    printf("\n_ramdisk is %u bytes\n", ramdisk_length);
    if (!send_segment(serial_fd, 0x4000000, ramdisk_data, ramdisk_length))
      return 1;
  }
  if (!send_execute_command(serial_fd))
    return 1;
  printf("\n_program 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,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f0 <main+0x50>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <read_hex_file@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1218 <main+0x78>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
cmpl   $0x4,-0x8(%rbp)
jne    1251 <main+0xb1>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1060 <read_binary_file@plt>
cmp    $0x0,%eax
jne    124c <main+0xac>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
jmp    1251 <main+0xb1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <open_serial_port@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1277 <main+0xd7>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
mov    -0x28(%rbp),%edi
call   1030 <ping_target@plt>
cmp    $0x0,%eax
jne    1294 <main+0xf4>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <send_segments@plt>
cmp    $0x0,%eax
jne    12b5 <main+0x115>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
cmpq   $0x0,-0x20(%rbp)
je     12ff <main+0x15f>
mov    -0x24(%rbp),%esi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x4000000,%esi
call   10b0 <send_segment@plt>
cmp    $0x0,%eax
jne    12fa <main+0x15a>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
jmp    12ff <main+0x15f>
mov    -0x28(%rbp),%edi
call   1040 <send_execute_command@plt>
cmp    $0x0,%eax
jne    131c <main+0x17c>
movl   $0x1,-0x4(%rbp)
jmp    1339 <main+0x199>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%edi
call   10a0 <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   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
cmp    $0x2,%edi
jg     11f2 <main+0x42>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%r15d
jmp    12b3 <main+0x103>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x10(%rsi),%rdi
call   10a0 <read_hex_file@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     12b3 <main+0x103>
mov    %rax,%r12
cmp    $0x4,%ebp
jne    1232 <main+0x82>
mov    0x18(%rbx),%rdi
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1070 <read_binary_file@plt>
test   %eax,%eax
je     12b3 <main+0x103>
mov    0x8(%rbx),%rdi
call   10d0 <open_serial_port@plt>
test   %eax,%eax
js     12b3 <main+0x103>
mov    %eax,%r14d
mov    %eax,%edi
call   1040 <ping_target@plt>
test   %eax,%eax
je     12b3 <main+0x103>
mov    %r14d,%edi
mov    %r12,%rsi
call   1060 <send_segments@plt>
test   %eax,%eax
je     12b3 <main+0x103>
cmpq   $0x0,0x8(%rsp)
je     1290 <main+0xe0>
mov    0x4(%rsp),%esi
lea    0xdd9(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x8(%rsp),%rdx
mov    0x4(%rsp),%ecx
mov    %r14d,%edi
mov    $0x4000000,%esi
call   10c0 <send_segment@plt>
test   %eax,%eax
je     12b3 <main+0x103>
mov    %r14d,%edi
call   1050 <send_execute_command@plt>
test   %eax,%eax
je     12b3 <main+0x103>
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   10b0 <do_console_mode@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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