#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 */
int /*<<< orphan*/ uart_putc(char);
int /*<<< orphan*/ uart_puts(char *);
void uart_gets(char *buffer) {
  int i = 0;
  while (1) {
    while (1) {
      if ((*(volatile unsigned int *)(0x7F005010)) & 1)
        break;
    }
    buffer[i] = (*(volatile unsigned int *)(0x7F005024));
    if (buffer[i] == 8 && i > 0) {
      uart_putc(buffer[i--]);
      buffer[i] = ' ';
      uart_putc(buffer[i]);
      uart_putc(8);
      continue;
    } else if (buffer[i] == 8 && i == 0) {
      continue;
    }
    if (i != 79) {
      uart_putc(buffer[i]);
    } else if (i == 79 && buffer[i] == 0xD) {
      uart_puts("");
      buffer[i] = '\0';
      return;
    }
    if (i == 0 && buffer[i] == 0xD) {
      buffer[i] = '\0';
      uart_puts("");
      return;
    }
    if (i > 0 && buffer[i] == 0xD) {
      uart_puts("");
      buffer[i] = '\0';
      return;
    }
    if (++i == 80)
      i = 79;
  }
}


x86 assembly (clang, -O0):
<uart_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
jmp    1138 <uart_gets+0x18>
mov    $0x7f005010,%eax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1150 <uart_gets+0x30>
jmp    1155 <uart_gets+0x35>
jmp    1138 <uart_gets+0x18>
mov    $0x7f005024,%eax
mov    (%rax),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x8,%eax
jne    11cf <uart_gets+0xaf>
cmpl   $0x0,-0xc(%rbp)
jle    11cf <uart_gets+0xaf>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <uart_putc@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <uart_putc@plt>
mov    $0x8,%edi
call   1030 <uart_putc@plt>
jmp    1133 <uart_gets+0x13>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x8,%eax
jne    11f3 <uart_gets+0xd3>
cmpl   $0x0,-0xc(%rbp)
jne    11f3 <uart_gets+0xd3>
jmp    1133 <uart_gets+0x13>
jmp    11f8 <uart_gets+0xd8>
cmpl   $0x4f,-0xc(%rbp)
je     1218 <uart_gets+0xf8>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <uart_putc@plt>
jmp    1259 <uart_gets+0x139>
cmpl   $0x4f,-0xc(%rbp)
jne    1254 <uart_gets+0x134>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1254 <uart_gets+0x134>
lea    0xdc2(%rip),%rdi
call   1040 <uart_puts@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12ef <uart_gets+0x1cf>
jmp    1259 <uart_gets+0x139>
cmpl   $0x0,-0xc(%rbp)
jne    1295 <uart_gets+0x175>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1295 <uart_gets+0x175>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xd75(%rip),%rdi
call   1040 <uart_puts@plt>
jmp    12ef <uart_gets+0x1cf>
cmpl   $0x0,-0xc(%rbp)
jle    12d1 <uart_gets+0x1b1>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    12d1 <uart_gets+0x1b1>
lea    0xd45(%rip),%rdi
call   1040 <uart_puts@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12ef <uart_gets+0x1cf>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x50,%eax
jne    12ea <uart_gets+0x1ca>
movl   $0x4f,-0xc(%rbp)
jmp    1133 <uart_gets+0x13>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_gets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %edx,%edx
mov    $0x4f,%r14d
jmp    1166 <uart_gets+0x46>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xffffffff,%ebp
movsbl %al,%edi
call   1030 <uart_putc@plt>
movb   $0x20,(%r15,%rbp,1)
mov    $0x20,%edi
call   1030 <uart_putc@plt>
mov    $0x8,%edi
call   1030 <uart_putc@plt>
mov    %ebp,%edx
mov    %edx,%ebp
nopl   0x0(%rax,%rax,1)
testl  $0x1,0x7f005010
je     1170 <uart_gets+0x50>
mov    0x7f005024,%eax
movslq %ebp,%rbx
mov    %al,(%r15,%rbx,1)
mov    %eax,%ecx
shl    $0x18,%ecx
cmp    $0x8000000,%ecx
jne    119c <uart_gets+0x7c>
test   %ebp,%ebp
jg     1140 <uart_gets+0x20>
cmp    $0x8000000,%ecx
jne    11ad <uart_gets+0x8d>
mov    $0x0,%edx
test   %ebp,%ebp
je     1166 <uart_gets+0x46>
cmp    $0x4f,%ebp
jne    11c0 <uart_gets+0xa0>
cmp    $0xd000000,%ecx
je     11ef <uart_gets+0xcf>
test   %ebp,%ebp
jne    11d3 <uart_gets+0xb3>
jmp    11cc <uart_gets+0xac>
movsbl %al,%edi
call   1030 <uart_putc@plt>
test   %ebp,%ebp
jne    11d3 <uart_gets+0xb3>
cmpb   $0xd,(%r15,%rbx,1)
je     120b <uart_gets+0xeb>
test   %ebp,%ebp
jle    11de <uart_gets+0xbe>
cmpb   $0xd,(%r15,%rbx,1)
je     11ef <uart_gets+0xcf>
add    $0x1,%ebp
cmp    $0x50,%ebp
cmove  %r14d,%ebp
mov    %ebp,%edx
jmp    1166 <uart_gets+0x46>
lea    0xe0a(%rip),%rdi
call   1040 <uart_puts@plt>
movb   $0x0,(%r15,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%r15,%rbx,1)
lea    0xde9(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <uart_puts@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define void @uart_gets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %4
4: ; preds = %1, %28, %58, %127
br label %5
5: ; preds = %4, %10
%6 = load volatile i32, i32* inttoptr (i64 2130726928 to i32*)
%7 = and i32 %6, 1
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %5
br label %11
10: ; preds = %5
br label %5
11: ; preds = %9
%12 = load volatile i32, i32* inttoptr (i64 2130726948 to i32*)
%13 = trunc i32 %12 to i8
%14 = load i8*, i8** %2
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 %13, i8* %17
%18 = load i8*, i8** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 8
br i1 %24, label %25, label %47
25: ; preds = %11
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %47
28: ; preds = %25
%29 = load i8*, i8** %2
%30 = load i32, i32* %3
%31 = add nsw i32 %30, -1
store i32 %31, i32* %3
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @uart_putc(i8 signext %34)
%36 = load i8*, i8** %2
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 32, i8* %39
%40 = load i8*, i8** %2
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = call i32 @uart_putc(i8 signext %44)
%46 = call i32 @uart_putc(i8 signext 8)
br label %4
47: ; preds = %25, %11
%48 = load i8*, i8** %2
%49 = load i32, i32* %3
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 8
br i1 %54, label %55, label %59
55: ; preds = %47
%56 = load i32, i32* %3
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %55
br label %4
59: ; preds = %55, %47
br label %60
60: ; preds = %59
%61 = load i32, i32* %3
%62 = icmp ne i32 %61, 79
br i1 %62, label %63, label %70
63: ; preds = %60
%64 = load i8*, i8** %2
%65 = load i32, i32* %3
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = call i32 @uart_putc(i8 signext %68)
br label %88
70: ; preds = %60
%71 = load i32, i32* %3
%72 = icmp eq i32 %71, 79
br i1 %72, label %73, label %87
73: ; preds = %70
%74 = load i8*, i8** %2
%75 = load i32, i32* %3
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 13
br i1 %80, label %81, label %87
81: ; preds = %73
%82 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%83 = load i8*, i8** %2
%84 = load i32, i32* %3
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
store i8 0, i8* %86
br label %128
87: ; preds = %73, %70
br label %88
88: ; preds = %87, %63
%89 = load i32, i32* %3
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %105
91: ; preds = %88
%92 = load i8*, i8** %2
%93 = load i32, i32* %3
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i8, i8* %92, i64 %94
%96 = load i8, i8* %95
%97 = sext i8 %96 to i32
%98 = icmp eq i32 %97, 13
br i1 %98, label %99, label %105
99: ; preds = %91
%100 = load i8*, i8** %2
%101 = load i32, i32* %3
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %100, i64 %102
store i8 0, i8* %103
%104 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %128
105: ; preds = %91, %88
%106 = load i32, i32* %3
%107 = icmp sgt i32 %106, 0
br i1 %107, label %108, label %122
108: ; preds = %105
%109 = load i8*, i8** %2
%110 = load i32, i32* %3
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds i8, i8* %109, i64 %111
%113 = load i8, i8* %112
%114 = sext i8 %113 to i32
%115 = icmp eq i32 %114, 13
br i1 %115, label %116, label %122
116: ; preds = %108
%117 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%118 = load i8*, i8** %2
%119 = load i32, i32* %3
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds i8, i8* %118, i64 %120
store i8 0, i8* %121
br label %128
122: ; preds = %108, %105
%123 = load i32, i32* %3
%124 = add nsw i32 %123, 1
store i32 %124, i32* %3
%125 = icmp eq i32 %124, 80
br i1 %125, label %126, label %127
126: ; preds = %122
store i32 79, i32* %3
br label %127
127: ; preds = %126, %122
br label %4
128: ; preds = %116, %99, %81
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define void @uart_gets(i8* nocapture %0) {
br label %2
2: ; preds = %24, %1
%3 = phi i32 [ 0, %1 ], [ %25, %24 ]
br label %4
4: ; preds = %4, %2
%5 = load volatile i32, i32* inttoptr (i64 2130726928 to i32*)
%6 = and i32 %5, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %4, label %8
8: ; preds = %4
%9 = load volatile i32, i32* inttoptr (i64 2130726948 to i32*)
%10 = trunc i32 %9 to i8
%11 = sext i32 %3 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %10, i8* %12
%13 = shl i32 %9, 24
%14 = icmp eq i32 %13, 134217728
%15 = icmp sgt i32 %3, 0
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %17, label %26
17: ; preds = %8
%18 = add nsw i32 %3, -1
%19 = tail call i32 @uart_putc(i8 signext %10)
%20 = zext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
store i8 32, i8* %21
%22 = tail call i32 @uart_putc(i8 signext 32)
%23 = tail call i32 @uart_putc(i8 signext 8)
br label %24
24: ; preds = %17, %49, %26
%25 = phi i32 [ %18, %17 ], [ 0, %26 ], [ %52, %49 ]
br label %2
26: ; preds = %8
%27 = icmp eq i32 %3, 0
%28 = select i1 %14, i1 %27, i1 false
br i1 %28, label %24, label %29
29: ; preds = %26
%30 = icmp eq i32 %3, 79
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = tail call i32 @uart_putc(i8 signext %10)
br label %37
33: ; preds = %29
%34 = icmp eq i32 %13, 218103808
br i1 %34, label %35, label %37
35: ; preds = %33
%36 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8 0, i8* %12
br label %53
37: ; preds = %33, %31
br i1 %27, label %38, label %43
38: ; preds = %37
%39 = load i8, i8* %12
%40 = icmp eq i8 %39, 13
br i1 %40, label %41, label %43
41: ; preds = %38
store i8 0, i8* %12
%42 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %53
43: ; preds = %38, %37
br i1 %15, label %44, label %49
44: ; preds = %43
%45 = load i8, i8* %12
%46 = icmp eq i8 %45, 13
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8 0, i8* %12
br label %53
49: ; preds = %44, %43
%50 = add nsw i32 %3, 1
%51 = icmp eq i32 %50, 80
%52 = select i1 %51, i32 79, i32 %50
br label %24
53: ; preds = %47, %41, %35
ret void
}