#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, unsigned int);
int /*<<< orphan*/ fix_connection(int);
scalar_t__ is_empty(unsigned char *, unsigned int);
int /*<<< orphan*/ load_memory(int, unsigned int, unsigned char *,
                               unsigned int);
int /*<<< orphan*/ print_progress_bar(unsigned int, unsigned int);
bool send_segment(int serial_fd, unsigned int address, unsigned char *data,
                  unsigned int data_length) {
  unsigned int offset = 0;
  print_progress_bar(0, data_length);
  while (offset < data_length) {
    int copied_correctly = 1;
    unsigned 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 false;
    } else {
      if (!load_memory(serial_fd, address + offset, data + offset,
                       this_slice)) {
        copied_correctly = 0;
        if (!fix_connection(serial_fd)) {
          return false;
        }
      }
    }
    if (copied_correctly) {
      offset += this_slice;
    }
    print_progress_bar(offset, data_length);
  }
  return true;
}


x86 assembly (clang, -O0):
<send_segment>:
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
jae    1262 <send_segment+0x112>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x400,-0x28(%rbp)
jbe    11a6 <send_segment+0x56>
movl   $0x400,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%eax
add    %rax,%rdi
mov    -0x28(%rbp),%esi
call   1030 <is_empty@plt>
cmp    $0x0,%rax
je     11ed <send_segment+0x9d>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
add    -0x20(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1050 <clear_memory@plt>
cmp    $0x0,%eax
jne    11e8 <send_segment+0x98>
movl   $0x0,-0x4(%rbp)
jmp    1269 <send_segment+0x119>
jmp    123f <send_segment+0xef>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
add    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%eax
add    %rax,%rdx
mov    -0x28(%rbp),%ecx
call   1070 <load_memory@plt>
cmp    $0x0,%eax
jne    123a <send_segment+0xea>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <fix_connection@plt>
cmp    $0x0,%eax
jne    1235 <send_segment+0xe5>
movl   $0x0,-0x4(%rbp)
jmp    1269 <send_segment+0x119>
jmp    123a <send_segment+0xea>
jmp    123f <send_segment+0xef>
cmpl   $0x0,-0x24(%rbp)
je     1252 <send_segment+0x102>
mov    -0x28(%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    1176 <send_segment+0x26>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_segment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,(%rsp)
mov    %esi,%r12d
mov    %edi,%r13d
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ecx,%esi
call   1060 <print_progress_bar@plt>
mov    $0x1,%eax
test   %r14d,%r14d
jne    119e <send_segment+0x4e>
jmp    11f1 <send_segment+0xa1>
nop
mov    %ebp,%edx
call   1050 <clear_memory@plt>
test   %eax,%eax
je     11e8 <send_segment+0x98>
add    %ebx,%ebp
mov    %ebp,%ebx
mov    %ebx,%edi
mov    %r14d,%esi
call   1060 <print_progress_bar@plt>
cmp    %r14d,%ebx
jae    11ec <send_segment+0x9c>
mov    %r14d,%ebp
sub    %ebx,%ebp
cmp    $0x400,%ebp
mov    $0x400,%eax
cmovae %eax,%ebp
mov    %ebx,%r15d
add    (%rsp),%r15
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <is_empty@plt>
lea    (%rbx,%r12,1),%esi
mov    %r13d,%edi
test   %rax,%rax
jne    1180 <send_segment+0x30>
mov    %r15,%rdx
mov    %ebp,%ecx
call   1070 <load_memory@plt>
test   %eax,%eax
jne    118b <send_segment+0x3b>
mov    %r13d,%edi
call   1040 <fix_connection@plt>
test   %eax,%eax
jne    118f <send_segment+0x3f>
xor    %eax,%eax
jmp    11f1 <send_segment+0xa1>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send_segment(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
%12 = 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
%13 = load i32, i32* %9
%14 = call i32 @print_progress_bar(i32 0, i32 %13)
br label %15
15: ; preds = %70, %4
%16 = load i32, i32* %10
%17 = load i32, i32* %9
%18 = icmp ult i32 %16, %17
br i1 %18, label %19, label %74
19: ; preds = %15
store i32 1, i32* %11
%20 = load i32, i32* %9
%21 = load i32, i32* %10
%22 = sub i32 %20, %21
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp ugt i32 %23, 1024
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 1024, i32* %12
br label %26
26: ; preds = %25, %19
%27 = load i8*, i8** %8
%28 = load i32, i32* %10
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %12
%32 = call i64 @is_empty(i8* %30, i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %44
34: ; preds = %26
%35 = load i32, i32* %6
%36 = load i32, i32* %7
%37 = load i32, i32* %10
%38 = add i32 %36, %37
%39 = load i32, i32* %12
%40 = call i32 @clear_memory(i32 %35, i32 %38, i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %34
store i32 0, i32* %5
br label %75
43: ; preds = %34
br label %63
44: ; preds = %26
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = load i32, i32* %10
%48 = add i32 %46, %47
%49 = load i8*, i8** %8
%50 = load i32, i32* %10
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i32, i32* %12
%54 = call i32 @load_memory(i32 %45, i32 %48, i8* %52, i32 %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %62, label %56
56: ; preds = %44
store i32 0, i32* %11
%57 = load i32, i32* %6
%58 = call i32 @fix_connection(i32 %57)
%59 = icmp ne i32 %58, 0
br i1 %59, label %61, label %60
60: ; preds = %56
store i32 0, i32* %5
br label %75
61: ; preds = %56
br label %62
62: ; preds = %61, %44
br label %63
63: ; preds = %62, %43
%64 = load i32, i32* %11
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i32, i32* %12
%68 = load i32, i32* %10
%69 = add i32 %68, %67
store i32 %69, i32* %10
br label %70
70: ; preds = %66, %63
%71 = load i32, i32* %10
%72 = load i32, i32* %9
%73 = call i32 @print_progress_bar(i32 %71, i32 %72)
br label %15
74: ; preds = %15
store i32 1, i32* %5
br label %75
75: ; preds = %74, %60, %42
%76 = load i32, i32* %5
ret i32 %76
}

LLVM IR (-O2):
define i32 @send_segment(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i32 @print_progress_bar(i32 0, i32 %3)
%6 = icmp eq i32 %3, 0
br i1 %6, label %32, label %7
7: ; preds = %4, %28
%8 = phi i32 [ %29, %28 ], [ 0, %4 ]
%9 = sub i32 %3, %8
%10 = icmp ult i32 %9, 1024
%11 = select i1 %10, i32 %9, i32 1024
%12 = zext 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 %32, label %26
20: ; preds = %7
%21 = tail call i32 @load_memory(i32 %0, i32 %16, i8* %13, i32 %11)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i32 @fix_connection(i32 %0)
%25 = icmp eq i32 %24, 0
br i1 %25, label %32, label %28
26: ; preds = %20, %17
%27 = add i32 %11, %8
br label %28
28: ; preds = %23, %26
%29 = phi i32 [ %27, %26 ], [ %8, %23 ]
%30 = tail call i32 @print_progress_bar(i32 %29, i32 %3)
%31 = icmp ult i32 %29, %3
br i1 %31, label %7, label %32
32: ; preds = %28, %17, %23, %4
%33 = phi i32 [ 1, %4 ], [ 0, %23 ], [ 0, %17 ], [ 1, %28 ]
ret i32 %33
}