#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 uart_getchar();
int /*<<< orphan*/ uart_putchar(char);
int /*<<< orphan*/ uart_puts(char *);
char *uart_getsn(char *str, int num) {
  int i;
  for (i = 0; i < num - 1; i++) {
    char c = uart_getchar();
    str[i] = c;
    if (c == '\b') {
      uart_puts(" \b");
      i -= 2;
    } else if (c == '\r') {
      str[i] = 0;
      uart_putchar('\n');
      return str;
    }
  }
  str[num] = 0;
  return str;
}


x86 assembly (clang, -O0):
<uart_getsn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11de <uart_getsn+0xae>
mov    $0x0,%al
call   1050 <uart_getchar@plt>
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x19(%rbp),%eax
cmp    $0x8,%eax
jne    1196 <uart_getsn+0x66>
lea    0xe7d(%rip),%rdi
call   1040 <uart_puts@plt>
mov    -0x18(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    11cb <uart_getsn+0x9b>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    11c6 <uart_getsn+0x96>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0xa,%edi
call   1030 <uart_putchar@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f2 <uart_getsn+0xc2>
jmp    11cb <uart_getsn+0x9b>
jmp    11d0 <uart_getsn+0xa0>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1146 <uart_getsn+0x16>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_getsn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r12
cmp    $0x2,%esi
jl     1199 <uart_getsn+0x69>
lea    -0x1(%r14),%ebp
lea    0xeb2(%rip),%r15
xor    %ebx,%ebx
jmp    1172 <uart_getsn+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1040 <uart_puts@plt>
add    $0xfffffffe,%ebx
add    $0x1,%ebx
cmp    %ebp,%ebx
jge    1199 <uart_getsn+0x69>
xor    %eax,%eax
call   1050 <uart_getchar@plt>
movslq %ebx,%rcx
mov    %al,(%r12,%rcx,1)
cmp    $0x8,%al
je     1160 <uart_getsn+0x30>
cmp    $0xd,%al
jne    116b <uart_getsn+0x3b>
movb   $0x0,(%r12,%rcx,1)
mov    $0xa,%edi
call   1030 <uart_putchar@plt>
jmp    11a1 <uart_getsn+0x71>
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c" \08\00"
define i8* @uart_getsn(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %40, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = sub nsw i32 %10, 1
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %43
13: ; preds = %8
%14 = call signext i8 (...) @uart_getchar()
store i8 %14, i8* %7
%15 = load i8, i8* %7
%16 = load i8*, i8** %4
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
store i8 %15, i8* %19
%20 = load i8, i8* %7
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 8
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, 2
store i32 %26, i32* %6
br label %39
27: ; preds = %13
%28 = load i8, i8* %7
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 13
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = call i32 @uart_putchar(i8 signext 10)
%37 = load i8*, i8** %4
store i8* %37, i8** %3
br label %49
38: ; preds = %27
br label %39
39: ; preds = %38, %23
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %8
43: ; preds = %8
%44 = load i8*, i8** %4
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %4
store i8* %48, i8** %3
br label %49
49: ; preds = %43, %31
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [3 x i8] c" \08\00"
define i8* @uart_getsn(i8* writeonly %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %20
5: ; preds = %2, %16
%6 = phi i32 [ %18, %16 ], [ 0, %2 ]
%7 = tail call signext i8 (...) @uart_getchar()
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %7, i8* %9
switch i8 %7, label %16 [
i8 8, label %10
i8 13, label %13
]
10: ; preds = %5
%11 = tail call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = add nsw i32 %6, -2
br label %16
13: ; preds = %5
%14 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 0, i8* %14
%15 = tail call i32 @uart_putchar(i8 signext 10)
br label %23
16: ; preds = %5, %10
%17 = phi i32 [ %6, %5 ], [ %12, %10 ]
%18 = add nsw i32 %17, 1
%19 = icmp slt i32 %18, %3
br i1 %19, label %5, label %20
20: ; preds = %16, %2
%21 = sext i32 %1 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %13, %20
ret i8* %0
}