#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 */
unsigned char serial_recv_byte(int);
int /*<<< orphan*/ serial_send_byte(int, int);
int xmodem_read_block(unsigned char, char *);
int /*<<< orphan*/ xmodem_wait();
long xmodem_recv(char *buf) {
  int r, receiving = 0;
  long size = 0;
  unsigned char c, block_number = 1;
  while (1) {
    if (!receiving)
      xmodem_wait();
    c = serial_recv_byte(1);
    if (c == 0x04) {
      serial_send_byte(1, 0x06);
      break;
    } else if (c == 0x18) {
      return -1;
    } else if (c == 0x01) {
      receiving++;
      r = xmodem_read_block(block_number, buf);
      if (r < 0) {
        serial_send_byte(1, 0x15);
      } else {
        block_number++;
        size += r;
        buf += r;
        serial_send_byte(1, 0x06);
      }
    } else {
      if (receiving)
        return -1;
    }
  }
  return size;
}


x86 assembly (clang, -O0):
<xmodem_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movb   $0x1,-0x22(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1170 <xmodem_recv+0x30>
mov    $0x0,%al
call   1030 <xmodem_wait@plt>
mov    $0x1,%edi
call   1060 <serial_recv_byte@plt>
mov    %al,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
cmp    $0x4,%eax
jne    119e <xmodem_recv+0x5e>
mov    $0x1,%edi
mov    $0x6,%esi
call   1040 <serial_send_byte@plt>
jmp    1262 <xmodem_recv+0x122>
movzbl -0x21(%rbp),%eax
cmp    $0x18,%eax
jne    11b8 <xmodem_recv+0x78>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    126a <xmodem_recv+0x12a>
movzbl -0x21(%rbp),%eax
cmp    $0x1,%eax
jne    1237 <xmodem_recv+0xf7>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x22(%rbp),%al
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1050 <xmodem_read_block@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fe <xmodem_recv+0xbe>
mov    $0x1,%edi
mov    $0x15,%esi
call   1040 <serial_send_byte@plt>
jmp    1232 <xmodem_recv+0xf2>
mov    -0x22(%rbp),%al
add    $0x1,%al
mov    %al,-0x22(%rbp)
movslq -0x14(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    $0x1,%edi
mov    $0x6,%esi
call   1040 <serial_send_byte@plt>
jmp    1253 <xmodem_recv+0x113>
cmpl   $0x0,-0x18(%rbp)
je     124e <xmodem_recv+0x10e>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    126a <xmodem_recv+0x12a>
jmp    1253 <xmodem_recv+0x113>
jmp    1258 <xmodem_recv+0x118>
jmp    125d <xmodem_recv+0x11d>
jmp    115f <xmodem_recv+0x1f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmodem_recv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x1,%r15b
xor    %ebp,%ebp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebp,%ebp
jne    116b <xmodem_recv+0x2b>
xor    %eax,%eax
call   1030 <xmodem_wait@plt>
mov    $0x1,%edi
call   1060 <serial_recv_byte@plt>
cmp    $0x1,%al
je     1197 <xmodem_recv+0x57>
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
cmp    $0x18,%cl
je     11e0 <xmodem_recv+0xa0>
cmp    $0x4,%cl
je     11ce <xmodem_recv+0x8e>
test   %ebp,%ebp
mov    $0x0,%ebp
je     1160 <xmodem_recv+0x20>
jmp    11e0 <xmodem_recv+0xa0>
movzbl %r15b,%edi
mov    %rbx,%rsi
call   1050 <xmodem_read_block@plt>
test   %eax,%eax
js     11ba <xmodem_recv+0x7a>
add    $0x1,%r15b
mov    %eax,%eax
add    %rax,%r14
add    %rax,%rbx
mov    $0x6,%esi
jmp    11bf <xmodem_recv+0x7f>
mov    $0x15,%esi
add    $0x1,%ebp
mov    $0x1,%edi
call   1040 <serial_send_byte@plt>
jmp    1160 <xmodem_recv+0x20>
mov    $0x1,%edi
mov    $0x6,%esi
call   1040 <serial_send_byte@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @xmodem_recv(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8
%8 = alloca i8
store i8* %0, i8** %3
store i32 0, i32* %5
store i64 0, i64* %6
store i8 1, i8* %8
br label %9
9: ; preds = %1, %60
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 (...) @xmodem_wait()
br label %14
14: ; preds = %12, %9
%15 = call zeroext i8 @serial_recv_byte(i32 1)
store i8 %15, i8* %7
%16 = load i8, i8* %7
%17 = zext i8 %16 to i32
%18 = icmp eq i32 %17, 4
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @serial_send_byte(i32 1, i32 6)
br label %61
21: ; preds = %14
%22 = load i8, i8* %7
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 24
br i1 %24, label %25, label %26
25: ; preds = %21
store i64 -1, i64* %2
br label %63
26: ; preds = %21
%27 = load i8, i8* %7
%28 = zext i8 %27 to i32
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %53
30: ; preds = %26
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
%33 = load i8, i8* %8
%34 = load i8*, i8** %3
%35 = call i32 @xmodem_read_block(i8 zeroext %33, i8* %34)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @serial_send_byte(i32 1, i32 21)
br label %52
40: ; preds = %30
%41 = load i8, i8* %8
%42 = add i8 %41, 1
store i8 %42, i8* %8
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = load i64, i64* %6
%46 = add nsw i64 %45, %44
store i64 %46, i64* %6
%47 = load i32, i32* %4
%48 = load i8*, i8** %3
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8* %50, i8** %3
%51 = call i32 @serial_send_byte(i32 1, i32 6)
br label %52
52: ; preds = %40, %38
br label %58
53: ; preds = %26
%54 = load i32, i32* %5
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
store i64 -1, i64* %2
br label %63
57: ; preds = %53
br label %58
58: ; preds = %57, %52
br label %59
59: ; preds = %58
br label %60
60: ; preds = %59
br label %9
61: ; preds = %19
%62 = load i64, i64* %6
store i64 %62, i64* %2
br label %63
63: ; preds = %61, %56, %25
%64 = load i64, i64* %2
ret i64 %64
}

LLVM IR (-O2):
define i64 @xmodem_recv(i8* %0) {
br label %2
2: ; preds = %26, %1
%3 = phi i8* [ %28, %26 ], [ %0, %1 ]
%4 = phi i32 [ %17, %26 ], [ 0, %1 ]
%5 = phi i64 [ %29, %26 ], [ 0, %1 ]
%6 = phi i8 [ %30, %26 ], [ 1, %1 ]
br label %7
7: ; preds = %2, %25
%8 = phi i32 [ 0, %25 ], [ %4, %2 ]
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (...) @xmodem_wait()
br label %12
12: ; preds = %10, %7
%13 = tail call zeroext i8 @serial_recv_byte(i32 1)
switch i8 %13, label %25 [
i8 4, label %14
i8 24, label %32
i8 1, label %16
]
14: ; preds = %12
%15 = tail call i32 @serial_send_byte(i32 1, i32 6)
br label %32
16: ; preds = %12
%17 = add nsw i32 %8, 1
%18 = tail call i32 @xmodem_read_block(i8 zeroext %6, i8* %3)
%19 = icmp slt i32 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %16
%21 = add i8 %6, 1
%22 = zext i32 %18 to i64
%23 = add nsw i64 %5, %22
%24 = getelementptr inbounds i8, i8* %3, i64 %22
br label %26
25: ; preds = %12
br i1 %9, label %7, label %32
26: ; preds = %16, %20
%27 = phi i32 [ 6, %20 ], [ 21, %16 ]
%28 = phi i8* [ %24, %20 ], [ %3, %16 ]
%29 = phi i64 [ %23, %20 ], [ %5, %16 ]
%30 = phi i8 [ %21, %20 ], [ %6, %16 ]
%31 = tail call i32 @serial_send_byte(i32 1, i32 %27)
br label %2
32: ; preds = %25, %12, %14
%33 = phi i64 [ %5, %14 ], [ -1, %12 ], [ -1, %25 ]
ret i64 %33
}