#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 getchar();
scalar_t__ isprint(char);
int /*<<< orphan*/ putchar(char);
int uart_read_line(char *str) {
  int n = 0;
  char c;
  while (n < 100 - 1 && (c = getchar()) != '\n' && c != '\r') {
    if (c == 0x7F || c == 0x08) {
      if (n > 0) {
        str[--n] = '\0';
        putchar(c);
        putchar(' ');
        putchar(c);
      }
    } else if (isprint(c)) {
      str[n++] = c;
      putchar(c);
    }
  }
  str[n] = '\0';
  putchar('\n');
  return n;
}


x86 assembly (clang, -O0):
<uart_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x63,-0xc(%rbp)
mov    %al,-0xe(%rbp)
jge    117a <uart_read_line+0x4a>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0xe(%rbp)
je     117a <uart_read_line+0x4a>
movsbl -0xd(%rbp),%eax
cmp    $0xd,%eax
setne  %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
test   $0x1,%al
jne    118a <uart_read_line+0x5a>
jmp    1221 <uart_read_line+0xf1>
movsbl -0xd(%rbp),%eax
cmp    $0x7f,%eax
je     11a4 <uart_read_line+0x74>
movsbl -0xd(%rbp),%eax
cmp    $0x8,%eax
jne    11e3 <uart_read_line+0xb3>
cmpl   $0x0,-0xc(%rbp)
jle    11de <uart_read_line+0xae>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movsbl -0xd(%rbp),%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
movsbl -0xd(%rbp),%edi
call   1030 <putchar@plt>
jmp    121c <uart_read_line+0xec>
movsbl -0xd(%rbp),%edi
call   1050 <isprint@plt>
cmp    $0x0,%rax
je     1217 <uart_read_line+0xe7>
mov    -0xd(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0xc(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0xd(%rbp),%edi
call   1030 <putchar@plt>
jmp    121c <uart_read_line+0xec>
jmp    1143 <uart_read_line+0x13>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_read_line>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %r15d,%r15d
jmp    114e <uart_read_line+0x1e>
nopl   (%rax)
movsbl %bl,%edi
call   1030 <putchar@plt>
cmp    $0x63,%r15d
jge    11b6 <uart_read_line+0x86>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebx
cmp    $0xc,%al
jg     1170 <uart_read_line+0x40>
cmp    $0x8,%bl
je     1175 <uart_read_line+0x45>
cmp    $0xa,%bl
jne    119c <uart_read_line+0x6c>
jmp    11b6 <uart_read_line+0x86>
nopw   0x0(%rax,%rax,1)
cmp    $0x7f,%bl
jne    1197 <uart_read_line+0x67>
test   %r15d,%r15d
jle    1148 <uart_read_line+0x18>
add    $0xffffffff,%r15d
movb   $0x0,(%r14,%r15,1)
movsbl %bl,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
jmp    1140 <uart_read_line+0x10>
cmp    $0xd,%bl
je     11b6 <uart_read_line+0x86>
movsbl %bl,%edi
call   1050 <isprint@plt>
test   %rax,%rax
je     1148 <uart_read_line+0x18>
movslq %r15d,%rax
add    $0x1,%r15d
mov    %bl,(%r14,%rax,1)
jmp    1140 <uart_read_line+0x10>
movslq %r15d,%rbx
movb   $0x0,(%r14,%rbx,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @uart_read_line(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8
store i8* %0, i8** %2
store i32 0, i32* %3
br label %5
5: ; preds = %55, %1
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 99
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = call signext i8 (...) @getchar()
store i8 %9, i8* %4
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i8, i8* %4
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 13
br label %16
16: ; preds = %12, %8, %5
%17 = phi i1 [ false, %8 ], [ false, %5 ], [ %15, %12 ]
br i1 %17, label %18, label %56
18: ; preds = %16
%19 = load i8, i8* %4
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 127
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i8, i8* %4
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 8
br i1 %25, label %26, label %41
26: ; preds = %22, %18
%27 = load i32, i32* %3
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %26
%30 = load i8*, i8** %2
%31 = load i32, i32* %3
%32 = add nsw i32 %31, -1
store i32 %32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = load i8, i8* %4
%36 = call i32 @putchar(i8 signext %35)
%37 = call i32 @putchar(i8 signext 32)
%38 = load i8, i8* %4
%39 = call i32 @putchar(i8 signext %38)
br label %40
40: ; preds = %29, %26
br label %55
41: ; preds = %22
%42 = load i8, i8* %4
%43 = call i64 @isprint(i8 signext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %41
%46 = load i8, i8* %4
%47 = load i8*, i8** %2
%48 = load i32, i32* %3
%49 = add nsw i32 %48, 1
store i32 %49, i32* %3
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 %46, i8* %51
%52 = load i8, i8* %4
%53 = call i32 @putchar(i8 signext %52)
br label %54
54: ; preds = %45, %41
br label %55
55: ; preds = %54, %40
br label %5
56: ; preds = %16
%57 = load i8*, i8** %2
%58 = load i32, i32* %3
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 0, i8* %60
%61 = call i32 @putchar(i8 signext 10)
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
define i32 @uart_read_line(i8* nocapture writeonly %0) {
br label %2
2: ; preds = %1, %23
%3 = phi i32 [ 0, %1 ], [ %24, %23 ]
%4 = tail call signext i8 (...) @getchar()
switch i8 %4, label %13 [
i8 13, label %26
i8 10, label %26
i8 127, label %5
i8 8, label %5
]
5: ; preds = %2, %2
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %23
7: ; preds = %5
%8 = add nsw i32 %3, -1
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %10
%11 = tail call i32 @putchar(i8 signext %4)
%12 = tail call i32 @putchar(i8 signext 32)
br label %20
13: ; preds = %2
%14 = tail call i64 @isprint(i8 signext %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %13
%17 = add nsw i32 %3, 1
%18 = sext i32 %3 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 %4, i8* %19
br label %20
20: ; preds = %7, %16
%21 = phi i32 [ %17, %16 ], [ %8, %7 ]
%22 = tail call i32 @putchar(i8 signext %4)
br label %23
23: ; preds = %20, %13, %5
%24 = phi i32 [ %3, %5 ], [ %3, %13 ], [ %21, %20 ]
%25 = icmp slt i32 %24, 99
br i1 %25, label %2, label %26
26: ; preds = %2, %2, %23
%27 = phi i32 [ %3, %2 ], [ %3, %2 ], [ %24, %23 ]
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
store i8 0, i8* %29
%30 = tail call i32 @putchar(i8 signext 10)
ret i32 %27
}