#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 scalar_t__ uint8_t;
typedef scalar_t__ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ serial_close(int);
int serial_open(char *);
int serial_read(int);
int /*<<< orphan*/ serial_send(int, scalar_t__ *, int);
int main(int argc, char *argv[]) {
  FILE *in;
  uint8_t buffer[1];
  int count;
  int fd;
  int ch, n;
  if (argc != 2) {
    printf("Usage: %s <filename>\n", argv[0]);
    exit(1);
  }
  in = fopen(argv[1], "rb");
  if (in == NULL) {
    printf("Could not open file '%s'\n", argv[1]);
    exit(1);
  }
  fd = serial_open("/dev/ttyUSB1");
  count = 0;
  while (1) {
    ch = getc(in);
    if (ch == EOF) {
      break;
    }
    buffer[0] = (uint32_t)ch;
    serial_send(fd, buffer, 1);
    n = serial_read(fd);
    if (n < 0) {
      break;
    }
    count++;
    if ((count % 100) == 0) {
      printf("bytes transferred: %d\n", count);
    }
  }
  serial_close(fd);
  fclose(in);
  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   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe38(%rip),%rsi
call   1080 <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   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe1a(%rip),%rdi
call   1070 <serial_open@plt>
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   10b0 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%eax
jne    124b <main+0xbb>
jmp    12b1 <main+0x121>
movslq -0x2c(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <serial_send@plt>
mov    -0x28(%rbp),%edi
call   10a0 <serial_read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    127e <main+0xee>
jmp    12b1 <main+0x121>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12ac <main+0x11c>
mov    -0x24(%rbp),%esi
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1228 <main+0x98>
mov    -0x28(%rbp),%edi
call   1060 <serial_close@plt>
mov    -0x18(%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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    127c <main+0xec>
mov    0x8(%rbx),%rdi
lea    0xe64(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1288 <main+0xf8>
mov    %rax,%r14
lea    0xe69(%rip),%rdi
call   1070 <serial_open@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   10b0 <getc@plt>
mov    0x2e08(%rip),%r13
cmp    0x0(%r13),%eax
jne    1206 <main+0x76>
mov    %ebp,%edi
call   1060 <serial_close@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r12d
mov    $0x1,%ebx
mov    %rsp,%r15
jmp    1235 <main+0xa5>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   10b0 <getc@plt>
add    $0xffffffff,%r12d
add    $0x1,%ebx
cmp    0x0(%r13),%eax
je     11e6 <main+0x56>
cltq
mov    %rax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <serial_send@plt>
mov    %ebp,%edi
call   10a0 <serial_read@plt>
test   %eax,%eax
js     11e6 <main+0x56>
mov    %ebx,%eax
imul   $0x51eb851f,%rax,%rax
shr    $0x25,%rax
imul   $0x64,%eax,%eax
add    %r12d,%eax
jne    1220 <main+0x90>
lea    0xdcf(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <main+0x90>
mov    (%rbx),%rsi
lea    0xd7a(%rip),%rdi
jmp    1293 <main+0x103>
mov    0x8(%rbx),%rsi
lea    0xd86(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Could not open file '%s'\0A\00"
@.str.3 = [13 x i8] c"/dev/ttyUSB1\00"
@EOF = global i32 0
@.str.4 = [23 x i8] c"bytes transferred: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x i64]
%4 = bitcast [1 x i64]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = tail call i32 @serial_open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @getc(i32* %13)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %43, label %24
24: ; preds = %19
%25 = getelementptr inbounds [1 x i64], [1 x i64]* %3, i64 0, i64 0
br label %26
26: ; preds = %24, %39
%27 = phi i32 [ %21, %24 ], [ %40, %39 ]
%28 = phi i32 [ 0, %24 ], [ %34, %39 ]
%29 = sext i32 %27 to i64
store i64 %29, i64* %25
%30 = call i32 @serial_send(i32 %20, i64* %25, i32 1)
%31 = call i32 @serial_read(i32 %20)
%32 = icmp slt i32 %31, 0
br i1 %32, label %43, label %33
33: ; preds = %26
%34 = add nuw nsw i32 %28, 1
%35 = urem i32 %34, 100
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %39
39: ; preds = %37, %33
%40 = call i32 @getc(i32* %13)
%41 = load i32, i32* @EOF
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %26
43: ; preds = %39, %26, %19
%44 = call i32 @serial_close(i32 %20)
%45 = call i32 @fclose(i32* %13)
ret i32 0
}