#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ adiv5_init();
int atol(char *);
char *blob(char *, size_t *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ kinetis_continue();
int /*<<< orphan*/ kinetis_disable_debug();
int /*<<< orphan*/ kinetis_halt_core();
int /*<<< orphan*/ kinetis_init();
int /*<<< orphan*/ kinetis_program(int, char *, size_t);
int /*<<< orphan*/ kinetis_reset_system();
int /*<<< orphan*/ kinetisbase_init();
int /*<<< orphan*/ kinetisbase_mass_erase();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcmp(char *, char *);
int main(int argc, char **argv) {
  printf("Attaching debugger... ");
  adiv5_init();
  if (argc > 1 && !strcmp(argv[1], "--mass-erase")) {
    kinetisbase_init();
    printf("done.\nMass erasing chip... ");
    kinetisbase_mass_erase();
    printf("done.\n");
  } else {
    kinetis_init();
    printf("done.\n");
    if (argc > 3) {
      size_t total;
      char *firmware = blob(argv[2], &total);
      uint32_t address = atol(argv[3]);
      printf("Programming %ul bytes of firmware to address 0x%02x...\n", total,
             address);
      kinetis_halt_core();
      kinetis_program(address, firmware, total);
      printf("done.\n");
      free(firmware);
    }
  }
  printf("Resetting... ");
  kinetis_reset_system();
  kinetis_disable_debug();
  kinetis_continue();
  printf("done.\n");
}


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)
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   1040 <adiv5_init@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1261 <main+0x81>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf3(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1261 <main+0x81>
mov    $0x0,%al
call   1070 <kinetisbase_init@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   10b0 <kinetisbase_mass_erase@plt>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    12ed <main+0x10d>
mov    $0x0,%al
call   10c0 <kinetis_init@plt>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
cmpl   $0x3,-0x8(%rbp)
jle    12e8 <main+0x108>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <blob@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1100 <atol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   1090 <kinetis_halt_core@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   10d0 <kinetis_program@plt>
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12ed <main+0x10d>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   1060 <kinetis_reset_system@plt>
mov    $0x0,%al
call   10f0 <kinetis_disable_debug@plt>
mov    $0x0,%al
call   10e0 <kinetis_continue@plt>
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   1040 <adiv5_init@plt>
cmp    $0x2,%ebp
jl     1226 <main+0x36>
mov    0x8(%rbx),%rdi
lea    0xdfa(%rip),%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     129b <main+0xab>
xor    %eax,%eax
call   10d0 <kinetis_init@plt>
lea    0xe52(%rip),%rdi
call   1060 <puts@plt>
cmp    $0x4,%ebp
jl     12c3 <main+0xd3>
mov    0x10(%rbx),%rdi
mov    %rsp,%rsi
call   1050 <blob@plt>
mov    %rax,%rbp
mov    0x18(%rbx),%rdi
call   1110 <atol@plt>
mov    %eax,%ebx
mov    (%rsp),%rsi
lea    0xdd9(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   10a0 <kinetis_halt_core@plt>
mov    (%rsp),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   10e0 <kinetis_program@plt>
lea    0xdfa(%rip),%rdi
call   1060 <puts@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    12c3 <main+0xd3>
xor    %eax,%eax
call   1080 <kinetisbase_init@plt>
lea    0xd7b(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   10c0 <kinetisbase_mass_erase@plt>
lea    0xdc8(%rip),%rdi
call   1060 <puts@plt>
lea    0xdae(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   1070 <kinetis_reset_system@plt>
xor    %eax,%eax
call   1100 <kinetis_disable_debug@plt>
xor    %eax,%eax
call   10f0 <kinetis_continue@plt>
lea    0xd99(%rip),%rdi
call   1060 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Attaching debugger... \00"
@.str.1 = [13 x i8] c"--mass-erase\00"
@.str.2 = [28 x i8] c"done.\0AMass erasing chip... \00"
@.str.3 = [7 x i8] c"done.\0A\00"
@.str.4 = [56 x i8] c"Programming %ul bytes of firmware to address 0x%02x...\0A\00"
@.str.5 = [14 x i8] c"Resetting... \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @adiv5_init()
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %24
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strcmp(i8* %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %13
%20 = call i32 (...) @kinetisbase_init()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 (...) @kinetisbase_mass_erase()
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %50
24: ; preds = %13, %2
%25 = call i32 (...) @kinetis_init()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 3
br i1 %28, label %29, label %49
29: ; preds = %24
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i8* @blob(i8* %32, i64* %6)
store i8* %33, i8** %7
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 3
%36 = load i8*, i8** %35
%37 = call i32 @atol(i8* %36)
store i32 %37, i32* %8
%38 = load i64, i64* %6
%39 = load i32, i32* %8
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i64 %38, i32 %39)
%41 = call i32 (...) @kinetis_halt_core()
%42 = load i32, i32* %8
%43 = load i8*, i8** %7
%44 = load i64, i64* %6
%45 = call i32 @kinetis_program(i32 %42, i8* %43, i64 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%47 = load i8*, i8** %7
%48 = call i32 @free(i8* %47)
br label %49
49: ; preds = %29, %24
br label %50
50: ; preds = %49, %19
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%52 = call i32 (...) @kinetis_reset_system()
%53 = call i32 (...) @kinetis_disable_debug()
%54 = call i32 (...) @kinetis_continue()
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [23 x i8] c"Attaching debugger... \00"
@.str.1 = [13 x i8] c"--mass-erase\00"
@.str.2 = [28 x i8] c"done.\0AMass erasing chip... \00"
@.str.4 = [56 x i8] c"Programming %ul bytes of firmware to address 0x%02x...\0A\00"
@.str.5 = [14 x i8] c"Resetting... \00"
@str.8 = [6 x i8] c"done.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (...) @adiv5_init()
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strcmp(i8* dereferenceable(1) %9, i8* dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = tail call i32 (...) @kinetisbase_init()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 (...) @kinetisbase_mass_erase()
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
br label %36
17: ; preds = %7, %2
%18 = tail call i32 (...) @kinetis_init()
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
%20 = icmp sgt i32 %0, 3
br i1 %20, label %21, label %36
21: ; preds = %17
%22 = bitcast i64* %3 to i8*
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = call i8* @blob(i8* %24, i64* %3)
%26 = getelementptr inbounds i8*, i8** %1, i64 3
%27 = load i8*, i8** %26
%28 = call i32 @atol(i8* %27)
%29 = load i64, i64* %3
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i64 %29, i32 %28)
%31 = call i32 (...) @kinetis_halt_core()
%32 = load i64, i64* %3
%33 = call i32 @kinetis_program(i32 %28, i8* %25, i64 %32)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
%35 = call i32 @free(i8* %25)
br label %36
36: ; preds = %17, %21, %12
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 (...) @kinetis_reset_system()
%39 = call i32 (...) @kinetis_disable_debug()
%40 = call i32 (...) @kinetis_continue()
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}