#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();
int /*<<< orphan*/ putchar(char);
char *gets(char *s) {
  char c;
  unsigned int count = 0;
  while (1) {
    c = getchar();
    switch (c) {
    case '\b':
      if (count) {
        putchar('\b');
        putchar(' ');
        putchar('\b');
        --s;
        --count;
      }
      break;
    case '\n':
    case '\r':
      putchar('\r');
      putchar('\n');
      *s = 0;
      return s;
    default:
      *s++ = c;
      ++count;
      putchar(c);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x8,%eax
je     1174 <gets+0x54>
jmp    1152 <gets+0x32>
mov    -0x14(%rbp),%eax
sub    $0xa,%eax
je     11b6 <gets+0x96>
jmp    1163 <gets+0x43>
mov    -0x14(%rbp),%eax
sub    $0xd,%eax
je     11b6 <gets+0x96>
jmp    11db <gets+0xbb>
cmpl   $0x0,-0x10(%rbp)
je     11b1 <gets+0x91>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1201 <gets+0xe1>
mov    $0xd,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x9(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movsbl -0x9(%rbp),%edi
call   1030 <putchar@plt>
jmp    1133 <gets+0x13>

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getchar@plt>
movsbl %al,%edi
cmp    $0x8,%edi
jne    1170 <gets+0x50>
xor    %eax,%eax
test   %ebp,%ebp
je     1130 <gets+0x10>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
add    $0xffffffffffffffff,%rbx
add    $0xffffffff,%ebp
mov    %ebp,%eax
jmp    1130 <gets+0x10>
cmp    $0xa,%edi
je     118d <gets+0x6d>
cmp    $0xd,%edi
je     118d <gets+0x6d>
mov    %dil,(%rbx)
add    $0x1,%rbx
add    $0x1,%ebp
call   1030 <putchar@plt>
mov    %ebp,%eax
jmp    1130 <gets+0x10>
mov    $0xd,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x0,(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @gets(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
br label %5
5: ; preds = %1, %34
%6 = call signext i8 (...) @getchar()
store i8 %6, i8* %3
%7 = load i8, i8* %3
%8 = sext i8 %7 to i32
switch i32 %8, label %26 [
i32 8, label %9
i32 10, label %21
i32 13, label %21
]
9: ; preds = %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = call i32 @putchar(i8 signext 8)
%14 = call i32 @putchar(i8 signext 32)
%15 = call i32 @putchar(i8 signext 8)
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i32 -1
store i8* %17, i8** %2
%18 = load i32, i32* %4
%19 = add i32 %18, -1
store i32 %19, i32* %4
br label %20
20: ; preds = %12, %9
br label %34
21: ; preds = %5, %5
%22 = call i32 @putchar(i8 signext 13)
%23 = call i32 @putchar(i8 signext 10)
%24 = load i8*, i8** %2
store i8 0, i8* %24
%25 = load i8*, i8** %2
ret i8* %25
26: ; preds = %5
%27 = load i8, i8* %3
%28 = load i8*, i8** %2
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %2
store i8 %27, i8* %28
%30 = load i32, i32* %4
%31 = add i32 %30, 1
store i32 %31, i32* %4
%32 = load i8, i8* %3
%33 = call i32 @putchar(i8 signext %32)
br label %34
34: ; preds = %26, %20
br label %5
}

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