#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 encodelong(int, int *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ getAck(int *);
scalar_t__ malloc(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;
  int sendone = 1;
  int position = 0;
  int longcount = count / 4;
  uint8_t *bigbuf;
  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");
  bigbuf = (uint8_t *)malloc(count * 11 + 1);
  for (n = 0; n < count; n += 4) {
    data = dlbuf[n] | (dlbuf[n + 1] << 8) | (dlbuf[n + 2] << 16) |
           (dlbuf[n + 3] << 24);
    if (sendone) {
      if (sendlong(data) == 0) {
        printf("sendlong error");
        free(bigbuf);
        return 1;
      }
    } else
      position = encodelong(data, bigbuf, position);
  }
  if (!sendone)
    tx(bigbuf, position);
  msleep(150);
  sendlong(0xF9);
  printf("Verifying ... ");
  fflush(stdout);
  for (n = 0; n < wv; n++) {
    msleep(25);
    getAck(&rc);
    if (rc)
      break;
  }
  if (n >= wv)
    printf("Upload Timeout Error!\n");
  else
    printf("Upload OK!\n");
  free(bigbuf);
  return 0;
}


x86 assembly (clang, -O0):
<upload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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)
movl   $0x1,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <sendlong@plt>
cmp    $0x0,%rax
jne    1206 <upload+0x76>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ff <upload+0x26f>
mov    -0x34(%rbp),%edi
call   1060 <sendlong@plt>
cmp    $0x0,%rax
jne    1232 <upload+0xa2>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ff <upload+0x26f>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%ecx
lea    0xe17(%rip),%rdx
lea    0xe02(%rip),%rax
cmp    $0x1,%ecx
cmove  %rax,%rdx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
imul   $0xb,-0x14(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1334 <upload+0x1a4>
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)
cmpl   $0x0,-0x2c(%rbp)
je     130f <upload+0x17f>
mov    -0x28(%rbp),%edi
call   1060 <sendlong@plt>
cmp    $0x0,%rax
jne    130a <upload+0x17a>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ff <upload+0x26f>
jmp    1321 <upload+0x191>
mov    -0x28(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   10b0 <encodelong@plt>
mov    %eax,-0x30(%rbp)
jmp    1326 <upload+0x196>
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    1272 <upload+0xe2>
cmpl   $0x0,-0x2c(%rbp)
jne    134a <upload+0x1ba>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   10a0 <tx@plt>
mov    $0x96,%edi
call   1040 <msleep@plt>
mov    $0xf9,%edi
call   1060 <sendlong@plt>
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    13c2 <upload+0x232>
mov    $0x19,%edi
call   1040 <msleep@plt>
lea    -0x20(%rbp),%rdi
call   1090 <getAck@plt>
cmpl   $0x0,-0x20(%rbp)
je     13af <upload+0x21f>
jmp    13c2 <upload+0x232>
jmp    13b4 <upload+0x224>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1381 <upload+0x1f1>
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     13e1 <upload+0x251>
lea    0xca6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13ef <upload+0x25f>
lea    0xcaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upload>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
mov    %edx,%edi
call   1070 <sendlong@plt>
test   %rax,%rax
je     12c5 <upload+0x145>
lea    0x3(%r15),%edi
test   %r15d,%r15d
cmovns %r15d,%edi
sar    $0x2,%edi
call   1070 <sendlong@plt>
test   %rax,%rax
je     12ce <upload+0x14e>
cmp    $0x1,%ebp
lea    0xe47(%rip),%rax
lea    0xe4e(%rip),%rdx
cmove  %rax,%rdx
lea    0xe1c(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    (%r15,%r15,4),%eax
lea    (%r15,%rax,2),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
test   %r15d,%r15d
jle    1243 <upload+0xc3>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    0x4(%rbx,%rbp,4),%eax
mov    0x8(%rbx,%rbp,4),%ecx
shl    $0x8,%eax
or     (%rbx,%rbp,4),%eax
shl    $0x10,%ecx
mov    0xc(%rbx,%rbp,4),%edi
shl    $0x18,%edi
or     %ecx,%edi
or     %eax,%edi
call   1070 <sendlong@plt>
test   %rax,%rax
je     12ec <upload+0x16c>
add    $0x4,%rbp
cmp    %r15d,%ebp
jl     1210 <upload+0x90>
mov    $0x96,%edi
call   1050 <msleep@plt>
mov    $0xf9,%edi
call   1070 <sendlong@plt>
lea    0xddf(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x64,%ebx
lea    0xc(%rsp),%rbp
lea    0xdc8(%rip),%r15
lea    0xdcc(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    $0x19,%edi
call   1050 <msleep@plt>
mov    %rbp,%rdi
call   10a0 <getAck@plt>
cmpl   $0x0,0xc(%rsp)
jne    12b1 <upload+0x131>
add    $0xffffffff,%ebx
jne    1290 <upload+0x110>
mov    %r12,%r15
mov    %r15,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    12df <upload+0x15f>
lea    0xdb8(%rip),%rdi
jmp    12d5 <upload+0x155>
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12da <upload+0x15a>

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 i32
%13 = alloca i32
%14 = alloca i32
%15 = 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
store i32 1, i32* %12
store i32 0, i32* %13
%16 = load i32, i32* %6
%17 = sdiv i32 %16, 4
store i32 %17, i32* %14
%18 = load i32, i32* %7
%19 = call i64 @sendlong(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = 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 %133
23: ; preds = %3
%24 = load i32, i32* %14
%25 = call i64 @sendlong(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = 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 %133
29: ; preds = %23
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 1
%33 = zext i1 %32 to i64
%34 = select i1 %32, 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)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8* %34)
%36 = load i32, i32* %6
%37 = mul nsw i32 %36, 11
%38 = add nsw i32 %37, 1
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %15
store i32 0, i32* %8
br label %41
41: ; preds = %92, %29
%42 = load i32, i32* %8
%43 = load i32, i32* %6
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %95
45: ; preds = %41
%46 = load i32*, i32** %5
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
%51 = load i32*, i32** %5
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %51, i64 %54
%56 = load i32, i32* %55
%57 = shl i32 %56, 8
%58 = or i32 %50, %57
%59 = load i32*, i32** %5
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 2
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %59, i64 %62
%64 = load i32, i32* %63
%65 = shl i32 %64, 16
%66 = or i32 %58, %65
%67 = load i32*, i32** %5
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 3
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32, i32* %67, i64 %70
%72 = load i32, i32* %71
%73 = shl i32 %72, 24
%74 = or i32 %66, %73
store i32 %74, i32* %11
%75 = load i32, i32* %12
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %86
77: ; preds = %45
%78 = load i32, i32* %11
%79 = call i64 @sendlong(i32 %78)
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %85
81: ; preds = %77
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%83 = load i32*, i32** %15
%84 = call i32 @free(i32* %83)
store i32 1, i32* %4
br label %133
85: ; preds = %77
br label %91
86: ; preds = %45
%87 = load i32, i32* %11
%88 = load i32*, i32** %15
%89 = load i32, i32* %13
%90 = call i32 @encodelong(i32 %87, i32* %88, i32 %89)
store i32 %90, i32* %13
br label %91
91: ; preds = %86, %85
br label %92
92: ; preds = %91
%93 = load i32, i32* %8
%94 = add nsw i32 %93, 4
store i32 %94, i32* %8
br label %41
95: ; preds = %41
%96 = load i32, i32* %12
%97 = icmp ne i32 %96, 0
br i1 %97, label %102, label %98
98: ; preds = %95
%99 = load i32*, i32** %15
%100 = load i32, i32* %13
%101 = call i32 @tx(i32* %99, i32 %100)
br label %102
102: ; preds = %98, %95
%103 = call i32 @msleep(i32 150)
%104 = call i64 @sendlong(i32 249)
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%106 = load i32, i32* @stdout
%107 = call i32 @fflush(i32 %106)
store i32 0, i32* %8
br label %108
108: ; preds = %119, %102
%109 = load i32, i32* %8
%110 = load i32, i32* %10
%111 = icmp slt i32 %109, %110
br i1 %111, label %112, label %122
112: ; preds = %108
%113 = call i32 @msleep(i32 25)
%114 = call i32 @getAck(i32* %9)
%115 = load i32, i32* %9
%116 = icmp ne i32 %115, 0
br i1 %116, label %117, label %118
117: ; preds = %112
br label %122
118: ; preds = %112
br label %119
119: ; preds = %118
%120 = load i32, i32* %8
%121 = add nsw i32 %120, 1
store i32 %121, i32* %8
br label %108
122: ; preds = %117, %108
%123 = load i32, i32* %8
%124 = load i32, i32* %10
%125 = icmp sge i32 %123, %124
br i1 %125, label %126, label %128
126: ; preds = %122
%127 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %130
128: ; preds = %122
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %130
130: ; preds = %128, %126
%131 = load i32*, i32** %15
%132 = call i32 @free(i32* %131)
store i32 0, i32* %4
br label %133
133: ; preds = %130, %81, %27, %21
%134 = load i32, i32* %4
ret i32 %134
}

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 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%6 = tail call i64 @sendlong(i32 %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.11, i64 0, i64 0))
br label %72
10: ; preds = %3
%11 = sdiv i32 %1, 4
%12 = tail call i64 @sendlong(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
br label %72
16: ; preds = %10
%17 = icmp eq i32 %2, 1
%18 = select i1 %17, 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)
%19 = 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* %18)
%20 = mul nsw i32 %1, 11
%21 = add nsw i32 %20, 1
%22 = tail call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i32*
%24 = icmp sgt i32 %1, 0
br i1 %24, label %28, label %53
25: ; preds = %28
%26 = trunc i64 %49 to i32
%27 = icmp slt i32 %26, %1
br i1 %27, label %28, label %53
28: ; preds = %16, %25
%29 = phi i64 [ %49, %25 ], [ 0, %16 ]
%30 = getelementptr inbounds i32, i32* %0, i64 %29
%31 = load i32, i32* %30
%32 = or i64 %29, 1
%33 = getelementptr inbounds i32, i32* %0, i64 %32
%34 = load i32, i32* %33
%35 = shl i32 %34, 8
%36 = or i32 %35, %31
%37 = or i64 %29, 2
%38 = getelementptr inbounds i32, i32* %0, i64 %37
%39 = load i32, i32* %38
%40 = shl i32 %39, 16
%41 = or i32 %36, %40
%42 = or i64 %29, 3
%43 = getelementptr inbounds i32, i32* %0, i64 %42
%44 = load i32, i32* %43
%45 = shl i32 %44, 24
%46 = or i32 %41, %45
%47 = tail call i64 @sendlong(i32 %46)
%48 = icmp eq i64 %47, 0
%49 = add nuw i64 %29, 4
br i1 %48, label %50, label %25
50: ; preds = %28
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%52 = tail call i32 @free(i32* %23)
br label %72
53: ; preds = %25, %16
%54 = tail call i32 @msleep(i32 150)
%55 = tail call i64 @sendlong(i32 249)
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%57 = load i32, i32* @stdout
%58 = tail call i32 @fflush(i32 %57)
br label %62
59: ; preds = %62
%60 = add nuw nsw i32 %63, 1
%61 = icmp eq i32 %60, 100
br i1 %61, label %68, label %62
62: ; preds = %53, %59
%63 = phi i32 [ 0, %53 ], [ %60, %59 ]
%64 = call i32 @msleep(i32 25)
%65 = call i32 @getAck(i32* %4)
%66 = load i32, i32* %4
%67 = icmp eq i32 %66, 0
br i1 %67, label %59, label %68
68: ; preds = %62, %59
%69 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0), %59 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0), %62 ]
%70 = call i32 @puts(i8* dereferenceable(1) %69)
%71 = call i32 @free(i32* %23)
br label %72
72: ; preds = %68, %50, %14, %8
%73 = phi i32 [ 1, %8 ], [ 1, %14 ], [ 1, %50 ], [ 0, %68 ]
ret i32 %73
}