#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 */

/* Variables and functions */
int /*<<< orphan*/ clear_memory(int, unsigned int, int);
int /*<<< orphan*/ fill_memory(int, unsigned int, unsigned char *, int);
scalar_t__ is_empty(unsigned char *, int);
int /*<<< orphan*/ print_progress_bar(int, int);
int send_file(int serial_fd, unsigned int address, unsigned char *data,
              int data_length) {
  int offset = 0;
  print_progress_bar(0, data_length);
  while (offset < data_length) {
    int this_slice = data_length - offset;
    if (this_slice > 1024)
      this_slice = 1024;
    if (is_empty(data + offset, this_slice)) {
      if (!clear_memory(serial_fd, address + offset, this_slice))
        return 0;
    } else {
      if (!fill_memory(serial_fd, address + offset, data + offset, this_slice))
        return 0;
    }
    offset += this_slice;
    print_progress_bar(offset, data_length);
  }
  return 1;
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
xor    %edi,%edi
call   1060 <print_progress_bar@plt>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1226 <send_file+0xe6>
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x400,-0x24(%rbp)
jle    118f <send_file+0x4f>
movl   $0x400,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
call   1040 <is_empty@plt>
cmp    $0x0,%rax
je     11d7 <send_file+0x97>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
add    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1050 <clear_memory@plt>
cmp    $0x0,%eax
jne    11d2 <send_file+0x92>
movl   $0x0,-0x4(%rbp)
jmp    122d <send_file+0xed>
jmp    120d <send_file+0xcd>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
add    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
movslq -0x20(%rbp),%rax
add    %rax,%rdx
mov    -0x24(%rbp),%ecx
call   1030 <fill_memory@plt>
cmp    $0x0,%eax
jne    1208 <send_file+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    122d <send_file+0xed>
jmp    120d <send_file+0xcd>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <print_progress_bar@plt>
jmp    1166 <send_file+0x26>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,0x10(%rsp)
mov    %esi,0xc(%rsp)
mov    %edi,%r13d
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ecx,%esi
call   1060 <print_progress_bar@plt>
mov    $0x1,%eax
test   %r14d,%r14d
jg     119f <send_file+0x5f>
jmp    11ec <send_file+0xac>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdx
mov    %ebp,%ecx
call   1030 <fill_memory@plt>
test   %eax,%eax
je     11e3 <send_file+0xa3>
add    %ebp,%ebx
mov    %ebx,%edi
mov    %r14d,%esi
call   1060 <print_progress_bar@plt>
cmp    %r14d,%ebx
jge    11e7 <send_file+0xa7>
mov    %r14d,%ebp
sub    %ebx,%ebp
cmp    $0x400,%ebp
mov    $0x400,%eax
cmovge %eax,%ebp
movslq %ebx,%r15
mov    0x10(%rsp),%rax
lea    (%rax,%r15,1),%r12
mov    %r12,%rdi
mov    %ebp,%esi
call   1040 <is_empty@plt>
add    0xc(%rsp),%r15d
mov    %r13d,%edi
mov    %r15d,%esi
test   %rax,%rax
je     1180 <send_file+0x40>
mov    %ebp,%edx
call   1050 <clear_memory@plt>
test   %eax,%eax
jne    118e <send_file+0x4e>
xor    %eax,%eax
jmp    11ec <send_file+0xac>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send_file(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* %9
%13 = call i32 @print_progress_bar(i32 0, i32 %12)
br label %14
14: ; preds = %57, %4
%15 = load i32, i32* %10
%16 = load i32, i32* %9
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %64
18: ; preds = %14
%19 = load i32, i32* %9
%20 = load i32, i32* %10
%21 = sub nsw i32 %19, %20
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = icmp sgt i32 %22, 1024
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 1024, i32* %11
br label %25
25: ; preds = %24, %18
%26 = load i8*, i8** %8
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %11
%31 = call i64 @is_empty(i8* %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %25
%34 = load i32, i32* %6
%35 = load i32, i32* %7
%36 = load i32, i32* %10
%37 = add i32 %35, %36
%38 = load i32, i32* %11
%39 = call i32 @clear_memory(i32 %34, i32 %37, i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %33
store i32 0, i32* %5
br label %65
42: ; preds = %33
br label %57
43: ; preds = %25
%44 = load i32, i32* %6
%45 = load i32, i32* %7
%46 = load i32, i32* %10
%47 = add i32 %45, %46
%48 = load i8*, i8** %8
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i32, i32* %11
%53 = call i32 @fill_memory(i32 %44, i32 %47, i8* %51, i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %43
store i32 0, i32* %5
br label %65
56: ; preds = %43
br label %57
57: ; preds = %56, %42
%58 = load i32, i32* %11
%59 = load i32, i32* %10
%60 = add nsw i32 %59, %58
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = load i32, i32* %9
%63 = call i32 @print_progress_bar(i32 %61, i32 %62)
br label %14
64: ; preds = %14
store i32 1, i32* %5
br label %65
65: ; preds = %64, %55, %41
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
define i32 @send_file(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i32 @print_progress_bar(i32 0, i32 %3)
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %27
7: ; preds = %4, %23
%8 = phi i32 [ %24, %23 ], [ 0, %4 ]
%9 = sub nsw i32 %3, %8
%10 = icmp slt i32 %9, 1024
%11 = select i1 %10, i32 %9, i32 1024
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %2, i64 %12
%14 = tail call i64 @is_empty(i8* %13, i32 %11)
%15 = icmp eq i64 %14, 0
%16 = add i32 %8, %1
br i1 %15, label %20, label %17
17: ; preds = %7
%18 = tail call i32 @clear_memory(i32 %0, i32 %16, i32 %11)
%19 = icmp eq i32 %18, 0
br i1 %19, label %27, label %23
20: ; preds = %7
%21 = tail call i32 @fill_memory(i32 %0, i32 %16, i8* %13, i32 %11)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %17, %20
%24 = add nsw i32 %11, %8
%25 = tail call i32 @print_progress_bar(i32 %24, i32 %3)
%26 = icmp slt i32 %24, %3
br i1 %26, label %7, label %27
27: ; preds = %23, %17, %20, %4
%28 = phi i32 [ 1, %4 ], [ 0, %20 ], [ 0, %17 ], [ 1, %23 ]
ret i32 %28
}