#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 int uint16_t;

/* Variables and functions */
char UART_InChar();
int /*<<< orphan*/ UART_OutChar(char);
void UART_InString(char *bufPt, uint16_t max) {
  int length = 0;
  char character;
  character = UART_InChar();
  while (character != 0x0D) {
    if (character == 0x08) {
      if (length) {
        bufPt--;
        length--;
        UART_OutChar(0x08);
      }
    } else if (length < max) {
      *bufPt = character;
      bufPt++;
      length++;
      UART_OutChar(character);
    }
    character = UART_InChar();
  }
  *bufPt = 0;
}


x86 assembly (clang, -O0):
<UART_InString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <UART_InChar@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xd,%eax
je     11cf <UART_InString+0xaf>
movsbl -0x11(%rbp),%eax
cmp    $0x8,%eax
jne    1188 <UART_InString+0x68>
cmpl   $0x0,-0x10(%rbp)
je     1183 <UART_InString+0x63>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    $0x8,%edi
call   1040 <UART_OutChar@plt>
jmp    11c0 <UART_InString+0xa0>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11bb <UART_InString+0x9b>
mov    -0x11(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movsbl -0x11(%rbp),%edi
call   1040 <UART_OutChar@plt>
jmp    11c0 <UART_InString+0xa0>
mov    $0x0,%al
call   1030 <UART_InChar@plt>
mov    %al,-0x11(%rbp)
jmp    1140 <UART_InString+0x20>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UART_InString>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
mov    %eax,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1030 <UART_InChar@plt>
cmp    $0xd,%al
je     1186 <UART_InString+0x66>
cmp    $0x8,%al
je     1170 <UART_InString+0x50>
cmp    %ebp,%ebx
jge    1130 <UART_InString+0x10>
mov    %al,(%r14)
mov    $0x1,%ecx
mov    $0x1,%edx
add    %rdx,%r14
add    %ecx,%ebx
movsbl %al,%edi
call   1040 <UART_OutChar@plt>
mov    %ebx,%eax
jmp    112b <UART_InString+0xb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
test   %ebx,%ebx
je     112b <UART_InString+0xb>
mov    $0x8,%al
mov    $0xffffffff,%ecx
mov    $0xffffffffffffffff,%rdx
jmp    1150 <UART_InString+0x30>
movb   $0x0,(%r14)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @UART_InString(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%7 = call signext i8 (...) @UART_InChar()
store i8 %7, i8* %6
br label %8
8: ; preds = %40, %2
%9 = load i8, i8* %6
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 13
br i1 %11, label %12, label %42
12: ; preds = %8
%13 = load i8, i8* %6
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 8
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i32 -1
store i8* %21, i8** %3
%22 = load i32, i32* %5
%23 = add nsw i32 %22, -1
store i32 %23, i32* %5
%24 = call i32 @UART_OutChar(i8 signext 8)
br label %25
25: ; preds = %19, %16
br label %40
26: ; preds = %12
%27 = load i32, i32* %5
%28 = load i32, i32* %4
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i8, i8* %6
%32 = load i8*, i8** %3
store i8 %31, i8* %32
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %3
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
%37 = load i8, i8* %6
%38 = call i32 @UART_OutChar(i8 signext %37)
br label %39
39: ; preds = %30, %26
br label %40
40: ; preds = %39, %25
%41 = call signext i8 (...) @UART_InChar()
store i8 %41, i8* %6
br label %8
42: ; preds = %8
%43 = load i8*, i8** %3
store i8 0, i8* %43
ret void
}

LLVM IR (-O2):
define void @UART_InString(i8* nocapture writeonly %0, i32 %1) {
br label %3
3: ; preds = %15, %2
%4 = phi i8* [ %19, %15 ], [ %0, %2 ]
%5 = phi i32 [ %20, %15 ], [ 0, %2 ]
br label %6
6: ; preds = %3, %11
%7 = phi i32 [ %5, %3 ], [ 0, %11 ]
%8 = icmp slt i32 %7, %1
br label %9
9: ; preds = %6, %13
%10 = tail call signext i8 (...) @UART_InChar()
switch i8 %10, label %13 [
i8 13, label %22
i8 8, label %11
]
11: ; preds = %9
%12 = icmp eq i32 %7, 0
br i1 %12, label %6, label %15
13: ; preds = %9
br i1 %8, label %14, label %9
14: ; preds = %13
store i8 %10, i8* %4
br label %15
15: ; preds = %11, %14
%16 = phi i64 [ 1, %14 ], [ -1, %11 ]
%17 = phi i32 [ 1, %14 ], [ -1, %11 ]
%18 = phi i8 [ %10, %14 ], [ 8, %11 ]
%19 = getelementptr inbounds i8, i8* %4, i64 %16
%20 = add nsw i32 %7, %17
%21 = tail call i32 @UART_OutChar(i8 signext %18)
br label %3
22: ; preds = %9
store i8 0, i8* %4
ret void
}