#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ getAck(int *, int);
int /*<<< orphan*/ msleep(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sendlong(int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tx(int *, int);
int upload(uint8_t *dlbuf, int count, int type) {
  int n = 0;
  int rc = 0;
  int wv = 100;
  uint32_t data = 0;
  uint8_t buf[1];
  int longcount = count / 4;
  if (sendlong(type) == 0) {
    printf("sendlong type failed.\n");
    return 1;
  }
  if (sendlong(longcount) == 0) {
    printf("sendlong count failed.\n");
    return 1;
  }
  printf("Writing %d bytes to %s.\n", count,
         (type == 1) ? "Propeller RAM" : "EEPROM");
  msleep(100);
  for (n = 0; n < count; n += 4) {
    data = dlbuf[n] | (dlbuf[n + 1] << 8) | (dlbuf[n + 2] << 16) |
           (dlbuf[n + 3] << 24);
    if (sendlong(data) == 0) {
      printf("sendlong error");
      return 1;
    }
  }
  msleep(150);
  buf[0] = 0xF9;
  printf("Verifying ... ");
  fflush(stdout);
  for (n = 0; n < wv; n++) {
    tx(buf, 1);
    getAck(&rc, 100);
    if (rc)
      break;
  }
  if (n >= wv)
    printf("Upload Timeout Error!\n");
  else
    printf("Upload OK!\n");
  return 0;
}


x86 assembly (clang, -O0):
<upload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x64,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <sendlong@plt>
cmp    $0x0,%rax
jne    11c8 <upload+0x68>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1378 <upload+0x218>
mov    -0x30(%rbp),%edi
call   1050 <sendlong@plt>
cmp    $0x0,%rax
jne    11f4 <upload+0x94>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1378 <upload+0x218>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%ecx
lea    0xe55(%rip),%rdx
lea    0xe40(%rip),%rax
cmp    $0x1,%ecx
cmove  %rax,%rdx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x64,%edi
call   1030 <msleep@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12c6 <upload+0x166>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
mov    (%rcx,%rdx,4),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%edx
add    $0x2,%edx
movslq %edx,%rdx
mov    (%rcx,%rdx,4),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%edx
add    $0x3,%edx
movslq %edx,%rdx
mov    (%rcx,%rdx,4),%ecx
shl    $0x18,%ecx
or     %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <sendlong@plt>
cmp    $0x0,%rax
jne    12b3 <upload+0x153>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1378 <upload+0x218>
jmp    12b8 <upload+0x158>
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    122e <upload+0xce>
mov    $0x96,%edi
call   1030 <msleep@plt>
movl   $0xf9,-0x2c(%rbp)
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1344 <upload+0x1e4>
lea    -0x2c(%rbp),%rdi
mov    $0x1,%esi
call   1080 <tx@plt>
lea    -0x20(%rbp),%rdi
mov    $0x64,%esi
call   1070 <getAck@plt>
cmpl   $0x0,-0x20(%rbp)
je     1331 <upload+0x1d1>
jmp    1344 <upload+0x1e4>
jmp    1336 <upload+0x1d6>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12fa <upload+0x19a>
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     1363 <upload+0x203>
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1371 <upload+0x211>
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upload>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
movl   $0x0,(%rsp)
mov    %edx,%edi
call   1060 <sendlong@plt>
test   %rax,%rax
je     128e <upload+0x11e>
lea    0x3(%r14),%edi
test   %r14d,%r14d
cmovns %r14d,%edi
sar    $0x2,%edi
call   1060 <sendlong@plt>
test   %rax,%rax
je     1297 <upload+0x127>
cmp    $0x1,%ebp
lea    0xe5d(%rip),%rax
lea    0xe64(%rip),%rdx
cmove  %rax,%rdx
lea    0xe32(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x64,%ebp
mov    $0x64,%edi
call   1040 <msleep@plt>
test   %r14d,%r14d
jle    1227 <upload+0xb7>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    0x4(%r15,%rbx,4),%eax
mov    0x8(%r15,%rbx,4),%ecx
shl    $0x8,%eax
or     (%r15,%rbx,4),%eax
shl    $0x10,%ecx
mov    0xc(%r15,%rbx,4),%edi
shl    $0x18,%edi
or     %ecx,%edi
or     %eax,%edi
call   1060 <sendlong@plt>
test   %rax,%rax
je     12aa <upload+0x13a>
add    $0x4,%rbx
cmp    %r14d,%ebx
jl     11f0 <upload+0x80>
mov    $0x96,%edi
call   1040 <msleep@plt>
movl   $0xf9,0x4(%rsp)
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0x4(%rsp),%r14
mov    %rsp,%rbx
nopl   (%rax)
mov    %r14,%rdi
mov    $0x1,%esi
call   1090 <tx@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
call   1080 <getAck@plt>
cmpl   $0x0,(%rsp)
jne    12bf <upload+0x14f>
add    $0xffffffff,%ebp
jne    1260 <upload+0xf0>
lea    0xdcb(%rip),%rdi
jmp    12c6 <upload+0x156>
lea    0xdef(%rip),%rdi
jmp    129e <upload+0x12e>
lea    0xdcf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    12cd <upload+0x15d>
lea    0xd7d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    12cd <upload+0x15d>
lea    0xd86(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"sendlong type failed.\0A\00"
@.str.1 = [24 x i8] c"sendlong count failed.\0A\00"
@.str.2 = [25 x i8] c"Writing %d bytes to %s.\0A\00"
@.str.3 = [14 x i8] c"Propeller RAM\00"
@.str.4 = [7 x i8] c"EEPROM\00"
@.str.5 = [15 x i8] c"sendlong error\00"
@.str.6 = [15 x i8] c"Verifying ... \00"
@stdout = global i32 0
@.str.7 = [23 x i8] c"Upload Timeout Error!\0A\00"
@.str.8 = [12 x i8] c"Upload OK!\0A\00"
define i32 @upload(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1 x i32]
%13 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 100, i32* %10
store i32 0, i32* %11
%14 = load i32, i32* %6
%15 = sdiv i32 %14, 4
store i32 %15, i32* %13
%16 = load i32, i32* %7
%17 = call i64 @sendlong(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %108
21: ; preds = %3
%22 = load i32, i32* %13
%23 = call i64 @sendlong(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %108
27: ; preds = %21
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 1
%31 = zext i1 %30 to i64
%32 = select i1 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %28, i8* %32)
%34 = call i32 @msleep(i32 100)
store i32 0, i32* %8
br label %35
35: ; preds = %75, %27
%36 = load i32, i32* %8
%37 = load i32, i32* %6
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %78
39: ; preds = %35
%40 = load i32*, i32** %5
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = load i32*, i32** %5
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %45, i64 %48
%50 = load i32, i32* %49
%51 = shl i32 %50, 8
%52 = or i32 %44, %51
%53 = load i32*, i32** %5
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 2
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %53, i64 %56
%58 = load i32, i32* %57
%59 = shl i32 %58, 16
%60 = or i32 %52, %59
%61 = load i32*, i32** %5
%62 = load i32, i32* %8
%63 = add nsw i32 %62, 3
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %61, i64 %64
%66 = load i32, i32* %65
%67 = shl i32 %66, 24
%68 = or i32 %60, %67
store i32 %68, i32* %11
%69 = load i32, i32* %11
%70 = call i64 @sendlong(i32 %69)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %39
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %4
br label %108
74: ; preds = %39
br label %75
75: ; preds = %74
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 4
store i32 %77, i32* %8
br label %35
78: ; preds = %35
%79 = call i32 @msleep(i32 150)
%80 = getelementptr inbounds [1 x i32], [1 x i32]* %12, i64 0, i64 0
store i32 249, i32* %80
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%82 = load i32, i32* @stdout
%83 = call i32 @fflush(i32 %82)
store i32 0, i32* %8
br label %84
84: ; preds = %96, %78
%85 = load i32, i32* %8
%86 = load i32, i32* %10
%87 = icmp slt i32 %85, %86
br i1 %87, label %88, label %99
88: ; preds = %84
%89 = getelementptr inbounds [1 x i32], [1 x i32]* %12, i64 0, i64 0
%90 = call i32 @tx(i32* %89, i32 1)
%91 = call i32 @getAck(i32* %9, i32 100)
%92 = load i32, i32* %9
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %88
br label %99
95: ; preds = %88
br label %96
96: ; preds = %95
%97 = load i32, i32* %8
%98 = add nsw i32 %97, 1
store i32 %98, i32* %8
br label %84
99: ; preds = %94, %84
%100 = load i32, i32* %8
%101 = load i32, i32* %10
%102 = icmp sge i32 %100, %101
br i1 %102, label %103, label %105
103: ; preds = %99
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %107
105: ; preds = %99
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %107
107: ; preds = %105, %103
store i32 0, i32* %4
br label %108
108: ; preds = %107, %72, %25, %19
%109 = load i32, i32* %4
ret i32 %109
}

LLVM IR (-O2):
@.str.2 = [25 x i8] c"Writing %d bytes to %s.\0A\00"
@.str.3 = [14 x i8] c"Propeller RAM\00"
@.str.4 = [7 x i8] c"EEPROM\00"
@.str.5 = [15 x i8] c"sendlong error\00"
@.str.6 = [15 x i8] c"Verifying ... \00"
@stdout = global i32 0
@str = [11 x i8] c"Upload OK!\00"
@str.9 = [22 x i8] c"Upload Timeout Error!\00"
@str.10 = [23 x i8] c"sendlong count failed.\00"
@str.11 = [22 x i8] c"sendlong type failed.\00"
define i32 @upload(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca [1 x i32]
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = bitcast [1 x i32]* %5 to i8*
%8 = tail call i64 @sendlong(i32 %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.11, i64 0, i64 0))
br label %70
12: ; preds = %3
%13 = sdiv i32 %1, 4
%14 = tail call i64 @sendlong(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
br label %70
18: ; preds = %12
%19 = icmp eq i32 %2, 1
%20 = select i1 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %1, i8* %20)
%22 = tail call i32 @msleep(i32 100)
%23 = icmp sgt i32 %1, 0
br i1 %23, label %27, label %51
24: ; preds = %27
%25 = trunc i64 %48 to i32
%26 = icmp slt i32 %25, %1
br i1 %26, label %27, label %51
27: ; preds = %18, %24
%28 = phi i64 [ %48, %24 ], [ 0, %18 ]
%29 = getelementptr inbounds i32, i32* %0, i64 %28
%30 = load i32, i32* %29
%31 = or i64 %28, 1
%32 = getelementptr inbounds i32, i32* %0, i64 %31
%33 = load i32, i32* %32
%34 = shl i32 %33, 8
%35 = or i32 %34, %30
%36 = or i64 %28, 2
%37 = getelementptr inbounds i32, i32* %0, i64 %36
%38 = load i32, i32* %37
%39 = shl i32 %38, 16
%40 = or i32 %35, %39
%41 = or i64 %28, 3
%42 = getelementptr inbounds i32, i32* %0, i64 %41
%43 = load i32, i32* %42
%44 = shl i32 %43, 24
%45 = or i32 %40, %44
%46 = tail call i64 @sendlong(i32 %45)
%47 = icmp eq i64 %46, 0
%48 = add nuw i64 %28, 4
br i1 %47, label %49, label %24
49: ; preds = %27
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %70
51: ; preds = %24, %18
%52 = tail call i32 @msleep(i32 150)
%53 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
store i32 249, i32* %53
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%55 = load i32, i32* @stdout
%56 = tail call i32 @fflush(i32 %55)
br label %60
57: ; preds = %60
%58 = add nuw nsw i32 %61, 1
%59 = icmp eq i32 %58, 100
br i1 %59, label %66, label %60
60: ; preds = %51, %57
%61 = phi i32 [ 0, %51 ], [ %58, %57 ]
%62 = call i32 @tx(i32* %53, i32 1)
%63 = call i32 @getAck(i32* %4, i32 100)
%64 = load i32, i32* %4
%65 = icmp eq i32 %64, 0
br i1 %65, label %57, label %68
66: ; preds = %57
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0))
br label %70
68: ; preds = %60
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %70
70: ; preds = %66, %68, %49, %16, %10
%71 = phi i32 [ 1, %10 ], [ 1, %16 ], [ 1, %49 ], [ 0, %68 ], [ 0, %66 ]
ret i32 %71
}