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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
scalar_t__ serial_data_avail();
char serial_read_byte();
int /*<<< orphan*/ serial_write(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int strlen(char *);
int samba_send_file(const char *filename, uint32_t address, uint32_t offset,
                    uint32_t size, int quiet) {
  FILE *f;
  unsigned char *buf;
  uint32_t file_size, sent;
  int idx = 0;
  int boffset = 0;
  char tmp[4097];
  uint32_t tstart;
  f = fopen(filename, "rb");
  if (!f)
    die("file open error: '%s'", filename);
  if (!size) {
    fseek(f, 0, SEEK_END);
    size = ftell(f);
    offset = 0;
    rewind(f);
  } else
    fseek(f, offset, SEEK_SET);
  if (!quiet)
    printf("size 0x%x (%dKb)\n", size, size / 1024);
  buf = malloc(size + 1);
  if (!buf)
    die("malloc failed");
  fread(buf, 1, size, f);
  fclose(f);
  while (size > 0) {
    int tosend = size > 1024 ? 1024 : size;
    snprintf(tmp, 128, "S%08x,%x#", address, tosend);
    serial_write(tmp, strlen(tmp));
    memcpy(tmp, buf + boffset, tosend);
    tmp[tosend] = '\n';
    serial_write(tmp, tosend + 1);
    size -= tosend;
    boffset += tosend;
    address += tosend;
    while (1) {
      if (serial_data_avail())
        if (serial_read_byte() == '>')
          break;
    }
  }
  free(buf);
  return boffset;
}


x86 assembly (clang, -O0):
<samba_send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdcb(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125b <samba_send_file+0x5b>
mov    -0x8(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1110 <die@plt>
cmpl   $0x0,-0x14(%rbp)
jne    129a <samba_send_file+0x9a>
mov    -0x20(%rbp),%rdi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <ftell@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <rewind@plt>
jmp    12af <samba_send_file+0xaf>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
call   10f0 <fseek@plt>
cmpl   $0x0,-0x18(%rbp)
jne    12d5 <samba_send_file+0xd5>
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
lea    0xd49(%rip),%rdi
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   10e0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12fd <samba_send_file+0xfd>
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1110 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x14(%rbp)
jle    1440 <samba_send_file+0x240>
cmpl   $0x400,-0x14(%rbp)
jle    1342 <samba_send_file+0x142>
mov    $0x400,%eax
mov    %eax,-0x104c(%rbp)
jmp    134b <samba_send_file+0x14b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x104c(%rbp)
mov    -0x104c(%rbp),%eax
mov    %eax,-0x1048(%rbp)
lea    -0x1040(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    -0x1048(%rbp),%r8d
mov    $0x80,%esi
lea    0xcc5(%rip),%rdx
call   1090 <snprintf@plt>
lea    -0x1040(%rbp),%rax
mov    %rax,-0x1058(%rbp)
lea    -0x1040(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x1058(%rbp),%rdi
mov    %eax,%esi
call   1120 <serial_write@plt>
lea    -0x1040(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x38(%rbp),%rax
add    %rax,%rsi
mov    -0x1048(%rbp),%edx
call   10c0 <memcpy@plt>
movslq -0x1048(%rbp),%rax
movb   $0xa,-0x1040(%rbp,%rax,1)
0a
lea    -0x1040(%rbp),%rdi
mov    -0x1048(%rbp),%esi
add    $0x1,%esi
call   1120 <serial_write@plt>
mov    -0x1048(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1048(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1048(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   10a0 <serial_data_avail@plt>
cmp    $0x0,%rax
je     1436 <samba_send_file+0x236>
mov    $0x0,%al
call   10d0 <serial_read_byte@plt>
movsbl %al,%eax
cmp    $0x3e,%eax
jne    1431 <samba_send_file+0x231>
jmp    143b <samba_send_file+0x23b>
jmp    1436 <samba_send_file+0x236>
jmp    1408 <samba_send_file+0x208>
jmp    131b <samba_send_file+0x11b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<samba_send_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %r8d,%r14d
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%r13
lea    0xddb(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     1355 <samba_send_file+0x155>
test   %ebx,%ebx
je     136e <samba_send_file+0x16e>
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12d,%esi
call   10f0 <fseek@plt>
test   %r14d,%r14d
jne    1273 <samba_send_file+0x73>
lea    0x3ff(%rbx),%edx
test   %ebx,%ebx
cmovns %ebx,%edx
sar    $0xa,%edx
lea    0xdad(%rip),%rdi
mov    %ebx,%esi
call   1070 <printf@plt>
lea    0x1(%rbx),%edi
call   10e0 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1291 <samba_send_file+0x91>
lea    0xda1(%rip),%rdi
xor    %eax,%eax
call   1110 <die@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %ebx,%ebx
jle    13a1 <samba_send_file+0x1a1>
mov    %rsp,%r13
xor    %r15d,%r15d
nopl   0x0(%rax)
cmp    $0x400,%ebx
mov    $0x400,%r12d
cmovb  %ebx,%r12d
mov    %r13,%rdi
mov    $0x80,%esi
lea    0xd5a(%rip),%rdx
mov    %ebp,%ecx
mov    %r12d,%r8d
call   1090 <snprintf@plt>
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1120 <serial_write@plt>
mov    %r15d,%esi
add    %r14,%rsi
mov    %r13,%rdi
mov    %r12d,%edx
call   10c0 <memcpy@plt>
movb   $0xa,(%rsp,%r12,1)
lea    0x1(%r12),%esi
mov    %r13,%rdi
call   1120 <serial_write@plt>
add    %r12d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <serial_data_avail@plt>
test   %rax,%rax
je     1330 <samba_send_file+0x130>
xor    %eax,%eax
call   10d0 <serial_read_byte@plt>
cmp    $0x3e,%al
jne    1330 <samba_send_file+0x130>
add    %r12d,%ebp
sub    %r12d,%ebx
jg     12c0 <samba_send_file+0xc0>
jmp    13a4 <samba_send_file+0x1a4>
lea    0xca7(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1110 <die@plt>
test   %ebx,%ebx
jne    123e <samba_send_file+0x3e>
mov    0x2c73(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %r15,%rdi
call   10b0 <ftell@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1080 <rewind@plt>
test   %r14d,%r14d
jne    1273 <samba_send_file+0x73>
jmp    1257 <samba_send_file+0x57>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [22 x i8] c"file open error: '%s'\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"size 0x%x (%dKb)\0A\00"
@.str.3 = [14 x i8] c"malloc failed\00"
@.str.4 = [10 x i8] c"S%08x,%x#\00"
define i32 @samba_send_file(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca [4097 x i8]
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %15
store i32 0, i32* %16
%20 = load i8*, i8** %6
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %11
%22 = load i32*, i32** %11
%23 = icmp ne i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %5
%25 = load i8*, i8** %6
%26 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %5
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %27
%31 = load i32*, i32** %11
%32 = load i32, i32* @SEEK_END
%33 = call i32 @fseek(i32* %31, i32 0, i32 %32)
%34 = load i32*, i32** %11
%35 = call i32 @ftell(i32* %34)
store i32 %35, i32* %9
store i32 0, i32* %8
%36 = load i32*, i32** %11
%37 = call i32 @rewind(i32* %36)
br label %43
38: ; preds = %27
%39 = load i32*, i32** %11
%40 = load i32, i32* %8
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @fseek(i32* %39, i32 %40, i32 %41)
br label %43
43: ; preds = %38, %30
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %43
%47 = load i32, i32* %9
%48 = load i32, i32* %9
%49 = sdiv i32 %48, 1024
%50 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %47, i32 %49)
br label %51
51: ; preds = %46, %43
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
%54 = call i8* @malloc(i32 %53)
store i8* %54, i8** %12
%55 = load i8*, i8** %12
%56 = icmp ne i8* %55, null
br i1 %56, label %59, label %57
57: ; preds = %51
%58 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %51
%60 = load i8*, i8** %12
%61 = load i32, i32* %9
%62 = load i32*, i32** %11
%63 = call i32 @fread(i8* %60, i32 1, i32 %61, i32* %62)
%64 = load i32*, i32** %11
%65 = call i32 @fclose(i32* %64)
br label %66
66: ; preds = %118, %59
%67 = load i32, i32* %9
%68 = icmp sgt i32 %67, 0
br i1 %68, label %69, label %119
69: ; preds = %66
%70 = load i32, i32* %9
%71 = icmp sgt i32 %70, 1024
br i1 %71, label %72, label %73
72: ; preds = %69
br label %75
73: ; preds = %69
%74 = load i32, i32* %9
br label %75
75: ; preds = %73, %72
%76 = phi i32 [ 1024, %72 ], [ %74, %73 ]
store i32 %76, i32* %19
%77 = getelementptr inbounds [4097 x i8], [4097 x i8]* %17, i64 0, i64 0
%78 = load i32, i32* %7
%79 = load i32, i32* %19
%80 = call i32 @snprintf(i8* %77, i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %78, i32 %79)
%81 = getelementptr inbounds [4097 x i8], [4097 x i8]* %17, i64 0, i64 0
%82 = getelementptr inbounds [4097 x i8], [4097 x i8]* %17, i64 0, i64 0
%83 = call i32 @strlen(i8* %82)
%84 = call i32 @serial_write(i8* %81, i32 %83)
%85 = getelementptr inbounds [4097 x i8], [4097 x i8]* %17, i64 0, i64 0
%86 = load i8*, i8** %12
%87 = load i32, i32* %16
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %86, i64 %88
%90 = load i32, i32* %19
%91 = call i32 @memcpy(i8* %85, i8* %89, i32 %90)
%92 = load i32, i32* %19
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [4097 x i8], [4097 x i8]* %17, i64 0, i64 %93
store i8 10, i8* %94
%95 = getelementptr inbounds [4097 x i8], [4097 x i8]* %17, i64 0, i64 0
%96 = load i32, i32* %19
%97 = add nsw i32 %96, 1
%98 = call i32 @serial_write(i8* %95, i32 %97)
%99 = load i32, i32* %19
%100 = load i32, i32* %9
%101 = sub nsw i32 %100, %99
store i32 %101, i32* %9
%102 = load i32, i32* %19
%103 = load i32, i32* %16
%104 = add nsw i32 %103, %102
store i32 %104, i32* %16
%105 = load i32, i32* %19
%106 = load i32, i32* %7
%107 = add nsw i32 %106, %105
store i32 %107, i32* %7
br label %108
108: ; preds = %75, %117
%109 = call i64 (...) @serial_data_avail()
%110 = icmp ne i64 %109, 0
br i1 %110, label %111, label %117
111: ; preds = %108
%112 = call signext i8 (...) @serial_read_byte()
%113 = sext i8 %112 to i32
%114 = icmp eq i32 %113, 62
br i1 %114, label %115, label %116
115: ; preds = %111
br label %118
116: ; preds = %111
br label %117
117: ; preds = %116, %108
br label %108
118: ; preds = %115
br label %66
119: ; preds = %66
%120 = load i8*, i8** %12
%121 = call i32 @free(i8* %120)
%122 = load i32, i32* %16
ret i32 %122
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [22 x i8] c"file open error: '%s'\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"size 0x%x (%dKb)\0A\00"
@.str.3 = [14 x i8] c"malloc failed\00"
@.str.4 = [10 x i8] c"S%08x,%x#\00"
define i32 @samba_send_file(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca [4097 x i8]
%7 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %5
%13 = icmp eq i32 %3, 0
br i1 %13, label %14, label %19
14: ; preds = %12
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @fseek(i32* %8, i32 0, i32 %15)
%17 = tail call i32 @ftell(i32* %8)
%18 = tail call i32 @rewind(i32* %8)
br label %22
19: ; preds = %12
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %8, i32 %2, i32 %20)
br label %22
22: ; preds = %19, %14
%23 = phi i32 [ %3, %19 ], [ %17, %14 ]
%24 = icmp eq i32 %4, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = sdiv i32 %23, 1024
%27 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %26)
br label %28
28: ; preds = %25, %22
%29 = add nsw i32 %23, 1
%30 = tail call i8* @malloc(i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = tail call i32 @fread(i8* %30, i32 1, i32 %23, i32* %8)
%36 = tail call i32 @fclose(i32* %8)
%37 = icmp sgt i32 %23, 0
br i1 %37, label %42, label %66
38: ; preds = %62
%39 = sub nsw i32 %45, %47
%40 = add nsw i32 %43, %47
%41 = icmp sgt i32 %39, 0
br i1 %41, label %42, label %66
42: ; preds = %34, %38
%43 = phi i32 [ %40, %38 ], [ %1, %34 ]
%44 = phi i32 [ %58, %38 ], [ 0, %34 ]
%45 = phi i32 [ %39, %38 ], [ %23, %34 ]
%46 = icmp ult i32 %45, 1024
%47 = select i1 %46, i32 %45, i32 1024
%48 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %43, i32 %47)
%49 = call i32 @strlen(i8* %7)
%50 = call i32 @serial_write(i8* %7, i32 %49)
%51 = zext i32 %44 to i64
%52 = getelementptr inbounds i8, i8* %30, i64 %51
%53 = call i32 @memcpy(i8* %7, i8* %52, i32 %47)
%54 = zext i32 %47 to i64
%55 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 %54
store i8 10, i8* %55
%56 = add nuw nsw i32 %47, 1
%57 = call i32 @serial_write(i8* %7, i32 %56)
%58 = add nuw nsw i32 %47, %44
br label %59
59: ; preds = %65, %42
%60 = call i64 (...) @serial_data_avail()
%61 = icmp eq i64 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = call signext i8 (...) @serial_read_byte()
%64 = icmp eq i8 %63, 62
br i1 %64, label %38, label %65
65: ; preds = %62, %59
br label %59
66: ; preds = %38, %34
%67 = phi i32 [ 0, %34 ], [ %58, %38 ]
%68 = call i32 @free(i8* %30)
ret i32 %67
}