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


x86 assembly (clang, -O0):
<xmodem_recv_srec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x1,-0x12(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    116b <xmodem_recv_srec+0x2b>
mov    $0x0,%al
call   1040 <xmodem_wait@plt>
mov    $0x0,%al
call   1030 <sci_recv@plt>
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x4,%eax
jne    1191 <xmodem_recv_srec+0x51>
mov    $0x6,%edi
call   1050 <sci_send@plt>
jmp    122d <xmodem_recv_srec+0xed>
movzbl -0x11(%rbp),%eax
cmp    $0x18,%eax
jne    11aa <xmodem_recv_srec+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <xmodem_recv_srec+0xf3>
movzbl -0x11(%rbp),%eax
cmp    $0x1,%eax
jne    1203 <xmodem_recv_srec+0xc3>
movl   $0x1,-0xc(%rbp)
movzbl -0x12(%rbp),%edi
call   1060 <xmodem_read_block@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e3 <xmodem_recv_srec+0xa3>
mov    $0x15,%edi
call   1050 <sci_send@plt>
jmp    11fe <xmodem_recv_srec+0xbe>
mov    -0x12(%rbp),%al
add    $0x1,%al
mov    %al,-0x12(%rbp)
mov    -0x8(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x6,%edi
call   1050 <sci_send@plt>
jmp    121e <xmodem_recv_srec+0xde>
cmpl   $0x0,-0xc(%rbp)
je     1219 <xmodem_recv_srec+0xd9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <xmodem_recv_srec+0xf3>
jmp    121e <xmodem_recv_srec+0xde>
jmp    1223 <xmodem_recv_srec+0xe3>
jmp    1228 <xmodem_recv_srec+0xe8>
jmp    115a <xmodem_recv_srec+0x1a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmodem_recv_srec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1,%bpl
mov    $0x15,%r14d
xor    %r15d,%r15d
mov    $0x1,%bl
cs nopw 0x0(%rax,%rax,1)
nop
test   $0x1,%bpl
je     116d <xmodem_recv_srec+0x2d>
xor    %eax,%eax
call   1040 <xmodem_wait@plt>
xor    %eax,%eax
call   1030 <sci_recv@plt>
cmp    $0x1,%al
je     1194 <xmodem_recv_srec+0x54>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x18,%cl
je     11ca <xmodem_recv_srec+0x8a>
cmp    $0x4,%cl
je     11bd <xmodem_recv_srec+0x7d>
test   $0x1,%bpl
mov    $0x1,%bpl
jne    1160 <xmodem_recv_srec+0x20>
jmp    11ca <xmodem_recv_srec+0x8a>
movzbl %bl,%ebx
mov    %ebx,%edi
call   1060 <xmodem_read_block@plt>
xor    %ebp,%ebp
test   %eax,%eax
setns  %cl
mov    $0x6,%edi
cmovs  %r14d,%edi
cmovs  %ebp,%eax
add    %eax,%r15d
add    %cl,%bl
call   1050 <sci_send@plt>
jmp    1160 <xmodem_recv_srec+0x20>
mov    $0x6,%edi
call   1050 <sci_send@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @xmodem_recv_srec() {
br label %1
1: ; preds = %0, %13
%2 = phi i1 [ true, %0 ], [ false, %13 ]
%3 = phi i32 [ 0, %0 ], [ %18, %13 ]
%4 = phi i8 [ 1, %0 ], [ %21, %13 ]
br label %5
5: ; preds = %1, %23
%6 = phi i1 [ true, %23 ], [ %2, %1 ]
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i32 (...) @xmodem_wait()
br label %9
9: ; preds = %7, %5
%10 = tail call zeroext i8 (...) @sci_recv()
switch i8 %10, label %23 [
i8 4, label %11
i8 24, label %24
i8 1, label %13
]
11: ; preds = %9
%12 = tail call i32 @sci_send(i32 6)
br label %24
13: ; preds = %9
%14 = tail call i32 @xmodem_read_block(i8 zeroext %4)
%15 = icmp slt i32 %14, 0
%16 = select i1 %15, i32 21, i32 6
%17 = select i1 %15, i32 0, i32 %14
%18 = add nuw nsw i32 %3, %17
%19 = xor i1 %15, true
%20 = zext i1 %19 to i8
%21 = add i8 %4, %20
%22 = tail call i32 @sci_send(i32 %16)
br label %1
23: ; preds = %9
br i1 %6, label %5, label %24
24: ; preds = %23, %9, %11
%25 = phi i32 [ %3, %11 ], [ -1, %9 ], [ -1, %23 ]
ret i32 %25
}