#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ serial_close(int);
int serial_open(char *);
int serial_read(int);
int /*<<< orphan*/ *stdout;
int main(int argc, char *argv[]) {
  FILE *out;
  int count;
  int fd;
  int n;
  if (argc != 2) {
    printf("Usage: %s <filename>\n", argv[0]);
    exit(1);
  }
  out = fopen(argv[1], "wb");
  if (out == NULL) {
    printf("Could not open file '%s'\n", argv[1]);
    exit(1);
  }
  fd = serial_open("/dev/ttyUSB1");
  count = 0;
  while (1) {
    n = serial_read(fd);
    if (n < 0) {
      break;
    }
    putc(n, out);
    fflush(out);
    printf("%c", n);
    fflush(stdout);
    count++;
  }
  serial_close(fd);
  fclose(out);
  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)
cmpl   $0x2,-0x8(%rbp)
je     11cf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe38(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1212 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe1a(%rip),%rdi
call   1070 <serial_open@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   10b0 <serial_read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1242 <main+0xb2>
jmp    1285 <main+0xf5>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <putc@plt>
mov    -0x18(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x24(%rbp),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%rdi
call   1080 <fflush@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1228 <main+0x98>
mov    -0x20(%rbp),%edi
call   1060 <serial_close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    124c <main+0xac>
mov    0x8(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1259 <main+0xb9>
mov    %rax,%rbx
lea    0xe5d(%rip),%rdi
call   1080 <serial_open@plt>
mov    %eax,%r14d
mov    %eax,%edi
call   10c0 <serial_read@plt>
test   %eax,%eax
js     122f <main+0x8f>
mov    %eax,%ebp
mov    0x2dee(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <putc@plt>
mov    %rbx,%rdi
call   1090 <fflush@plt>
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    (%r15),%rdi
call   1090 <fflush@plt>
mov    %r14d,%edi
call   10c0 <serial_read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1200 <main+0x60>
mov    %r14d,%edi
call   1070 <serial_close@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%rbp),%rsi
lea    0xda9(%rip),%rdi
jmp    1264 <main+0xc4>
mov    0x8(%rbp),%rsi
lea    0xdb5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [26 x i8] c"Could not open file '%s'\0A\00"
@.str.3 = [13 x i8] c"/dev/ttyUSB1\00"
@.str.4 = [3 x i8] c"%c\00"
@stdout = global i32* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %18
%32 = call i32 @serial_open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 %32, i32* %8
store i32 0, i32* %7
br label %33
33: ; preds = %31, %39
%34 = load i32, i32* %8
%35 = call i32 @serial_read(i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %51
39: ; preds = %33
%40 = load i32, i32* %9
%41 = load i32*, i32** %6
%42 = call i32 @putc(i32 %40, i32* %41)
%43 = load i32*, i32** %6
%44 = call i32 @fflush(i32* %43)
%45 = load i32, i32* %9
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = load i32*, i32** @stdout
%48 = call i32 @fflush(i32* %47)
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %33
51: ; preds = %38
%52 = load i32, i32* %8
%53 = call i32 @serial_close(i32 %52)
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [26 x i8] c"Could not open file '%s'\0A\00"
@.str.3 = [13 x i8] c"/dev/ttyUSB1\00"
@stdout = global i32* null
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = load i8*, i8** %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = tail call i32 @serial_open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @serial_read(i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %17, %21
%22 = phi i32 [ %28, %21 ], [ %19, %17 ]
%23 = tail call i32 @putc(i32 %22, i32* %11)
%24 = tail call i32 @fflush(i32* %11)
%25 = tail call i32 @putchar(i32 %22)
%26 = load i32*, i32** @stdout
%27 = tail call i32 @fflush(i32* %26)
%28 = tail call i32 @serial_read(i32 %18)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %21
30: ; preds = %21, %17
%31 = tail call i32 @serial_close(i32 %18)
%32 = tail call i32 @fclose(i32* %11)
ret i32 0
}