#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 EOF;
scalar_t__ download_file(char *, unsigned int);
int getopt(int, char **, char *);
int /*<<< orphan*/ optarg;
int optind;
int /*<<< orphan*/ printf(char *, ...);
unsigned int strtol(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  unsigned load_addr = 0x57e00000;
  char *path = NULL;
  int c;
  while ((c = getopt(argc, argv, "a:h")) != EOF)
    switch (c) {
    case 'a':
      load_addr = strtol(optarg, NULL, 16);
      continue;
    case '?':
    case 'h':
    default:
    usage:
      printf("Usage: dwn [-a load_addr] <filename>\n");
      printf("Default load address: 0x57e00000\n");
      return 1;
    }
  if (optind < argc)
    path = argv[optind];
  else
    goto usage;
  printf("load address: 0x%08X\n", load_addr);
  if (download_file(path, load_addr) != 0) {
    return -1;
  }
  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)
movl   $0x57e00000,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe8d(%rip),%rdx
call   1060 <getopt@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%eax
je     1211 <main+0xd1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x3f,%eax
je     11df <main+0x9f>
jmp    119e <main+0x5e>
mov    -0x28(%rbp),%eax
sub    $0x61,%eax
je     11c0 <main+0x80>
jmp    11af <main+0x6f>
mov    -0x28(%rbp),%eax
sub    $0x68,%eax
je     11df <main+0x9f>
jmp    11e4 <main+0xa4>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x14(%rbp)
jmp    1165 <main+0x25>
jmp    11e4 <main+0xa4>
jmp    11e9 <main+0xa9>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127d <main+0x13d>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jge    123e <main+0xfe>
mov    -0x10(%rbp),%rax
mov    0x2daa(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
jmp    1243 <main+0x103>
jmp    11e9 <main+0xa9>
mov    -0x14(%rbp),%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <download_file@plt>
cmp    $0x0,%rax
je     1276 <main+0x136>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <main+0x13d>
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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe99(%rip),%rdx
call   1070 <getopt@plt>
mov    0x2e6d(%rip),%r12
mov    $0x57e00000,%ebx
cmp    (%r12),%eax
je     11ba <main+0x6a>
mov    0x2e73(%rip),%r13
lea    0xe74(%rip),%r15
nopl   0x0(%rax)
cmp    $0x61,%eax
jne    11ed <main+0x9d>
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <getopt@plt>
cmp    (%r12),%eax
jne    1190 <main+0x40>
mov    0x2e17(%rip),%rax
movslq (%rax),%rax
cmp    %ebp,%eax
jge    11ed <main+0x9d>
mov    (%r14,%rax,8),%rbp
lea    0xe31(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1040 <download_file@plt>
neg    %rax
sbb    %eax,%eax
jmp    120a <main+0xba>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"a:h\00"
@EOF = global i32 0
@optarg = global i32 0
@.str.1 = [38 x i8] c"Usage: dwn [-a load_addr] <filename>\0A\00"
@.str.2 = [34 x i8] c"Default load address: 0x57e00000\0A\00"
@optind = global i32 0
@.str.3 = [22 x i8] c"load address: 0x%08X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1474297856, i32* %6
store i8* null, i8** %7
br label %9
9: ; preds = %17, %2
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = call i32 @getopt(i32 %10, i8** %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %8
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %9
%16 = load i32, i32* %8
switch i32 %16, label %21 [
i32 97, label %17
i32 63, label %20
i32 104, label %20
]
17: ; preds = %15
%18 = load i32, i32* @optarg
%19 = call i32 @strtol(i32 %18, i32* null, i32 16)
store i32 %19, i32* %6
br label %9
20: ; preds = %15, %15
br label %21
21: ; preds = %15, %20
br label %22
22: ; preds = %35, %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %45
25: ; preds = %9
%26 = load i32, i32* @optind
%27 = load i32, i32* %4
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i8**, i8*** %5
%31 = load i32, i32* @optind
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8*, i8** %30, i64 %32
%34 = load i8*, i8** %33
store i8* %34, i8** %7
br label %36
35: ; preds = %25
br label %22
36: ; preds = %29
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = load i8*, i8** %7
%40 = load i32, i32* %6
%41 = call i64 @download_file(i8* %39, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %36
store i32 -1, i32* %3
br label %45
44: ; preds = %36
store i32 0, i32* %3
br label %45
45: ; preds = %44, %43, %22
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [4 x i8] c"a:h\00"
@EOF = global i32 0
@optarg = global i32 0
@optind = global i32 0
@.str.3 = [22 x i8] c"load address: 0x%08X\0A\00"
@str = [37 x i8] c"Usage: dwn [-a load_addr] <filename>\00"
@str.4 = [33 x i8] c"Default load address: 0x57e00000\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @EOF
%5 = icmp eq i32 %3, %4
br i1 %5, label %18, label %6
6: ; preds = %2, %9
%7 = phi i32 [ %12, %9 ], [ %3, %2 ]
%8 = icmp eq i32 %7, 97
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load i32, i32* @optarg
%11 = tail call i32 @strtol(i32 %10, i32* null, i32 16)
%12 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %18, label %6
15: ; preds = %6, %18
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.4, i64 0, i64 0))
br label %30
18: ; preds = %9, %2
%19 = phi i32 [ 1474297856, %2 ], [ %11, %9 ]
%20 = load i32, i32* @optind
%21 = icmp slt i32 %20, %0
br i1 %21, label %22, label %15
22: ; preds = %18
%23 = sext i32 %20 to i64
%24 = getelementptr inbounds i8*, i8** %1, i64 %23
%25 = load i8*, i8** %24
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%27 = tail call i64 @download_file(i8* %25, i32 %19)
%28 = icmp ne i64 %27, 0
%29 = sext i1 %28 to i32
br label %30
30: ; preds = %22, %15
%31 = phi i32 [ 1, %15 ], [ %29, %22 ]
ret i32 %31
}