#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 */
void *sw_getchar();
int /*<<< orphan*/ sw_printf(char *, ...);
int /*<<< orphan*/ sw_putchar(int);
int /*<<< orphan*/ sw_usleep(int);
unsigned int sw_xmodem_rec(unsigned int addr) {
  int c;
  int i;
  int count;
  char *xbuf;
  xbuf = (char *)addr;
  count = 0;
  sw_printf("Countdown to receive data, you have 10 seconds...");
  for (i = 9; i >= 0; i--) {
    sw_usleep(1000 * 1000);
    sw_printf("%u ", i);
  }
  sw_putchar(0x15);
  for (;;) {
    c = sw_getchar();
    if (c == 0x04) {
      break;
    }
    if (c != 0x01) {
      continue;
    }
    sw_getchar();
    sw_getchar();
    for (i = 0; i < 128; i++) {
      *xbuf++ = sw_getchar();
    }
    sw_getchar();
    sw_putchar(0x06);
  }
  sw_putchar(0x06);
  return count;
}


x86 assembly (clang, -O0):
<sw_xmodem_rec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1040 <sw_printf@plt>
movl   $0x9,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11a1 <sw_xmodem_rec+0x61>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    -0xc(%rbp),%esi
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <sw_printf@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    116e <sw_xmodem_rec+0x2e>
mov    $0x15,%edi
call   1030 <sw_putchar@plt>
mov    $0x0,%al
call   1050 <sw_getchar@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11c4 <sw_xmodem_rec+0x84>
jmp    1233 <sw_xmodem_rec+0xf3>
cmpl   $0x1,-0x8(%rbp)
je     11d3 <sw_xmodem_rec+0x93>
jmp    11ab <sw_xmodem_rec+0x6b>
mov    $0x0,%al
call   1050 <sw_getchar@plt>
mov    $0x0,%al
call   1050 <sw_getchar@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x80,-0xc(%rbp)
jge    121d <sw_xmodem_rec+0xdd>
mov    $0x0,%al
call   1050 <sw_getchar@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11e8 <sw_xmodem_rec+0xa8>
mov    $0x0,%al
call   1050 <sw_getchar@plt>
mov    $0x6,%edi
call   1030 <sw_putchar@plt>
jmp    11ab <sw_xmodem_rec+0x6b>
mov    $0x6,%edi
call   1030 <sw_putchar@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sw_xmodem_rec>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%r15d
lea    0xeb1(%rip),%rdi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
lea    0xecb(%rip),%r14
mov    %r14,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0xf4240,%edi
call   1060 <sw_usleep@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <sw_printf@plt>
mov    $0x15,%edi
call   1030 <sw_putchar@plt>
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <sw_getchar@plt>
cmp    $0x4,%eax
je     12b8 <sw_xmodem_rec+0x178>
cmp    $0x1,%eax
jne    1260 <sw_xmodem_rec+0x120>
xor    %eax,%eax
call   1050 <sw_getchar@plt>
xor    %eax,%eax
call   1050 <sw_getchar@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <sw_getchar@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0x80,%ebx
jne    1290 <sw_xmodem_rec+0x150>
xor    %eax,%eax
call   1050 <sw_getchar@plt>
add    %rbx,%r15
mov    $0x6,%edi
jmp    1259 <sw_xmodem_rec+0x119>
mov    $0x6,%edi
call   1030 <sw_putchar@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Countdown to receive data, you have 10 seconds...\00"
@.str.1 = [4 x i8] c"%u \00"
define i32 @sw_xmodem_rec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = zext i32 %7 to i64
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %6
store i32 0, i32* %5
%10 = call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
store i32 9, i32* %4
br label %11
11: ; preds = %18, %1
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = call i32 @sw_usleep(i32 1000000)
%16 = load i32, i32* %4
%17 = call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14
%19 = load i32, i32* %4
%20 = add nsw i32 %19, -1
store i32 %20, i32* %4
br label %11
21: ; preds = %11
%22 = call i32 @sw_putchar(i32 21)
br label %23
23: ; preds = %47, %32, %21
%24 = call i8* (...) @sw_getchar()
%25 = ptrtoint i8* %24 to i32
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 4
br i1 %27, label %28, label %29
28: ; preds = %23
br label %50
29: ; preds = %23
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %33
32: ; preds = %29
br label %23
33: ; preds = %29
%34 = call i8* (...) @sw_getchar()
%35 = call i8* (...) @sw_getchar()
store i32 0, i32* %4
br label %36
36: ; preds = %44, %33
%37 = load i32, i32* %4
%38 = icmp slt i32 %37, 128
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = call i8* (...) @sw_getchar()
%41 = ptrtoint i8* %40 to i8
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %6
store i8 %41, i8* %42
br label %44
44: ; preds = %39
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %36
47: ; preds = %36
%48 = call i8* (...) @sw_getchar()
%49 = call i32 @sw_putchar(i32 6)
br label %23
50: ; preds = %28
%51 = call i32 @sw_putchar(i32 6)
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
@.str = [50 x i8] c"Countdown to receive data, you have 10 seconds...\00"
@.str.1 = [4 x i8] c"%u \00"
define i32 @sw_xmodem_rec(i32 %0) {
%2 = zext i32 %0 to i64
%3 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @sw_usleep(i32 1000000)
%5 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 9)
%6 = tail call i32 @sw_usleep(i32 1000000)
%7 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 8)
%8 = tail call i32 @sw_usleep(i32 1000000)
%9 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 7)
%10 = tail call i32 @sw_usleep(i32 1000000)
%11 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 6)
%12 = tail call i32 @sw_usleep(i32 1000000)
%13 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 5)
%14 = tail call i32 @sw_usleep(i32 1000000)
%15 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%16 = tail call i32 @sw_usleep(i32 1000000)
%17 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%18 = tail call i32 @sw_usleep(i32 1000000)
%19 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 2)
%20 = tail call i32 @sw_usleep(i32 1000000)
%21 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 1)
%22 = tail call i32 @sw_usleep(i32 1000000)
%23 = tail call i32 (i8*, ...) @sw_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 0)
%24 = inttoptr i64 %2 to i8*
br label %25
25: ; preds = %45, %1
%26 = phi i32 [ 6, %45 ], [ 21, %1 ]
%27 = phi i8* [ %42, %45 ], [ %24, %1 ]
%28 = tail call i32 @sw_putchar(i32 %26)
br label %29
29: ; preds = %25, %29
%30 = tail call i8* (...) @sw_getchar()
%31 = ptrtoint i8* %30 to i64
%32 = trunc i64 %31 to i32
switch i32 %32, label %29 [
i32 4, label %47
i32 1, label %33
]
33: ; preds = %29
%34 = tail call i8* (...) @sw_getchar()
%35 = tail call i8* (...) @sw_getchar()
br label %36
36: ; preds = %33, %36
%37 = phi i8* [ %27, %33 ], [ %42, %36 ]
%38 = phi i32 [ 0, %33 ], [ %43, %36 ]
%39 = tail call i8* (...) @sw_getchar()
%40 = ptrtoint i8* %39 to i64
%41 = trunc i64 %40 to i8
%42 = getelementptr inbounds i8, i8* %37, i64 1
store i8 %41, i8* %37
%43 = add nuw nsw i32 %38, 1
%44 = icmp eq i32 %43, 128
br i1 %44, label %45, label %36
45: ; preds = %36
%46 = tail call i8* (...) @sw_getchar()
br label %25
47: ; preds = %29
%48 = tail call i32 @sw_putchar(i32 6)
ret i32 0
}