#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 */
char USARTGetc();
char getMessage(char *buf, unsigned char limit) {
  char ch;
  unsigned char idx;
  idx = 0;
  do {
    ch = USARTGetc();
  } while (ch != 0x7E);
  ch = USARTGetc();
  while ((ch != 0x7E) && (idx < limit)) {
    if (ch == 0x7D) {
      ch = USARTGetc();
      ch ^= 0x20;
    }
    buf[idx] = ch;
    idx++;
    USARTGetc();
  }
  return idx;
}


x86 assembly (clang, -O0):
<getMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movb   $0x0,-0xb(%rbp)
mov    $0x0,%al
call   1030 <USARTGetc@plt>
mov    %al,-0xa(%rbp)
movsbl -0xa(%rbp),%eax
cmp    $0x7e,%eax
jne    1126 <getMessage+0x16>
mov    $0x0,%al
call   1030 <USARTGetc@plt>
mov    %al,-0xa(%rbp)
movsbl -0xa(%rbp),%ecx
xor    %eax,%eax
cmp    $0x7e,%ecx
mov    %al,-0xc(%rbp)
je     1169 <getMessage+0x59>
movzbl -0xb(%rbp),%eax
movzbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0xc(%rbp)
mov    -0xc(%rbp),%al
test   $0x1,%al
jne    1179 <getMessage+0x69>
jmp    11bc <getMessage+0xac>
movsbl -0xa(%rbp),%eax
cmp    $0x7d,%eax
jne    119a <getMessage+0x8a>
mov    $0x0,%al
call   1030 <USARTGetc@plt>
mov    %al,-0xa(%rbp)
movsbl -0xa(%rbp),%eax
xor    $0x20,%eax
mov    %al,-0xa(%rbp)
mov    -0xa(%rbp),%dl
mov    -0x8(%rbp),%rax
movzbl -0xb(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0xb(%rbp),%al
add    $0x1,%al
mov    %al,-0xb(%rbp)
mov    $0x0,%al
call   1030 <USARTGetc@plt>
jmp    1147 <getMessage+0x37>
movsbl -0xb(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
nopl   (%rax)
xor    %eax,%eax
call   1030 <USARTGetc@plt>
cmp    $0x7e,%al
jne    1120 <getMessage+0x10>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <USARTGetc@plt>
cmp    $0x7e,%al
je     117d <getMessage+0x6d>
test   %r15b,%r15b
je     117d <getMessage+0x6d>
mov    %eax,%ebp
movzbl %r15b,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x7d,%bpl
jne    1163 <getMessage+0x53>
xor    %eax,%eax
call   1030 <USARTGetc@plt>
mov    %eax,%ebp
xor    $0x20,%bpl
mov    %bpl,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <USARTGetc@plt>
cmp    $0x7e,%bpl
je     117d <getMessage+0x6d>
cmp    %r15,%rbx
jb     1150 <getMessage+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define signext i8 @getMessage(i8* %0, i8 zeroext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
store i8* %0, i8** %3
store i8 %1, i8* %4
store i8 0, i8* %6
br label %7
7: ; preds = %9, %2
%8 = call signext i8 (...) @USARTGetc()
store i8 %8, i8* %5
br label %9
9: ; preds = %7
%10 = load i8, i8* %5
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 126
br i1 %12, label %7, label %13
13: ; preds = %9
%14 = call signext i8 (...) @USARTGetc()
store i8 %14, i8* %5
br label %15
15: ; preds = %37, %13
%16 = load i8, i8* %5
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 126
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i8, i8* %6
%21 = zext i8 %20 to i32
%22 = load i8, i8* %4
%23 = zext i8 %22 to i32
%24 = icmp slt i32 %21, %23
br label %25
25: ; preds = %19, %15
%26 = phi i1 [ false, %15 ], [ %24, %19 ]
br i1 %26, label %27, label %46
27: ; preds = %25
%28 = load i8, i8* %5
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 125
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = call signext i8 (...) @USARTGetc()
store i8 %32, i8* %5
%33 = load i8, i8* %5
%34 = sext i8 %33 to i32
%35 = xor i32 %34, 32
%36 = trunc i32 %35 to i8
store i8 %36, i8* %5
br label %37
37: ; preds = %31, %27
%38 = load i8, i8* %5
%39 = load i8*, i8** %3
%40 = load i8, i8* %6
%41 = zext i8 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 %38, i8* %42
%43 = load i8, i8* %6
%44 = add i8 %43, 1
store i8 %44, i8* %6
%45 = call signext i8 (...) @USARTGetc()
br label %15
46: ; preds = %25
%47 = load i8, i8* %6
ret i8 %47
}

LLVM IR (-O2):
define signext i8 @getMessage(i8* nocapture writeonly %0, i8 zeroext %1) {
br label %3
3: ; preds = %3, %2
%4 = tail call signext i8 (...) @USARTGetc()
%5 = icmp eq i8 %4, 126
br i1 %5, label %6, label %3
6: ; preds = %3
%7 = tail call signext i8 (...) @USARTGetc()
%8 = icmp ne i8 %7, 126
%9 = icmp ne i8 %1, 0
%10 = and i1 %8, %9
br i1 %10, label %11, label %30
11: ; preds = %6
%12 = zext i8 %1 to i64
br label %13
13: ; preds = %11, %20
%14 = phi i64 [ 0, %11 ], [ %23, %20 ]
%15 = phi i8 [ %7, %11 ], [ %21, %20 ]
%16 = icmp eq i8 %15, 125
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call signext i8 (...) @USARTGetc()
%19 = xor i8 %18, 32
br label %20
20: ; preds = %17, %13
%21 = phi i8 [ %19, %17 ], [ %15, %13 ]
%22 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 %21, i8* %22
%23 = add nuw nsw i64 %14, 1
%24 = tail call signext i8 (...) @USARTGetc()
%25 = icmp ne i8 %21, 126
%26 = icmp ult i64 %23, %12
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %13, label %28
28: ; preds = %20
%29 = trunc i64 %23 to i8
br label %30
30: ; preds = %28, %6
%31 = phi i8 [ 0, %6 ], [ %29, %28 ]
ret i8 %31
}