#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*/ command;
typedef int /*<<< orphan*/ __u_char;

/* Variables and functions */
int /*<<< orphan*/ cbmarch_listen(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ cbmarch_raw_write(int, ...);
int /*<<< orphan*/ cbmarch_unlisten(int);
int cbm_upload(int HandleDevice, __u_char DeviceAddress, int DriveMemAddress,
               const void *Program, size_t Size) {
  const char *bufferToProgram = Program;
  unsigned char command[] = {'M', '-', 'W', ' ', ' ', ' '};
  size_t i;
  int rv = 0;
  int c;
  ;
  ;
  for (i = 0; i < Size; i += 32) {
    cbmarch_listen(HandleDevice, DeviceAddress, 15);
    c = Size - i;
    if (c > 32) {
      c = 32;
    }
    command[3] = (unsigned char)(DriveMemAddress % 256);
    command[4] = (unsigned char)(DriveMemAddress / 256);
    command[5] = (unsigned char)c;
    cbmarch_raw_write(HandleDevice, command, sizeof(command));
    cbmarch_raw_write(HandleDevice, bufferToProgram, c);
    DriveMemAddress += c;
    bufferToProgram += c;
    rv += c;
    cbmarch_unlisten(HandleDevice);
  }
  return rv;
}


x86 assembly (clang, -O0):
<cbm_upload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0xea9(%rip),%eax
mov    %eax,-0x2e(%rbp)
mov    0xea3(%rip),%ax
mov    %ax,-0x2a(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1232 <cbm_upload+0x102>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0xf,%edx
call   1030 <cbmarch_listen@plt>
mov    -0x20(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %eax,-0x40(%rbp)
cmpl   $0x20,-0x40(%rbp)
jle    11ae <cbm_upload+0x7e>
movl   $0x20,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x2b(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %al,-0x2a(%rbp)
mov    -0x40(%rbp),%eax
mov    %al,-0x29(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x2e(%rbp),%rsi
mov    $0x6,%edx
mov    $0x0,%al
call   1050 <cbmarch_raw_write@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%edx
mov    $0x0,%al
call   1050 <cbmarch_raw_write@plt>
mov    -0x40(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x40(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <cbmarch_unlisten@plt>
mov    -0x38(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x38(%rbp)
jmp    1174 <cbm_upload+0x44>
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbm_upload>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movw   $0x2020,0x8(%rsp)
movl   $0x20572d4d,0x4(%rsp)
20
mov    %r8,0x10(%rsp)
test   %r8,%r8
je     1207 <cbm_upload+0xd7>
mov    %edx,%ebx
mov    %edi,%r12d
mov    0x10(%rsp),%rax
mov    %eax,%r14d
xor    %ebp,%ebp
xor    %r13d,%r13d
mov    %rcx,%r15
mov    %esi,0xc(%rsp)
mov    %edi,(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    0xc(%rsp),%esi
mov    $0xf,%edx
call   1030 <cbmarch_listen@plt>
cmp    $0x20,%r14d
mov    $0x20,%r12d
cmovl  %r14d,%r12d
mov    %bl,0x7(%rsp)
lea    0xff(%rbx),%eax
test   %ebx,%ebx
cmovns %ebx,%eax
mov    %ah,0x8(%rsp)
mov    %r12b,0x9(%rsp)
mov    $0x6,%edx
mov    (%rsp),%edi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1050 <cbmarch_raw_write@plt>
mov    (%rsp),%edi
mov    %r15,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <cbmarch_raw_write@plt>
add    %r12d,%ebx
movslq %r12d,%rax
mov    (%rsp),%r12d
add    %rax,%r15
add    %eax,%ebp
mov    %r12d,%edi
call   1040 <cbmarch_unlisten@plt>
add    $0x20,%r13
add    $0xffffffe0,%r14d
cmp    0x10(%rsp),%r13
jb     1180 <cbm_upload+0x50>
jmp    1209 <cbm_upload+0xd9>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.cbm_upload.command = [6 x i8] c"M-W "
define i32 @cbm_upload(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca [6 x i8]
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%16 = load i8*, i8** %9
store i8* %16, i8** %11
%17 = bitcast [6 x i8]* %12 to i8*
store i32 0, i32* %14
store i64 0, i64* %13
br label %18
18: ; preds = %64, %5
%19 = load i64, i64* %13
%20 = load i64, i64* %10
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %67
22: ; preds = %18
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = call i32 @cbmarch_listen(i32 %23, i32 %24, i32 15)
%26 = load i64, i64* %10
%27 = load i64, i64* %13
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
store i32 %29, i32* %15
%30 = load i32, i32* %15
%31 = icmp sgt i32 %30, 32
br i1 %31, label %32, label %33
32: ; preds = %22
store i32 32, i32* %15
br label %33
33: ; preds = %32, %22
%34 = load i32, i32* %8
%35 = srem i32 %34, 256
%36 = trunc i32 %35 to i8
%37 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 3
store i8 %36, i8* %37
%38 = load i32, i32* %8
%39 = sdiv i32 %38, 256
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 4
store i8 %40, i8* %41
%42 = load i32, i32* %15
%43 = trunc i32 %42 to i8
%44 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 5
store i8 %43, i8* %44
%45 = load i32, i32* %6
%46 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 0
%47 = call i32 (i32, ...) @cbmarch_raw_write(i32 %45, i8* %46, i64 6)
%48 = load i32, i32* %6
%49 = load i8*, i8** %11
%50 = load i32, i32* %15
%51 = call i32 (i32, ...) @cbmarch_raw_write(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* %15
%53 = load i32, i32* %8
%54 = add nsw i32 %53, %52
store i32 %54, i32* %8
%55 = load i32, i32* %15
%56 = load i8*, i8** %11
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8* %58, i8** %11
%59 = load i32, i32* %15
%60 = load i32, i32* %14
%61 = add nsw i32 %60, %59
store i32 %61, i32* %14
%62 = load i32, i32* %6
%63 = call i32 @cbmarch_unlisten(i32 %62)
br label %64
64: ; preds = %33
%65 = load i64, i64* %13
%66 = add i64 %65, 32
store i64 %66, i64* %13
br label %18
67: ; preds = %18
%68 = load i32, i32* %14
ret i32 %68
}

LLVM IR (-O2):
@__const.cbm_upload.command = [6 x i8] c"M-W "
define i32 @cbm_upload(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca [6 x i8]
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%8 = icmp eq i64 %4, 0
br i1 %8, label %36, label %9
9: ; preds = %5
%10 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 3
%11 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 4
%12 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 5
br label %13
13: ; preds = %9, %13
%14 = phi i32 [ %2, %9 ], [ %29, %13 ]
%15 = phi i32 [ 0, %9 ], [ %32, %13 ]
%16 = phi i64 [ 0, %9 ], [ %34, %13 ]
%17 = phi i8* [ %3, %9 ], [ %31, %13 ]
%18 = call i32 @cbmarch_listen(i32 %0, i32 %1, i32 15)
%19 = sub i64 %4, %16
%20 = trunc i64 %19 to i32
%21 = icmp slt i32 %20, 32
%22 = select i1 %21, i32 %20, i32 32
%23 = trunc i32 %14 to i8
store i8 %23, i8* %10
%24 = sdiv i32 %14, 256
%25 = trunc i32 %24 to i8
store i8 %25, i8* %11
%26 = trunc i32 %22 to i8
store i8 %26, i8* %12
%27 = call i32 (i32, ...) @cbmarch_raw_write(i32 %0, i8* %7, i64 6)
%28 = call i32 (i32, ...) @cbmarch_raw_write(i32 %0, i8* %17, i32 %22)
%29 = add nsw i32 %22, %14
%30 = sext i32 %22 to i64
%31 = getelementptr inbounds i8, i8* %17, i64 %30
%32 = add nsw i32 %22, %15
%33 = call i32 @cbmarch_unlisten(i32 %0)
%34 = add i64 %16, 32
%35 = icmp ult i64 %34, %4
br i1 %35, label %13, label %36
36: ; preds = %13, %5
%37 = phi i32 [ 0, %5 ], [ %32, %13 ]
ret i32 %37
}