#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 */
int /*<<< orphan*/ GetRecord(char, char *);
int getc(int);
int /*<<< orphan*/ putchar(int);
int xmodem_rx(char *dest) {
  int starting, ch;
  char packetNumber, *startAddress = dest;
  packetNumber = 1;
  starting = 1;
  while (1) {
    if (starting)
      putchar('C');
    if (((ch = getc(1)) == -1) || (ch != 0x01 && ch != 0x04))
      continue;
    if (ch == 0x04) {
      putchar(0x06);
      return (dest - startAddress);
    }
    starting = 0;
    if (!GetRecord(packetNumber, dest))
      return (-1);
    dest += 128;
    packetNumber++;
  }
  return (-1);
}


x86 assembly (clang, -O0):
<xmodem_rx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movb   $0x1,-0x19(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1163 <xmodem_rx+0x33>
mov    $0x43,%edi
call   1030 <putchar@plt>
mov    $0x1,%edi
call   1050 <getc@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     118d <xmodem_rx+0x5d>
cmpl   $0x1,-0x18(%rbp)
je     1192 <xmodem_rx+0x62>
cmpl   $0x4,-0x18(%rbp)
je     1192 <xmodem_rx+0x62>
jmp    114f <xmodem_rx+0x1f>
cmpl   $0x4,-0x18(%rbp)
jne    11b9 <xmodem_rx+0x89>
mov    $0x6,%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    11ff <xmodem_rx+0xcf>
movl   $0x0,-0x14(%rbp)
mov    -0x19(%rbp),%al
mov    -0x10(%rbp),%rsi
movsbl %al,%edi
call   1040 <GetRecord@plt>
cmp    $0x0,%eax
jne    11e4 <xmodem_rx+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <xmodem_rx+0xcf>
mov    -0x10(%rbp),%rax
add    $0x80,%rax
mov    %rax,-0x10(%rbp)
mov    -0x19(%rbp),%al
add    $0x1,%al
mov    %al,-0x19(%rbp)
jmp    114f <xmodem_rx+0x1f>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmodem_rx>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1,%r15b
xor    %ebp,%ebp
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   $0x1,%bpl
jne    1160 <xmodem_rx+0x30>
mov    $0x43,%edi
call   1030 <putchar@plt>
mov    $0x1,%edi
call   1050 <getc@plt>
cmp    $0x1,%eax
je     1176 <xmodem_rx+0x46>
cmp    $0x4,%eax
jne    1150 <xmodem_rx+0x20>
jmp    1193 <xmodem_rx+0x63>
movsbl %r15b,%edi
mov    %rbx,%rsi
call   1040 <GetRecord@plt>
test   %eax,%eax
je     11ad <xmodem_rx+0x7d>
sub    $0xffffffffffffff80,%rbx
add    $0x1,%r15b
mov    $0x1,%bpl
jmp    1150 <xmodem_rx+0x20>
mov    $0x6,%edi
call   1030 <putchar@plt>
sub    %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebx
jmp    11a0 <xmodem_rx+0x70>

LLVM IR (-O0):
define i32 @xmodem_rx(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
store i8* %8, i8** %7
store i8 1, i8* %6
store i32 1, i32* %4
br label %9
9: ; preds = %1, %23, %41
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @putchar(i32 67)
br label %14
14: ; preds = %12, %9
%15 = call i32 @getc(i32 1)
store i32 %15, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 4
br i1 %22, label %23, label %24
23: ; preds = %20, %14
br label %9
24: ; preds = %20, %17
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 4
br i1 %26, label %27, label %35
27: ; preds = %24
%28 = call i32 @putchar(i32 6)
%29 = load i8*, i8** %3
%30 = load i8*, i8** %7
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
store i32 %34, i32* %2
br label %46
35: ; preds = %24
store i32 0, i32* %4
%36 = load i8, i8* %6
%37 = load i8*, i8** %3
%38 = call i32 @GetRecord(i8 signext %36, i8* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %35
store i32 -1, i32* %2
br label %46
41: ; preds = %35
%42 = load i8*, i8** %3
%43 = getelementptr inbounds i8, i8* %42, i64 128
store i8* %43, i8** %3
%44 = load i8, i8* %6
%45 = add i8 %44, 1
store i8 %45, i8* %6
br label %9
46: ; preds = %40, %27
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
define i32 @xmodem_rx(i8* %0) {
br label %2
2: ; preds = %20, %1
%3 = phi i1 [ true, %20 ], [ false, %1 ]
%4 = phi i8* [ %21, %20 ], [ %0, %1 ]
%5 = phi i8 [ %22, %20 ], [ 1, %1 ]
br label %6
6: ; preds = %2, %9
br i1 %3, label %9, label %7
7: ; preds = %6
%8 = tail call i32 @putchar(i32 67)
br label %9
9: ; preds = %7, %6
%10 = tail call i32 @getc(i32 1)
switch i32 %10, label %6 [
i32 1, label %17
i32 4, label %11
]
11: ; preds = %9
%12 = tail call i32 @putchar(i32 6)
%13 = ptrtoint i8* %4 to i64
%14 = ptrtoint i8* %0 to i64
%15 = sub i64 %13, %14
%16 = trunc i64 %15 to i32
br label %23
17: ; preds = %9
%18 = tail call i32 @GetRecord(i8 signext %5, i8* %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = getelementptr inbounds i8, i8* %4, i64 128
%22 = add i8 %5, 1
br label %2
23: ; preds = %17, %11
%24 = phi i32 [ %16, %11 ], [ -1, %17 ]
ret i32 %24
}