#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 */
typedef unsigned char crc;

/* Variables and functions */
unsigned short crc16(unsigned char, unsigned short);
int /*<<< orphan*/ memcpy(unsigned char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int serial_read_byte();
int /*<<< orphan*/ serial_write(unsigned char *, int);
int write_xmodem(int index, char *p) {
  unsigned char data[133], c;
  unsigned short crc = 0;
  int i;
  data[0] = 1;
  data[1] = index;
  data[2] = 0xff - index;
  memcpy(data + 3, p, 128);
  for (i = 3; i < 131; i++) {
    crc = crc16(data[i], crc);
  }
  data[131] = (crc >> 8) & 0xff;
  data[132] = (crc)&0xff;
  printf("XMDump: ");
  for (i = 0; i < 133; i++)
    printf("%02x ", data[i]);
  printf("\n\n");
  serial_write(data, 133);
  return serial_read_byte();
}


x86 assembly (clang, -O0):
<write_xmodem>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movw   $0x0,-0xa4(%rbp)
movb   $0x1,-0xa0(%rbp)
mov    -0x4(%rbp),%eax
mov    %al,-0x9f(%rbp)
mov    $0xff,%eax
sub    -0x4(%rbp),%eax
mov    %al,-0x9e(%rbp)
lea    -0xa0(%rbp),%rdi
add    $0x3,%rdi
mov    -0x10(%rbp),%rsi
mov    $0x80,%edx
call   1040 <memcpy@plt>
movl   $0x3,-0xa8(%rbp)
cmpl   $0x83,-0xa8(%rbp)
jge    11f4 <write_xmodem+0xa4>
movslq -0xa8(%rbp),%rax
mov    -0xa0(%rbp,%rax,1),%al
movzbl %al,%edi
movzwl -0xa4(%rbp),%esi
call   1070 <crc16@plt>
mov    %ax,-0xa4(%rbp)
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    11ac <write_xmodem+0x5c>
movzwl -0xa4(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
movzwl -0xa4(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1c(%rbp)
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xa8(%rbp)
cmpl   $0x85,-0xa8(%rbp)
jge    126e <write_xmodem+0x11e>
movslq -0xa8(%rbp),%rax
movzbl -0xa0(%rbp,%rax,1),%esi
ff
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    122d <write_xmodem+0xdd>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xa0(%rbp),%rdi
mov    $0x85,%esi
call   1060 <serial_write@plt>
mov    $0x0,%al
call   1050 <serial_read_byte@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_xmodem>:
push   %r14
push   %rbx
sub    $0x88,%rsp
movb   $0x1,(%rsp)
mov    %dil,0x1(%rsp)
not    %dil
mov    %dil,0x2(%rsp)
lea    0x3(%rsp),%rdi
mov    $0x80,%edx
call   1050 <memcpy@plt>
xor    %ebx,%ebx
xor    %eax,%eax
xchg   %ax,%ax
movzbl 0x3(%rsp,%rbx,1),%edi
movzwl %ax,%esi
call   1080 <crc16@plt>
add    $0x1,%rbx
cmp    $0x80,%rbx
jne    1190 <write_xmodem+0x30>
rol    $0x8,%ax
mov    %ax,0x83(%rsp)
lea    0xe43(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe3e(%rip),%r14
xor    %ebx,%ebx
nopl   (%rax)
movzbl (%rsp,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x85,%rbx
jne    11d0 <write_xmodem+0x70>
lea    0xe1d(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
mov    $0x85,%esi
call   1070 <serial_write@plt>
xor    %eax,%eax
call   1060 <serial_read_byte@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"XMDump: \00"
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [3 x i8] c"\0A\0A\00"
define i32 @write_xmodem(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [133 x i8]
%6 = alloca i8
%7 = alloca i16
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i16 0, i16* %7
%9 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 0
store i8 1, i8* %9
%10 = load i32, i32* %3
%11 = trunc i32 %10 to i8
%12 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 1
store i8 %11, i8* %12
%13 = load i32, i32* %3
%14 = sub nsw i32 255, %13
%15 = trunc i32 %14 to i8
%16 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 2
store i8 %15, i8* %16
%17 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 0
%18 = getelementptr inbounds i8, i8* %17, i64 3
%19 = load i8*, i8** %4
%20 = call i32 @memcpy(i8* %18, i8* %19, i32 128)
store i32 3, i32* %8
br label %21
21: ; preds = %31, %2
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 131
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = load i16, i16* %7
%30 = call zeroext i16 @crc16(i8 zeroext %28, i16 zeroext %29)
store i16 %30, i16* %7
br label %31
31: ; preds = %24
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %21
34: ; preds = %21
%35 = load i16, i16* %7
%36 = zext i16 %35 to i32
%37 = ashr i32 %36, 8
%38 = and i32 %37, 255
%39 = trunc i32 %38 to i8
%40 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 131
store i8 %39, i8* %40
%41 = load i16, i16* %7
%42 = zext i16 %41 to i32
%43 = and i32 %42, 255
%44 = trunc i32 %43 to i8
%45 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 132
store i8 %44, i8* %45
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %8
br label %47
47: ; preds = %57, %34
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 133
br i1 %49, label %50, label %60
50: ; preds = %47
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %50
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %47
60: ; preds = %47
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%62 = getelementptr inbounds [133 x i8], [133 x i8]* %5, i64 0, i64 0
%63 = call i32 @serial_write(i8* %62, i32 133)
%64 = call i32 (...) @serial_read_byte()
ret i32 %64
}

LLVM IR (-O2):
@.str = [9 x i8] c"XMDump: \00"
@.str.1 = [6 x i8] c"%02x \00"
@str = [2 x i8] c"\0A\00"
define i32 @write_xmodem(i32 %0, i8* %1) {
%3 = alloca [133 x i8]
%4 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 0
store i8 1, i8* %4
%5 = trunc i32 %0 to i8
%6 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 1
store i8 %5, i8* %6
%7 = xor i8 %5, -1
%8 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 2
store i8 %7, i8* %8
%9 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 3
%10 = call i32 @memcpy(i8* %9, i8* %1, i32 128)
br label %11
11: ; preds = %2, %11
%12 = phi i64 [ 3, %2 ], [ %17, %11 ]
%13 = phi i16 [ 0, %2 ], [ %16, %11 ]
%14 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 %12
%15 = load i8, i8* %14
%16 = call zeroext i16 @crc16(i8 zeroext %15, i16 zeroext %13)
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, 131
br i1 %18, label %19, label %11
19: ; preds = %11
%20 = lshr i16 %16, 8
%21 = trunc i16 %20 to i8
%22 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 131
store i8 %21, i8* %22
%23 = trunc i16 %16 to i8
%24 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 132
store i8 %23, i8* %24
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %19, %26
%27 = phi i64 [ 0, %19 ], [ %32, %26 ]
%28 = getelementptr inbounds [133 x i8], [133 x i8]* %3, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = add nuw nsw i64 %27, 1
%33 = icmp eq i64 %32, 133
br i1 %33, label %34, label %26
34: ; preds = %26
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%36 = call i32 @serial_write(i8* %4, i32 133)
%37 = call i32 (...) @serial_read_byte()
ret i32 %37
}