#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getopt(int, char **, char *);
int open_tty(char *);
int /*<<< orphan*/ optarg;
scalar_t__ optind;
char *strdup(int /*<<< orphan*/);
int /*<<< orphan*/ transfer(int, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  char *dev = "/dev/ttyU0";
  int c, fd;
  FILE *dump;
  while ((c = getopt(argc, argv, "f:")) != -1)
    switch (c) {
    case 'f':
      dev = strdup(optarg);
      break;
    default:
      break;
    }
  argc -= optind;
  argv += optind;
  dump = fopen("memdump", "w");
  fd = open_tty(dev);
  transfer(fd, dump, 0x8FF);
  close(fd);
  fclose(dump);
  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)
lea    0xe73(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11e2 <main+0x72>
mov    -0x1c(%rbp),%eax
sub    $0x66,%eax
jne    11d8 <main+0x68>
jmp    11c1 <main+0x51>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    11dd <main+0x6d>
jmp    11dd <main+0x6d>
jmp    1191 <main+0x21>
mov    0x2df7(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    0xdfb(%rip),%rdi
lea    0xdfc(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <open_tty@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x8ff,%edx
call   1090 <transfer@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe7c(%rip),%r14
lea    0xe80(%rip),%r15
mov    0x2e66(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <getopt@plt>
cmp    $0xffffffff,%eax
je     11c5 <main+0x55>
cmp    $0x66,%eax
jne    11a0 <main+0x30>
mov    (%r12),%edi
call   1080 <strdup@plt>
mov    %rax,%r14
jmp    11a0 <main+0x30>
lea    0xe42(%rip),%rdi
lea    0xe43(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <open_tty@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x8ff,%edx
call   1090 <transfer@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ttyU0\00"
@.str.1 = [3 x i8] c"f:\00"
@optarg = global i32 0
@optind = global i64 0
@.str.2 = [8 x i8] c"memdump\00"
@.str.3 = [2 x i8] c"w\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 i32
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %6
br label %10
10: ; preds = %21, %2
%11 = load i32, i32* %4
%12 = load i8**, i8*** %5
%13 = call i32 @getopt(i32 %11, i8** %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 %13, i32* %7
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %10
%16 = load i32, i32* %7
switch i32 %16, label %20 [
i32 102, label %17
]
17: ; preds = %15
%18 = load i32, i32* @optarg
%19 = call i8* @strdup(i32 %18)
store i8* %19, i8** %6
br label %21
20: ; preds = %15
br label %21
21: ; preds = %20, %17
br label %10
22: ; preds = %10
%23 = load i64, i64* @optind
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = sub nsw i64 %25, %23
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
%28 = load i64, i64* @optind
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 %28
store i8** %30, i8*** %5
%31 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %31, i32** %9
%32 = load i8*, i8** %6
%33 = call i32 @open_tty(i8* %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = load i32*, i32** %9
%36 = call i32 @transfer(i32 %34, i32* %35, i32 2303)
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32*, i32** %9
%40 = call i32 @fclose(i32* %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ttyU0\00"
@.str.1 = [3 x i8] c"f:\00"
@optarg = global i32 0
@optind = global i64 0
@.str.2 = [8 x i8] c"memdump\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %7, %2
%4 = phi i8* [ %9, %7 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %2 ]
br label %5
5: ; preds = %3, %5
%6 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
switch i32 %6, label %5 [
i32 -1, label %10
i32 102, label %7
]
7: ; preds = %5
%8 = load i32, i32* @optarg
%9 = tail call i8* @strdup(i32 %8)
br label %3
10: ; preds = %5
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @open_tty(i8* %4)
%13 = tail call i32 @transfer(i32 %12, i32* %11, i32 2303)
%14 = tail call i32 @close(i32 %12)
%15 = tail call i32 @fclose(i32* %11)
ret i32 0
}