#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 short calcrc(unsigned char *, int);
unsigned char getchar();
int /*<<< orphan*/ kbhit();
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ newline();
int /*<<< orphan*/ print_word(unsigned int);
int /*<<< orphan*/ putchar(unsigned char);
int /*<<< orphan*/ puts(char *);
int loadintoRam(unsigned int ramAddr) {
  int n, ii, flag;
  unsigned short crcval;
  unsigned char ch;
  unsigned char *p;
  unsigned char buff[140];
  int ix = 0, index = 0, tempindex = 0, offset = 0, count = 1000000;
  print_word(ramAddr);
  newline();
  p = (unsigned char *)&crcval;
  while (1) {
    putchar('C');
    while (!kbhit()) {
      if (!(count)) {
        putchar('C');
        count = 1000000;
      } else
        count--;
    }
    ch = getchar();
    if (ch == 0x01) {
      flag = 1;
      break;
    }
  }
  while (1) {
    if (flag == 1)
      flag = 0;
    else
      ch = getchar();
    if (ch == 0x01) {
      buff[ix++] = ch;
    } else if (ch == 0x04) {
      ch = 0x06;
      putchar(ch);
      return 0;
    } else if (ch == 0x18) {
      puts("CAN received\n");
      return 1;
    } else {
      continue;
    }
    while (ix < 133) {
      ch = getchar();
      buff[ix++] = ch;
    }
    crcval = calcrc(buff + 3, 128);
    if ((p[1] == buff[131]) && (p[0] == buff[132])) {
      memcpy((unsigned char *)ramAddr + index, buff + 3, 128);
      index = index + 128;
      ch = 0x06;
      putchar(ch);
      ix = 0;
    } else {
      ch = 0x15;
      putchar(ch);
      ix = 0;
    }
  }
}


