#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 xmodem_read_block(unsigned char block_number, char *buf) {
  unsigned char c, block_num, check_sum;
  int i;
  block_num = serial_recv_byte(1);
  if (block_num != block_number)
    return -1;
  block_num ^= serial_recv_byte(1);
  if (block_num != 0xff)
    return -1;
  check_sum = 0;
  for (i = 0; i < 128; i++) {
    c = serial_recv_byte(1);
    *(buf++) = c;
    check_sum += c;
  }
  check_sum ^= serial_recv_byte(1);
  if (check_sum)
    return -1;
  return i;
}


x86 assembly (clang, -O0):
<xmodem_read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
mov    %al,-0x12(%rbp)
movzbl -0x12(%rbp),%eax
movzbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
je     114b <xmodem_read_block+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <xmodem_read_block+0xf2>
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
movzbl %al,%ecx
movzbl -0x12(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x12(%rbp)
movzbl -0x12(%rbp),%eax
cmp    $0xff,%eax
je     117c <xmodem_read_block+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <xmodem_read_block+0xf2>
movb   $0x0,-0x13(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x80,-0x18(%rbp)
jge    11d0 <xmodem_read_block+0xc0>
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movzbl -0x11(%rbp),%ecx
movzbl -0x13(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x13(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1187 <xmodem_read_block+0x77>
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
movzbl %al,%ecx
movzbl -0x13(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x13(%rbp)
cmpb   $0x0,-0x13(%rbp)
je     11fc <xmodem_read_block+0xec>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <xmodem_read_block+0xf2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmodem_read_block>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
mov    $0xffffffff,%r14d
cmp    %bpl,%al
jne    118f <xmodem_read_block+0x7f>
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
xor    %bpl,%al
cmp    $0xff,%al
jne    118f <xmodem_read_block+0x7f>
xor    %ebp,%ebp
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
mov    %al,(%r15,%rbp,1)
movzbl %al,%ebx
add    %r14d,%ebx
movzbl %bl,%r14d
add    $0x1,%rbp
cmp    $0x80,%ebp
jne    1150 <xmodem_read_block+0x40>
mov    $0x1,%edi
call   1030 <serial_recv_byte@plt>
cmp    %bl,%al
mov    $0x80,%eax
mov    $0xffffffff,%r14d
cmove  %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @xmodem_read_block(i8 zeroext %0, i8* nocapture writeonly %1) {
%3 = tail call zeroext i8 @serial_recv_byte(i32 1)
%4 = icmp eq i8 %3, %0
br i1 %4, label %5, label %25
5: ; preds = %2
%6 = tail call zeroext i8 @serial_recv_byte(i32 1)
%7 = xor i8 %6, %0
%8 = icmp eq i8 %7, -1
br i1 %8, label %9, label %25
9: ; preds = %5, %9
%10 = phi i32 [ %17, %9 ], [ 0, %5 ]
%11 = phi i32 [ %18, %9 ], [ 0, %5 ]
%12 = phi i8* [ %14, %9 ], [ %1, %5 ]
%13 = tail call zeroext i8 @serial_recv_byte(i32 1)
%14 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %13, i8* %12
%15 = zext i8 %13 to i32
%16 = add nuw nsw i32 %11, %15
%17 = add nuw nsw i32 %10, 1
%18 = and i32 %16, 255
%19 = icmp eq i32 %17, 128
br i1 %19, label %20, label %9
20: ; preds = %9
%21 = tail call zeroext i8 @serial_recv_byte(i32 1)
%22 = trunc i32 %16 to i8
%23 = icmp eq i8 %21, %22
%24 = select i1 %23, i32 128, i32 -1
br label %25
25: ; preds = %20, %5, %2
%26 = phi i32 [ -1, %2 ], [ -1, %5 ], [ %24, %20 ]
ret i32 %26
}