#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*/ printf(char *, int, char *, unsigned int);
int /*<<< orphan*/ send_data_packet(int, unsigned int, unsigned char *, int);
int download_words(int is_data, unsigned addr, unsigned char *buf, int buflen) {
  while (buflen >= 254) {
    if (!send_data_packet(is_data, addr, buf, 254)) {
      printf("Error downloading %d bytes of %s to 0x%x\n", 254,
             (is_data ? "data" : "code"), addr);
      return 0;
    }
    addr += 254;
    buf += 254;
    buflen -= 254;
  }
  if (buflen && !send_data_packet(is_data, addr, buf, buflen)) {
    printf("Error downloading %d bytes of %s to 0x%x\n", buflen,
           (is_data ? "data" : "code"), addr);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<download_words>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0xfe,-0x1c(%rbp)
jl     11c0 <download_words+0xa0>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    $0xfe,%ecx
call   1040 <send_data_packet@plt>
cmp    $0x0,%eax
jne    1197 <download_words+0x77>
mov    -0x8(%rbp),%ecx
lea    0xec6(%rip),%rdx
lea    0xeba(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0xc(%rbp),%ecx
lea    0xe7f(%rip),%rdi
mov    $0xfe,%esi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1222 <download_words+0x102>
mov    -0xc(%rbp),%eax
add    $0xfe,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0xfe,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0xfe,%eax
mov    %eax,-0x1c(%rbp)
jmp    1135 <download_words+0x15>
cmpl   $0x0,-0x1c(%rbp)
je     121b <download_words+0xfb>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <send_data_packet@plt>
cmp    $0x0,%eax
jne    121b <download_words+0xfb>
mov    -0x1c(%rbp),%esi
mov    -0x8(%rbp),%ecx
lea    0xe3d(%rip),%rdx
lea    0xe31(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0xc(%rbp),%ecx
lea    0xdf6(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1222 <download_words+0x102>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<download_words>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebx
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %edi,%r14d
cmp    $0xfe,%ecx
jl     1179 <download_words+0x59>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15d,%esi
mov    %rbp,%rdx
mov    $0xfe,%ecx
call   1040 <send_data_packet@plt>
test   %eax,%eax
je     11bc <download_words+0x9c>
add    $0xfe,%r15d
add    $0xfe,%rbp
lea    -0xfe(%rbx),%r12d
cmp    $0x1fb,%ebx
mov    %r12d,%ebx
jg     1140 <download_words+0x20>
jmp    117c <download_words+0x5c>
mov    %ebx,%r12d
mov    $0x1,%ebx
test   %r12d,%r12d
je     11e7 <download_words+0xc7>
mov    %r14d,%edi
mov    %r15d,%esi
mov    %rbp,%rdx
mov    %r12d,%ecx
call   1040 <send_data_packet@plt>
test   %eax,%eax
jne    11e7 <download_words+0xc7>
test   %r14d,%r14d
lea    0xe8a(%rip),%rax
lea    0xe7e(%rip),%rdx
cmove  %rax,%rdx
lea    0xe49(%rip),%rdi
mov    %r12d,%esi
jmp    11dd <download_words+0xbd>
test   %r14d,%r14d
lea    0xe69(%rip),%rax
lea    0xe5d(%rip),%rdx
cmove  %rax,%rdx
lea    0xe28(%rip),%rdi
mov    $0xfe,%esi
mov    %r15d,%ecx
call   1030 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Error downloading %d bytes of %s to 0x%x\0A\00"
@.str.1 = [5 x i8] c"data\00"
@.str.2 = [5 x i8] c"code\00"
define i32 @download_words(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
br label %10
10: ; preds = %26, %4
%11 = load i32, i32* %9
%12 = icmp sge i32 %11, 254
br i1 %12, label %13, label %33
13: ; preds = %10
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = load i8*, i8** %8
%17 = call i32 @send_data_packet(i32 %14, i32 %15, i8* %16, i32 254)
%18 = icmp ne i32 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %13
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
%22 = zext i1 %21 to i64
%23 = select i1 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%24 = load i32, i32* %7
%25 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 254, i8* %23, i32 %24)
store i32 0, i32* %5
br label %52
26: ; preds = %13
%27 = load i32, i32* %7
%28 = add i32 %27, 254
store i32 %28, i32* %7
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i64 254
store i8* %30, i8** %8
%31 = load i32, i32* %9
%32 = sub nsw i32 %31, 254
store i32 %32, i32* %9
br label %10
33: ; preds = %10
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %33
%37 = load i32, i32* %6
%38 = load i32, i32* %7
%39 = load i8*, i8** %8
%40 = load i32, i32* %9
%41 = call i32 @send_data_packet(i32 %37, i32 %38, i8* %39, i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %36
%44 = load i32, i32* %9
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
%47 = zext i1 %46 to i64
%48 = select i1 %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%49 = load i32, i32* %7
%50 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %44, i8* %48, i32 %49)
store i32 0, i32* %5
br label %52
51: ; preds = %36, %33
store i32 1, i32* %5
br label %52
52: ; preds = %51, %43, %19
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@.str = [42 x i8] c"Error downloading %d bytes of %s to 0x%x\0A\00"
@.str.1 = [5 x i8] c"data\00"
@.str.2 = [5 x i8] c"code\00"
define i32 @download_words(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp sgt i32 %3, 253
br i1 %5, label %6, label %21
6: ; preds = %4, %16
%7 = phi i32 [ %19, %16 ], [ %3, %4 ]
%8 = phi i8* [ %18, %16 ], [ %2, %4 ]
%9 = phi i32 [ %17, %16 ], [ %1, %4 ]
%10 = tail call i32 @send_data_packet(i32 %0, i32 %9, i8* %8, i32 254)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = icmp eq i32 %0, 0
%14 = select i1 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 254, i8* %14, i32 %9)
br label %33
16: ; preds = %6
%17 = add i32 %9, 254
%18 = getelementptr inbounds i8, i8* %8, i64 254
%19 = add nsw i32 %7, -254
%20 = icmp sgt i32 %7, 507
br i1 %20, label %6, label %21
21: ; preds = %16, %4
%22 = phi i32 [ %1, %4 ], [ %17, %16 ]
%23 = phi i8* [ %2, %4 ], [ %18, %16 ]
%24 = phi i32 [ %3, %4 ], [ %19, %16 ]
%25 = icmp eq i32 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %21
%27 = tail call i32 @send_data_packet(i32 %0, i32 %22, i8* %23, i32 %24)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = icmp eq i32 %0, 0
%31 = select i1 %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0)
%32 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %24, i8* %31, i32 %22)
br label %33
33: ; preds = %21, %26, %29, %12
%34 = phi i32 [ 0, %12 ], [ 0, %29 ], [ 1, %26 ], [ 1, %21 ]
ret i32 %34
}