#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ closeuart();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ init(char *, int);
unsigned char *malloc(long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ receive(unsigned char *, int);
int /*<<< orphan*/ transmit(unsigned char *, int);
int /*<<< orphan*/ usleep(float);
int main(int argc, char *argv[]) {
  int baud = 115200;
  init("/dev/tty.usbserial-142B", baud);
  FILE *f = fopen("frame_64.raw", "rb");
  fseek(f, 0, SEEK_END);
  long fsize = ftell(f);
  fseek(f, 0, SEEK_SET);
  unsigned char *txBuffer = malloc(fsize + 1);
  unsigned char *rxBuffer = malloc(fsize + 1);
  fread(txBuffer, fsize, 1, f);
  fclose(f);
  txBuffer[fsize] = 0;
  int block = 512;
  float sleepTime = (block * 8) / ((float)baud);
  sleepTime *= 1000000;
  sleepTime *= 2.0f;
  printf("TOT SIZE %d\n", fsize);
  for (int i = 0; i < fsize; i += block) {
    transmit(txBuffer + i, block);
    printf("TXDONE\n");
    usleep(sleepTime);
    receive(rxBuffer + i, block);
  }
  FILE *of = fopen("uart.raw", "wb");
  fwrite(rxBuffer, 1, fsize, of);
  fclose(of);
  closeuart();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1c200,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1090 <init@plt>
lea    0xe1d(%rip),%rdi
lea    0xe23(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x200,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
shl    $0x3,%eax
cvtsi2ss %eax,%xmm0
cvtsi2ssl -0x14(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x40(%rbp)
movss  0xd48(%rip),%xmm0
mulss  -0x40(%rbp),%xmm0
movss  %xmm0,-0x40(%rbp)
movss  0xd32(%rip),%xmm0
mulss  -0x40(%rbp),%xmm0
movss  %xmm0,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    134b <main+0x17b>
mov    -0x30(%rbp),%rdi
movslq -0x44(%rbp),%rax
add    %rax,%rdi
mov    -0x3c(%rbp),%esi
call   10a0 <transmit@plt>
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movss  -0x40(%rbp),%xmm0
call   10f0 <usleep@plt>
mov    -0x38(%rbp),%rdi
movslq -0x44(%rbp),%rax
add    %rax,%rdi
mov    -0x3c(%rbp),%esi
call   1060 <receive@plt>
mov    -0x3c(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    12f1 <main+0x121>
lea    0xcf3(%rip),%rdi
lea    0xcf5(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    -0x50(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1070 <closeuart@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe14(%rip),%rdi
mov    $0x1c200,%esi
call   10a0 <init@plt>
lea    0xe1b(%rip),%rdi
lea    0xe21(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,%r12
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r12,%rdi
call   1090 <ftell@plt>
mov    %rax,%r14
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
lea    0x1(%r14),%r15
mov    %r15,%rdi
call   10c0 <malloc@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   10c0 <malloc@plt>
mov    %rax,%r15
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
movb   $0x0,0x0(%r13,%r14,1)
lea    0xda9(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r14,%r14
jle    12dd <main+0xfd>
xor    %ebx,%ebx
lea    0xdaa(%rip),%r12
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%r13,1),%rdi
mov    $0x200,%esi
call   10b0 <transmit@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
movss  0xd42(%rip),%xmm0
call   1100 <usleep@plt>
lea    (%r15,%rbx,1),%rdi
mov    $0x200,%esi
call   1070 <receive@plt>
add    $0x200,%rbx
cmp    %rbx,%r14
jg     12a0 <main+0xc0>
lea    0xd55(%rip),%rdi
lea    0xd57(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   10f0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
call   1080 <closeuart@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/dev/tty.usbserial-142B\00"
@.str.1 = [13 x i8] c"frame_64.raw\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"TOT SIZE %d\0A\00"
@.str.4 = [8 x i8] c"TXDONE\0A\00"
@.str.5 = [9 x i8] c"uart.raw\00"
@.str.6 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca float
%13 = alloca i32
%14 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 115200, i32* %6
%15 = load i32, i32* %6
%16 = call i32 @init(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %7
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %7
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %8
%23 = load i32*, i32** %7
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i64, i64* %8
%27 = add nsw i64 %26, 1
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %9
%29 = load i64, i64* %8
%30 = add nsw i64 %29, 1
%31 = call i8* @malloc(i64 %30)
store i8* %31, i8** %10
%32 = load i8*, i8** %9
%33 = load i64, i64* %8
%34 = load i32*, i32** %7
%35 = call i32 @fread(i8* %32, i64 %33, i32 1, i32* %34)
%36 = load i32*, i32** %7
%37 = call i32 @fclose(i32* %36)
%38 = load i8*, i8** %9
%39 = load i64, i64* %8
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 0, i8* %40
store i32 512, i32* %11
%41 = load i32, i32* %11
%42 = mul nsw i32 %41, 8
%43 = sitofp i32 %42 to float
%44 = load i32, i32* %6
%45 = sitofp i32 %44 to float
%46 = fdiv float %43, %45
store float %46, float* %12
%47 = load float, float* %12
%48 = fmul float %47, 1.000000e+06
store float %48, float* %12
%49 = load float, float* %12
%50 = fmul float %49, 2.000000e+00
store float %50, float* %12
%51 = load i64, i64* %8
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 %51)
store i32 0, i32* %13
br label %53
53: ; preds = %74, %2
%54 = load i32, i32* %13
%55 = sext i32 %54 to i64
%56 = load i64, i64* %8
%57 = icmp slt i64 %55, %56
br i1 %57, label %58, label %78
58: ; preds = %53
%59 = load i8*, i8** %9
%60 = load i32, i32* %13
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = load i32, i32* %11
%64 = call i32 @transmit(i8* %62, i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%66 = load float, float* %12
%67 = call i32 @usleep(float %66)
%68 = load i8*, i8** %10
%69 = load i32, i32* %13
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i32, i32* %11
%73 = call i32 @receive(i8* %71, i32 %72)
br label %74
74: ; preds = %58
%75 = load i32, i32* %11
%76 = load i32, i32* %13
%77 = add nsw i32 %76, %75
store i32 %77, i32* %13
br label %53
78: ; preds = %53
%79 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %79, i32** %14
%80 = load i8*, i8** %10
%81 = load i64, i64* %8
%82 = load i32*, i32** %14
%83 = call i32 @fwrite(i8* %80, i32 1, i64 %81, i32* %82)
%84 = load i32*, i32** %14
%85 = call i32 @fclose(i32* %84)
%86 = call i32 (...) @closeuart()
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@.str = [24 x i8] c"/dev/tty.usbserial-142B\00"
@.str.1 = [13 x i8] c"frame_64.raw\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"TOT SIZE %d\0A\00"
@.str.5 = [9 x i8] c"uart.raw\00"
@.str.6 = [3 x i8] c"wb\00"
@str = [7 x i8] c"TXDONE\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @init(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 115200)
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %4, i32 0, i32 %5)
%7 = tail call i64 @ftell(i32* %4)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %4, i32 0, i32 %8)
%10 = add nsw i64 %7, 1
%11 = tail call i8* @malloc(i64 %10)
%12 = tail call i8* @malloc(i64 %10)
%13 = tail call i32 @fread(i8* %11, i64 %7, i32 1, i32* %4)
%14 = tail call i32 @fclose(i32* %4)
%15 = getelementptr inbounds i8, i8* %11, i64 %7
store i8 0, i8* %15
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 %7)
%17 = icmp sgt i64 %7, 0
br i1 %17, label %23, label %18
18: ; preds = %23, %2
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%20 = tail call i32 @fwrite(i8* %12, i32 1, i64 %7, i32* %19)
%21 = tail call i32 @fclose(i32* %19)
%22 = tail call i32 (...) @closeuart()
ret i32 0
23: ; preds = %2, %23
%24 = phi i64 [ %31, %23 ], [ 0, %2 ]
%25 = getelementptr inbounds i8, i8* %11, i64 %24
%26 = tail call i32 @transmit(i8* %25, i32 512)
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%28 = tail call i32 @usleep(float 0x40F15C71C0000000)
%29 = getelementptr inbounds i8, i8* %12, i64 %24
%30 = tail call i32 @receive(i8* %29, i32 512)
%31 = add nuw i64 %24, 512
%32 = icmp sgt i64 %7, %31
br i1 %32, label %23, label %18
}