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


x86 assembly (clang, -O0):
<xmodem_read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    $0x0,%al
call   1030 <sci_recv@plt>
mov    %al,-0x7(%rbp)
movzbl -0x7(%rbp),%eax
movzbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
je     1154 <xmodem_read_block+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <xmodem_read_block+0xed>
mov    $0x0,%al
call   1030 <sci_recv@plt>
movzbl %al,%ecx
movzbl -0x7(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x7(%rbp)
movzbl -0x7(%rbp),%eax
cmp    $0xff,%eax
je     1182 <xmodem_read_block+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <xmodem_read_block+0xed>
movb   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x80,-0xc(%rbp)
jge    11de <xmodem_read_block+0xbe>
mov    $0x0,%al
call   1030 <sci_recv@plt>
mov    %al,-0x6(%rbp)
movzbl -0x6(%rbp),%edi
call   1040 <xmodem_srec_seqload@plt>
cmp    $0x0,%rax
jge    11c3 <xmodem_read_block+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <xmodem_read_block+0xed>
movzbl -0x6(%rbp),%ecx
movzbl -0x8(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    118d <xmodem_read_block+0x6d>
mov    $0x0,%al
call   1030 <sci_recv@plt>
movzbl %al,%ecx
movzbl -0x8(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x8(%rbp)
cmpb   $0x0,-0x8(%rbp)
je     1207 <xmodem_read_block+0xe7>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <xmodem_read_block+0xed>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmodem_read_block>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <sci_recv@plt>
mov    $0xffffffff,%r14d
cmp    %bpl,%al
jne    119d <xmodem_read_block+0x7d>
xor    %eax,%eax
call   1030 <sci_recv@plt>
xor    %bpl,%al
cmp    $0xff,%al
jne    119d <xmodem_read_block+0x7d>
mov    $0x80,%r12d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <sci_recv@plt>
movzbl %al,%ebp
mov    %ebp,%edi
call   1040 <xmodem_srec_seqload@plt>
test   %rax,%rax
js     119d <xmodem_read_block+0x7d>
mov    %r15d,%ebx
add    %ebp,%ebx
movzbl %bl,%r15d
add    $0xffffffff,%r12d
jne    1160 <xmodem_read_block+0x40>
xor    %eax,%eax
call   1030 <sci_recv@plt>
cmp    %bl,%al
mov    $0x80,%eax
mov    $0xffffffff,%r14d
cmove  %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @xmodem_read_block(i8 zeroext %0) {
%2 = tail call zeroext i8 (...) @sci_recv()
%3 = icmp eq i8 %2, %0
br i1 %3, label %4, label %25
4: ; preds = %1
%5 = tail call zeroext i8 (...) @sci_recv()
%6 = xor i8 %5, %0
%7 = icmp eq i8 %6, -1
br i1 %7, label %8, label %25
8: ; preds = %4, %14
%9 = phi i32 [ %17, %14 ], [ 0, %4 ]
%10 = phi i32 [ %18, %14 ], [ 0, %4 ]
%11 = tail call zeroext i8 (...) @sci_recv()
%12 = tail call i64 @xmodem_srec_seqload(i8 zeroext %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %8
%15 = zext i8 %11 to i32
%16 = add nuw nsw i32 %10, %15
%17 = add nuw nsw i32 %9, 1
%18 = and i32 %16, 255
%19 = icmp eq i32 %17, 128
br i1 %19, label %20, label %8
20: ; preds = %14
%21 = tail call zeroext i8 (...) @sci_recv()
%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 = %8, %20, %4, %1
%26 = phi i32 [ -1, %1 ], [ -1, %4 ], [ %24, %20 ], [ -1, %8 ]
ret i32 %26
}