#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 char __u_char;

/* Variables and functions */
int /*<<< orphan*/ cbm_listen(int, char, int);
int /*<<< orphan*/ cbm_unlisten(int);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int);
int /*<<< orphan*/ write(int, char *, int);
int cbm_upload(int f, __u_char dev, int adr, __u_char *prog, int size) {
  int c, i, rv = 0;
  char cmd[40];
  for (i = 0; i < size; i += 32) {
    cbm_listen(f, dev, 15);
    c = size - i;
    if (c > 32)
      c = 32;
    sprintf(cmd, "M-W%c%c%c", adr % 256, adr / 256, c);
    adr += c;
    write(f, cmd, 6);
    write(f, prog, c);
    prog += c;
    rv += c;
    cbm_unlisten(f);
  }
  return rv;
}


x86 assembly (clang, -O0):
<cbm_upload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    122c <cbm_upload+0xec>
mov    -0x4(%rbp),%edi
mov    $0xf,%edx
movsbl -0x5(%rbp),%esi
call   1050 <cbm_listen@plt>
mov    -0x1c(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x20,-0x20(%rbp)
jle    11a1 <cbm_upload+0x61>
movl   $0x20,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %edx,-0x54(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x54(%rbp),%edx
mov    %eax,%ecx
mov    -0x20(%rbp),%r8d
lea    0xe32(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <cbm_unlisten@plt>
mov    -0x24(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x24(%rbp)
jmp    116a <cbm_upload+0x2a>
mov    -0x28(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbm_upload>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,0x4(%rsp)
test   %r8d,%r8d
jle    1221 <cbm_upload+0xe1>
mov    %edx,%ebp
mov    %edi,%r14d
xor    %ebx,%ebx
movsbl %sil,%eax
mov    %eax,0x8(%rsp)
mov    0x4(%rsp),%r13d
xor    %r15d,%r15d
mov    %rcx,%r12
mov    %edi,(%rsp)
nopl   0x0(%rax)
mov    %ebx,0xc(%rsp)
mov    %r14d,%edi
mov    0x8(%rsp),%esi
mov    $0xf,%edx
call   1050 <cbm_listen@plt>
cmp    $0x20,%r13d
mov    $0x20,%r14d
cmovl  %r13d,%r14d
lea    0xff(%rbp),%ecx
test   %ebp,%ebp
cmovns %ebp,%ecx
mov    %ecx,%eax
and    $0xffffff00,%eax
mov    %ebp,%edx
sub    %eax,%edx
sar    $0x8,%ecx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
lea    0xe35(%rip),%rsi
mov    %r14d,%r8d
call   1060 <sprintf@plt>
add    %r14d,%ebp
mov    (%rsp),%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    (%rsp),%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
movslq %r14d,%rax
mov    0xc(%rsp),%ebx
mov    (%rsp),%r14d
add    %rax,%r12
add    %eax,%ebx
mov    %r14d,%edi
call   1040 <cbm_unlisten@plt>
add    $0x20,%r15d
add    $0xffffffe0,%r13d
cmp    0x4(%rsp),%r15d
jl     1180 <cbm_upload+0x40>
jmp    1223 <cbm_upload+0xe3>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"M-W%c%c%c\00"
define i32 @cbm_upload(i32 %0, i8 signext %1, i32 %2, i8* nocapture %3, i32 %4) {
%6 = alloca [40 x i8]
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%8 = icmp sgt i32 %4, 0
br i1 %8, label %9, label %30
9: ; preds = %5, %9
%10 = phi i32 [ %26, %9 ], [ 0, %5 ]
%11 = phi i32 [ %28, %9 ], [ 0, %5 ]
%12 = phi i32 [ %21, %9 ], [ %2, %5 ]
%13 = phi i8* [ %25, %9 ], [ %3, %5 ]
%14 = tail call i32 @cbm_listen(i32 %0, i8 signext %1, i32 15)
%15 = sub nsw i32 %4, %11
%16 = icmp slt i32 %15, 32
%17 = select i1 %16, i32 %15, i32 32
%18 = srem i32 %12, 256
%19 = sdiv i32 %12, 256
%20 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %19, i32 %17)
%21 = add nsw i32 %17, %12
%22 = call i32 @write(i32 %0, i8* %7, i32 6)
%23 = tail call i32 @write(i32 %0, i8* %13, i32 %17)
%24 = sext i32 %17 to i64
%25 = getelementptr inbounds i8, i8* %13, i64 %24
%26 = add nsw i32 %17, %10
%27 = tail call i32 @cbm_unlisten(i32 %0)
%28 = add nuw nsw i32 %11, 32
%29 = icmp slt i32 %28, %4
br i1 %29, label %9, label %30
30: ; preds = %9, %5
%31 = phi i32 [ 0, %5 ], [ %26, %9 ]
ret i32 %31
}