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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
long ftell(int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int skytraq_cmd_wait(int, char *, int, char *, int);
int /*<<< orphan*/ skytraq_dbg_printf(int /*<<< orphan*/, char *, long, long);
int skytraq_waitstatus(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, long, unsigned char, int);
int strlen(char *);
int skytraq_send_bin(int fd, const char *filename) {
  FILE *file;
  char buf[8192];
  int count;
  unsigned char cksum;
  int c;
  long size;
  long pos;
  char message[1024];
  int ret;
  file = fopen(filename, "r");
  if (!file) {
    perror(filename);
    return -1;
  }
  cksum = 0;
  while ((c = getc(file)) != EOF)
    cksum += (unsigned char)c;
  size = ftell(file);
  rewind(file);
  sprintf(message, "BINSIZE = %d Checksum = %d Loopnumber = %d ", size, cksum,
          1);
  ret = skytraq_cmd_wait(fd, message, strlen(message) + 1, "OK", 20000);
  if (ret < 0)
    printf("waitstatus failed %d\n", ret);
  pos = 0;
  for (;;) {
    count = fread(buf, 1, sizeof(buf), file);
    if (count < 0) {
      perror("fread");
      fclose(file);
      return -1;
    }
    if (count == 0)
      break;
    skytraq_dbg_printf(0, "%7d of %7d ", pos + count, size);
    pos += count;
    ret = skytraq_cmd_wait(fd, buf, count, "OK", 20000);
    if (ret < 0)
      return ret;
  }
  return skytraq_waitstatus(fd, "END", 30000);
}


x86 assembly (clang, -O0):
<skytraq_send_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2460,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1216 <skytraq_send_bin+0x46>
mov    -0x10(%rbp),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1409 <skytraq_send_bin+0x239>
movb   $0x0,-0x2025(%rbp)
mov    -0x18(%rbp),%rdi
call   10f0 <getc@plt>
mov    %eax,-0x202c(%rbp)
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%eax
je     1258 <skytraq_send_bin+0x88>
mov    -0x202c(%rbp),%eax
movzbl %al,%ecx
movzbl -0x2025(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x2025(%rbp)
jmp    121d <skytraq_send_bin+0x4d>
mov    -0x18(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x2038(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0x2440(%rbp),%rdi
mov    -0x2038(%rbp),%rdx
lea    0xd7c(%rip),%rsi
mov    $0x1,%r8d
movzbl -0x2025(%rbp),%ecx
call   10e0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2454(%rbp)
lea    -0x2440(%rbp),%rax
mov    %rax,-0x2450(%rbp)
lea    -0x2440(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2454(%rbp),%edi
mov    -0x2450(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
lea    0xd5a(%rip),%rcx
mov    $0x4e20,%r8d
call   10d0 <skytraq_cmd_wait@plt>
mov    %eax,-0x2444(%rbp)
cmpl   $0x0,-0x2444(%rbp)
jge    1304 <skytraq_send_bin+0x134>
mov    -0x2444(%rbp),%esi
lea    0xd32(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x2040(%rbp)
lea    -0x2020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %eax,-0x2024(%rbp)
cmpl   $0x0,-0x2024(%rbp)
jge    135d <skytraq_send_bin+0x18d>
lea    0xd04(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1409 <skytraq_send_bin+0x239>
cmpl   $0x0,-0x2024(%rbp)
jne    136f <skytraq_send_bin+0x19f>
jmp    13f2 <skytraq_send_bin+0x222>
mov    -0x2040(%rbp),%rdx
movslq -0x2024(%rbp),%rax
add    %rax,%rdx
mov    -0x2038(%rbp),%rcx
xor    %edi,%edi
lea    0xcbd(%rip),%rsi
call   1090 <skytraq_dbg_printf@plt>
movslq -0x2024(%rbp),%rax
add    -0x2040(%rbp),%rax
mov    %rax,-0x2040(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x2024(%rbp),%edx
lea    0xc6d(%rip),%rcx
mov    $0x4e20,%r8d
call   10d0 <skytraq_cmd_wait@plt>
mov    %eax,-0x2444(%rbp)
cmpl   $0x0,-0x2444(%rbp)
jge    13ed <skytraq_send_bin+0x21d>
mov    -0x2444(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1409 <skytraq_send_bin+0x239>
jmp    130f <skytraq_send_bin+0x13f>
mov    -0x8(%rbp),%edi
lea    0xc5d(%rip),%rsi
mov    $0x7530,%edx
call   1080 <skytraq_waitstatus@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skytraq_send_bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2418,%rsp
mov    %rsi,%rbp
mov    %edi,0xc(%rsp)
lea    0xe11(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     131c <skytraq_send_bin+0x14c>
mov    %rax,%r14
xor    %ecx,%ecx
mov    0x2ddc(%rip),%rbx
nopl   0x0(%rax)
mov    %ecx,%ebp
mov    %r14,%rdi
call   10f0 <getc@plt>
lea    (%rax,%rbp,1),%ecx
cmp    (%rbx),%eax
jne    1210 <skytraq_send_bin+0x40>
mov    %r14,%rdi
call   10a0 <ftell@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1070 <rewind@plt>
movzbl %bpl,%ecx
lea    0xdc3(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rdx
mov    $0x1,%r8d
call   10e0 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edx
lea    0xdc7(%rip),%rcx
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    $0x4e20,%r8d
call   10d0 <skytraq_cmd_wait@plt>
test   %eax,%eax
jns    128b <skytraq_send_bin+0xbb>
lea    0xdad(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
xor    %ebp,%ebp
lea    0x410(%rsp),%r12
lea    0xd92(%rip),%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
js     132b <skytraq_send_bin+0x15b>
je     12f5 <skytraq_send_bin+0x125>
mov    %eax,%r13d
mov    %eax,%eax
add    %rax,%rbp
xor    %edi,%edi
lea    0xd81(%rip),%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1090 <skytraq_dbg_printf@plt>
mov    0xc(%rsp),%edi
mov    %r12,%rsi
mov    %r13d,%edx
mov    %rbx,%rcx
mov    $0x4e20,%r8d
call   10d0 <skytraq_cmd_wait@plt>
test   %eax,%eax
jns    12a0 <skytraq_send_bin+0xd0>
jmp    130a <skytraq_send_bin+0x13a>
lea    0xd5d(%rip),%rsi
mov    0xc(%rsp),%edi
mov    $0x7530,%edx
call   1080 <skytraq_waitstatus@plt>
add    $0x2418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
jmp    130a <skytraq_send_bin+0x13a>
lea    0xd15(%rip),%rdi
call   10c0 <perror@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
jmp    130a <skytraq_send_bin+0x13a>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [44 x i8] c"BINSIZE = %d Checksum = %d Loopnumber = %d \00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [22 x i8] c"waitstatus failed %d\0A\00"
@.str.4 = [6 x i8] c"fread\00"
@.str.5 = [12 x i8] c"%7d of %7d \00"
@.str.6 = [4 x i8] c"END\00"
define i32 @skytraq_send_bin(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [8192 x i8]
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca [1024 x i8]
%14 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = call i32 @perror(i8* %20)
store i32 -1, i32* %3
br label %94
22: ; preds = %2
store i8 0, i8* %9
br label %23
23: ; preds = %28, %22
%24 = load i32*, i32** %6
%25 = call i32 @getc(i32* %24)
store i32 %25, i32* %10
%26 = load i32, i32* @EOF
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %36
28: ; preds = %23
%29 = load i32, i32* %10
%30 = trunc i32 %29 to i8
%31 = zext i8 %30 to i32
%32 = load i8, i8* %9
%33 = zext i8 %32 to i32
%34 = add nsw i32 %33, %31
%35 = trunc i32 %34 to i8
store i8 %35, i8* %9
br label %23
36: ; preds = %23
%37 = load i32*, i32** %6
%38 = call i64 @ftell(i32* %37)
store i64 %38, i64* %11
%39 = load i32*, i32** %6
%40 = call i32 @rewind(i32* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%42 = load i64, i64* %11
%43 = load i8, i8* %9
%44 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %42, i8 zeroext %43, i32 1)
%45 = load i32, i32* %4
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = add nsw i32 %48, 1
%50 = call i32 @skytraq_cmd_wait(i32 %45, i8* %46, i32 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 20000)
store i32 %50, i32* %14
%51 = load i32, i32* %14
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %36
%54 = load i32, i32* %14
%55 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %53, %36
store i64 0, i64* %12
br label %57
57: ; preds = %90, %56
%58 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%59 = load i32*, i32** %6
%60 = call i32 @fread(i8* %58, i32 1, i32 8192, i32* %59)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
store i32 -1, i32* %3
br label %94
67: ; preds = %57
%68 = load i32, i32* %8
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %67
br label %91
71: ; preds = %67
%72 = load i64, i64* %12
%73 = load i32, i32* %8
%74 = sext i32 %73 to i64
%75 = add nsw i64 %72, %74
%76 = load i64, i64* %11
%77 = call i32 @skytraq_dbg_printf(i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i64 %75, i64 %76)
%78 = load i32, i32* %8
%79 = sext i32 %78 to i64
%80 = load i64, i64* %12
%81 = add nsw i64 %80, %79
store i64 %81, i64* %12
%82 = load i32, i32* %4
%83 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%84 = load i32, i32* %8
%85 = call i32 @skytraq_cmd_wait(i32 %82, i8* %83, i32 %84, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 20000)
store i32 %85, i32* %14
%86 = load i32, i32* %14
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %71
%89 = load i32, i32* %14
store i32 %89, i32* %3
br label %94
90: ; preds = %71
br label %57
91: ; preds = %70
%92 = load i32, i32* %4
%93 = call i32 @skytraq_waitstatus(i32 %92, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 30000)
store i32 %93, i32* %3
br label %94
94: ; preds = %91, %88, %63, %19
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [44 x i8] c"BINSIZE = %d Checksum = %d Loopnumber = %d \00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [22 x i8] c"waitstatus failed %d\0A\00"
@.str.4 = [6 x i8] c"fread\00"
@.str.5 = [12 x i8] c"%7d of %7d \00"
@.str.6 = [4 x i8] c"END\00"
define i32 @skytraq_send_bin(i32 %0, i8* nocapture %1) {
%3 = alloca [8192 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* %1)
br label %46
11: ; preds = %2, %11
%12 = phi i8 [ %17, %11 ], [ 0, %2 ]
%13 = tail call i32 @getc(i32* %7)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
%16 = trunc i32 %13 to i8
%17 = add i8 %12, %16
br i1 %15, label %18, label %11
18: ; preds = %11
%19 = tail call i64 @ftell(i32* %7)
%20 = tail call i32 @rewind(i32* %7)
%21 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %19, i8 zeroext %12, i32 1)
%22 = call i32 @strlen(i8* %6)
%23 = add nsw i32 %22, 1
%24 = call i32 @skytraq_cmd_wait(i32 %0, i8* %6, i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 20000)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %28
28: ; preds = %26, %18
br label %29
29: ; preds = %28, %38
%30 = phi i64 [ %40, %38 ], [ 0, %28 ]
%31 = call i32 @fread(i8* %5, i32 1, i32 8192, i32* %7)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @fclose(i32* %7)
br label %46
36: ; preds = %29
%37 = icmp eq i32 %31, 0
br i1 %37, label %44, label %38
38: ; preds = %36
%39 = zext i32 %31 to i64
%40 = add nuw nsw i64 %30, %39
%41 = call i32 @skytraq_dbg_printf(i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i64 %40, i64 %19)
%42 = call i32 @skytraq_cmd_wait(i32 %0, i8* %5, i32 %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 20000)
%43 = icmp slt i32 %42, 0
br i1 %43, label %46, label %29
44: ; preds = %36
%45 = call i32 @skytraq_waitstatus(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 30000)
br label %46
46: ; preds = %38, %44, %33, %9
%47 = phi i32 [ -1, %33 ], [ %45, %44 ], [ -1, %9 ], [ %42, %38 ]
ret i32 %47
}