#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 EOF;
int getchar();
int /*<<< orphan*/ putchar(int);
char *gets(char *buf) {
  int i;
  int c;
  i = 0;
  while (1) {
    c = getchar();
    if (c == EOF)
      break;
    if (c == '\r' || c == '\n') {
      putchar('\n');
      break;
    }
    if (c == '\b' || c == 0x7f) {
      if (i > 0) {
        putchar('\b');
        putchar(' ');
        putchar('\b');
        i--;
      }
      continue;
    }
    buf[i++] = c;
    putchar(c);
  }
  buf[i] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2ea1(%rip),%rcx
cmp    (%rcx),%eax
jne    1154 <gets+0x34>
jmp    11e8 <gets+0xc8>
cmpl   $0xd,-0x10(%rbp)
je     1168 <gets+0x48>
cmpl   $0xa,-0x10(%rbp)
jne    1177 <gets+0x57>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11e8 <gets+0xc8>
cmpl   $0x8,-0x10(%rbp)
je     118b <gets+0x6b>
cmpl   $0x7f,-0x10(%rbp)
jne    11c1 <gets+0xa1>
cmpl   $0x0,-0xc(%rbp)
jle    11bc <gets+0x9c>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1133 <gets+0x13>
mov    -0x10(%rbp),%eax
mov    %al,%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)
mov    -0x10(%rbp),%edi
call   1030 <putchar@plt>
jmp    1133 <gets+0x13>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getchar@plt>
mov    0x2eb1(%rip),%rbx
cmp    (%rbx),%eax
je     11ab <gets+0x8b>
xor    %ebp,%ebp
jmp    1160 <gets+0x40>
cmp    $0xa,%eax
je     11a1 <gets+0x81>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%r14,%rcx,1)
mov    %eax,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    (%rbx),%eax
je     11ab <gets+0x8b>
cmp    $0xc,%eax
jg     1170 <gets+0x50>
cmp    $0x8,%eax
je     1175 <gets+0x55>
jmp    113f <gets+0x1f>
nopl   0x0(%rax)
cmp    $0x7f,%eax
jne    119c <gets+0x7c>
test   %ebp,%ebp
jle    1155 <gets+0x35>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
add    $0xffffffff,%ebp
jmp    1155 <gets+0x35>
cmp    $0xd,%eax
jne    1144 <gets+0x24>
mov    $0xa,%edi
call   1030 <putchar@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i8* @gets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %5
5: ; preds = %1, %34, %35
%6 = call i32 (...) @getchar()
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %5
br label %45
11: ; preds = %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 13
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %19
17: ; preds = %14, %11
%18 = call i32 @putchar(i32 10)
br label %45
19: ; preds = %14
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 8
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 127
br i1 %24, label %25, label %35
25: ; preds = %22, %19
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = call i32 @putchar(i32 8)
%30 = call i32 @putchar(i32 32)
%31 = call i32 @putchar(i32 8)
%32 = load i32, i32* %3
%33 = add nsw i32 %32, -1
store i32 %33, i32* %3
br label %34
34: ; preds = %28, %25
br label %5
35: ; preds = %22
%36 = load i32, i32* %4
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %2
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 %37, i8* %42
%43 = load i32, i32* %4
%44 = call i32 @putchar(i32 %43)
br label %5
45: ; preds = %17, %10
%46 = load i8*, i8** %2
%47 = load i32, i32* %3
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %2
ret i8* %50
}

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