x86 assembly (clang, -O0):
<loadintoRam>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xb4(%rbp)
movl   $0x0,-0xb8(%rbp)
movl   $0x0,-0xbc(%rbp)
movl   $0x0,-0xc0(%rbp)
movl   $0xf4240,-0xc4(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <print_word@plt>
mov    $0x0,%al
call   1030 <newline@plt>
lea    -0x16(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x43,%edi
call   1040 <putchar@plt>
mov    $0x0,%al
call   1070 <kbhit@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11fd <loadintoRam+0x7d>
jmp    1237 <loadintoRam+0xb7>
cmpl   $0x0,-0xc4(%rbp)
jne    1223 <loadintoRam+0xa3>
mov    $0x43,%edi
call   1040 <putchar@plt>
movl   $0xf4240,-0xc4(%rbp)
jmp    1232 <loadintoRam+0xb2>
mov    -0xc4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc4(%rbp)
jmp    11e1 <loadintoRam+0x61>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    %al,-0x17(%rbp)
movzbl -0x17(%rbp),%eax
cmp    $0x1,%eax
jne    125a <loadintoRam+0xda>
movl   $0x1,-0x14(%rbp)
jmp    125f <loadintoRam+0xdf>
jmp    11d7 <loadintoRam+0x57>
jmp    1264 <loadintoRam+0xe4>
cmpl   $0x1,-0x14(%rbp)
jne    127a <loadintoRam+0xfa>
movl   $0x0,-0x14(%rbp)
jmp    1284 <loadintoRam+0x104>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    %al,-0x17(%rbp)
movzbl -0x17(%rbp),%eax
cmp    $0x1,%eax
jne    12b3 <loadintoRam+0x133>
mov    -0x17(%rbp),%cl
mov    -0xb4(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0xb4(%rbp)
cltq
mov    %cl,-0xb0(%rbp,%rax,1)
jmp    1303 <loadintoRam+0x183>
movzbl -0x17(%rbp),%eax
cmp    $0x4,%eax
jne    12d9 <loadintoRam+0x159>
movb   $0x6,-0x17(%rbp)
movzbl -0x17(%rbp),%edi
call   1040 <putchar@plt>
movl   $0x0,-0x4(%rbp)
jmp    13f1 <loadintoRam+0x271>
movzbl -0x17(%rbp),%eax
cmp    $0x18,%eax
jne    12fe <loadintoRam+0x17e>
lea    0xd13(%rip),%rdi
call   1050 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f1 <loadintoRam+0x271>
jmp    1264 <loadintoRam+0xe4>
jmp    1308 <loadintoRam+0x188>
cmpl   $0x85,-0xb4(%rbp)
jge    1344 <loadintoRam+0x1c4>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    %al,-0x17(%rbp)
mov    -0x17(%rbp),%cl
mov    -0xb4(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0xb4(%rbp)
cltq
mov    %cl,-0xb0(%rbp,%rax,1)
jmp    1308 <loadintoRam+0x188>
lea    -0xb0(%rbp),%rdi
add    $0x3,%rdi
mov    $0x80,%esi
call   1080 <calcrc@plt>
mov    %ax,-0x16(%rbp)
mov    -0x20(%rbp),%rax
movzbl 0x1(%rax),%eax
movzbl -0x2d(%rbp),%ecx
cmp    %ecx,%eax
jne    13d5 <loadintoRam+0x255>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
movzbl -0x2c(%rbp),%ecx
cmp    %ecx,%eax
jne    13d5 <loadintoRam+0x255>
mov    -0x8(%rbp),%eax
mov    %eax,%edi
movslq -0xb8(%rbp),%rax
add    %rax,%rdi
lea    -0xb0(%rbp),%rsi
add    $0x3,%rsi
mov    $0x80,%edx
call   10a0 <memcpy@plt>
mov    -0xb8(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0xb8(%rbp)
movb   $0x6,-0x17(%rbp)
movzbl -0x17(%rbp),%edi
call   1040 <putchar@plt>
movl   $0x0,-0xb4(%rbp)
jmp    13ec <loadintoRam+0x26c>
movb   $0x15,-0x17(%rbp)
movzbl -0x17(%rbp),%edi
call   1040 <putchar@plt>
movl   $0x0,-0xb4(%rbp)
jmp    1264 <loadintoRam+0xe4>
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadintoRam>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %edi,%ebp
call   1060 <print_word@plt>
xor    %eax,%eax
call   1030 <newline@plt>
mov    $0xf4240,%ebx
jmp    11bb <loadintoRam+0x3b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    $0x1,%al
je     11f3 <loadintoRam+0x73>
mov    $0x43,%edi
call   1040 <putchar@plt>
jmp    11d3 <loadintoRam+0x53>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
xor    %eax,%eax
call   1070 <kbhit@plt>
test   %eax,%eax
jne    11b0 <loadintoRam+0x30>
test   %ebx,%ebx
jne    11d0 <loadintoRam+0x50>
mov    $0x43,%edi
call   1040 <putchar@plt>
mov    $0xf4240,%ebx
jmp    11d3 <loadintoRam+0x53>
lea    0x3(%rsp),%r14
mov    %ebp,%r15d
mov    $0x1,%bl
xor    %ebp,%ebp
mov    $0x1,%al
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   $0x1,%al
jne    121d <loadintoRam+0x9d>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %eax,%ebx
cmp    $0x18,%bl
je     12aa <loadintoRam+0x12a>
cmp    $0x4,%bl
je     12bd <loadintoRam+0x13d>
xor    %eax,%eax
cmp    $0x1,%bl
jne    1210 <loadintoRam+0x90>
movb   $0x1,(%rsp)
mov    $0x1,%ebx
nop
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %al,(%rsp,%rbx,1)
add    $0x1,%rbx
cmp    $0x85,%rbx
jne    1240 <loadintoRam+0xc0>
mov    %r14,%rdi
mov    $0x80,%esi
call   1080 <calcrc@plt>
movzwl %ax,%ecx
shr    $0x8,%ecx
movzbl 0x83(%rsp),%edx
mov    $0x15,%bl
cmp    %dx,%cx
jne    129b <loadintoRam+0x11b>
cmp    %al,0x84(%rsp)
jne    129b <loadintoRam+0x11b>
movslq %ebp,%rbp
lea    (%r15,%rbp,1),%rdi
mov    %r14,%rsi
mov    $0x80,%edx
call   10a0 <memcpy@plt>
sub    $0xffffff80,%ebp
mov    $0x6,%bl
movzbl %bl,%edi
call   1040 <putchar@plt>
xor    %eax,%eax
jmp    1210 <loadintoRam+0x90>
lea    0xd4f(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
jmp    12c9 <loadintoRam+0x149>
mov    $0x6,%edi
call   1040 <putchar@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"CAN received\0A\00"
define i32 @loadintoRam(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16
%8 = alloca i8
%9 = alloca i8*
%10 = alloca [140 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 1000000, i32* %15
%16 = load i32, i32* %3
%17 = call i32 @print_word(i32 %16)
%18 = call i32 (...) @newline()
%19 = bitcast i16* %7 to i8*
store i8* %19, i8** %9
br label %20
20: ; preds = %1, %41
%21 = call i32 @putchar(i8 zeroext 67)
br label %22
22: ; preds = %34, %20
%23 = call i32 (...) @kbhit()
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = load i32, i32* %15
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @putchar(i8 zeroext 67)
store i32 1000000, i32* %15
br label %34
31: ; preds = %26
%32 = load i32, i32* %15
%33 = add nsw i32 %32, -1
store i32 %33, i32* %15
br label %34
34: ; preds = %31, %29
br label %22
35: ; preds = %22
%36 = call zeroext i8 (...) @getchar()
store i8 %36, i8* %8
%37 = load i8, i8* %8
%38 = zext i8 %37 to i32
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 1, i32* %6
br label %42
41: ; preds = %35
br label %20
42: ; preds = %40
br label %43
43: ; preds = %42, %72, %122
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 0, i32* %6
br label %49
47: ; preds = %43
%48 = call zeroext i8 (...) @getchar()
store i8 %48, i8* %8
br label %49
49: ; preds = %47, %46
%50 = load i8, i8* %8
%51 = zext i8 %50 to i32
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %59
53: ; preds = %49
%54 = load i8, i8* %8
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds [140 x i8], [140 x i8]* %10, i64 0, i64 %57
store i8 %54, i8* %58
br label %73
59: ; preds = %49
%60 = load i8, i8* %8
%61 = zext i8 %60 to i32
%62 = icmp eq i32 %61, 4
br i1 %62, label %63, label %66
63: ; preds = %59
store i8 6, i8* %8
%64 = load i8, i8* %8
%65 = call i32 @putchar(i8 zeroext %64)
store i32 0, i32* %2
br label %123
66: ; preds = %59
%67 = load i8, i8* %8
%68 = zext i8 %67 to i32
%69 = icmp eq i32 %68, 24
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %123
72: ; preds = %66
br label %43
73: ; preds = %53
br label %74
74: ; preds = %77, %73
%75 = load i32, i32* %11
%76 = icmp slt i32 %75, 133
br i1 %76, label %77, label %84
77: ; preds = %74
%78 = call zeroext i8 (...) @getchar()
store i8 %78, i8* %8
%79 = load i8, i8* %8
%80 = load i32, i32* %11
%81 = add nsw i32 %80, 1
store i32 %81, i32* %11
%82 = sext i32 %80 to i64
%83 = getelementptr inbounds [140 x i8], [140 x i8]* %10, i64 0, i64 %82
store i8 %79, i8* %83
br label %74
84: ; preds = %74
%85 = getelementptr inbounds [140 x i8], [140 x i8]* %10, i64 0, i64 0
%86 = getelementptr inbounds i8, i8* %85, i64 3
%87 = call zeroext i16 @calcrc(i8* %86, i32 128)
store i16 %87, i16* %7
%88 = load i8*, i8** %9
%89 = getelementptr inbounds i8, i8* %88, i64 1
%90 = load i8, i8* %89
%91 = zext i8 %90 to i32
%92 = getelementptr inbounds [140 x i8], [140 x i8]* %10, i64 0, i64 131
%93 = load i8, i8* %92
%94 = zext i8 %93 to i32
%95 = icmp eq i32 %91, %94
br i1 %95, label %96, label %119
96: ; preds = %84
%97 = load i8*, i8** %9
%98 = getelementptr inbounds i8, i8* %97, i64 0
%99 = load i8, i8* %98
%100 = zext i8 %99 to i32
%101 = getelementptr inbounds [140 x i8], [140 x i8]* %10, i64 0, i64 132
%102 = load i8, i8* %101
%103 = zext i8 %102 to i32
%104 = icmp eq i32 %100, %103
br i1 %104, label %105, label %119
105: ; preds = %96
%106 = load i32, i32* %3
%107 = zext i32 %106 to i64
%108 = inttoptr i64 %107 to i8*
%109 = load i32, i32* %12
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds i8, i8* %108, i64 %110
%112 = getelementptr inbounds [140 x i8], [140 x i8]* %10, i64 0, i64 0
%113 = getelementptr inbounds i8, i8* %112, i64 3
%114 = call i32 @memcpy(i8* %111, i8* %113, i32 128)
%115 = load i32, i32* %12
%116 = add nsw i32 %115, 128
store i32 %116, i32* %12
store i8 6, i8* %8
%117 = load i8, i8* %8
%118 = call i32 @putchar(i8 zeroext %117)
store i32 0, i32* %11
br label %122
119: ; preds = %96, %84
store i8 21, i8* %8
%120 = load i8, i8* %8
%121 = call i32 @putchar(i8 zeroext %120)
store i32 0, i32* %11
br label %122
122: ; preds = %119, %105
br label %43
123: ; preds = %70, %63
%124 = load i32, i32* %2
ret i32 %124
}

LLVM IR (-O2):
@.str = [14 x i8] c"CAN received\0A\00"
define i32 @loadintoRam(i32 %0) {
%2 = alloca [140 x i8]
%3 = getelementptr inbounds [140 x i8], [140 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @print_word(i32 %0)
%5 = tail call i32 (...) @newline()
br label %6
6: ; preds = %22, %1
%7 = phi i32 [ 1000000, %1 ], [ %23, %22 ]
%8 = tail call i32 @putchar(i8 zeroext 67)
%9 = tail call i32 (...) @kbhit()
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %6, %18
%12 = phi i32 [ %19, %18 ], [ %7, %6 ]
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @putchar(i8 zeroext 67)
br label %18
16: ; preds = %11
%17 = add nsw i32 %12, -1
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %17, %16 ], [ 1000000, %14 ]
%20 = tail call i32 (...) @kbhit()
%21 = icmp eq i32 %20, 0
br i1 %21, label %11, label %22
22: ; preds = %18, %6
%23 = phi i32 [ %7, %6 ], [ %19, %18 ]
%24 = tail call zeroext i8 (...) @getchar()
%25 = icmp eq i8 %24, 1
br i1 %25, label %26, label %6
26: ; preds = %22
%27 = getelementptr inbounds [140 x i8], [140 x i8]* %2, i64 0, i64 3
%28 = getelementptr inbounds [140 x i8], [140 x i8]* %2, i64 0, i64 131
%29 = getelementptr inbounds [140 x i8], [140 x i8]* %2, i64 0, i64 132
%30 = zext i32 %0 to i64
%31 = inttoptr i64 %30 to i8*
br label %32
32: ; preds = %26, %69
%33 = phi i8 [ 1, %26 ], [ %70, %69 ]
%34 = phi i32 [ 0, %26 ], [ %71, %69 ]
%35 = phi i1 [ true, %26 ], [ false, %69 ]
br label %36
36: ; preds = %32, %41
%37 = phi i8 [ %42, %41 ], [ %33, %32 ]
%38 = phi i1 [ false, %41 ], [ %35, %32 ]
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call zeroext i8 (...) @getchar()
br label %41
41: ; preds = %36, %39
%42 = phi i8 [ %40, %39 ], [ %37, %36 ]
switch i8 %42, label %36 [
i8 1, label %43
i8 4, label %44
i8 24, label %46
]
43: ; preds = %41
store i8 1, i8* %3
br label %48
44: ; preds = %41
%45 = call i32 @putchar(i8 zeroext 6)
br label %73
46: ; preds = %41
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %73
48: ; preds = %43, %48
%49 = phi i64 [ 1, %43 ], [ %51, %48 ]
%50 = call zeroext i8 (...) @getchar()
%51 = add nuw nsw i64 %49, 1
%52 = getelementptr inbounds [140 x i8], [140 x i8]* %2, i64 0, i64 %49
store i8 %50, i8* %52
%53 = icmp eq i64 %51, 133
br i1 %53, label %54, label %48
54: ; preds = %48
%55 = call zeroext i16 @calcrc(i8* %27, i32 128)
%56 = lshr i16 %55, 8
%57 = load i8, i8* %28
%58 = zext i8 %57 to i16
%59 = icmp eq i16 %56, %58
%60 = load i8, i8* %29
%61 = trunc i16 %55 to i8
%62 = icmp eq i8 %60, %61
%63 = select i1 %59, i1 %62, i1 false
br i1 %63, label %64, label %69
64: ; preds = %54
%65 = sext i32 %34 to i64
%66 = getelementptr inbounds i8, i8* %31, i64 %65
%67 = call i32 @memcpy(i8* %66, i8* %27, i32 128)
%68 = add nsw i32 %34, 128
br label %69
69: ; preds = %54, %64
%70 = phi i8 [ 6, %64 ], [ 21, %54 ]
%71 = phi i32 [ %68, %64 ], [ %34, %54 ]
%72 = call i32 @putchar(i8 zeroext %70)
br label %32
73: ; preds = %46, %44
%74 = phi i32 [ 0, %44 ], [ 1, %46 ]
ret i32 %74
}