#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 getc();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putc(char);
int gets(char *buf, int size) {
  int i = 0, done = 0;
  char c;
  if (!size) {
    size = -1;
  }
  while (!done) {
    c = getc();
    if (i == size && c != '\b') {
      continue;
    }
    switch (c) {
    case ('\r'):
      printf("\r\n");
      *(buf + i) = '\0';
      done = 1;
      break;
    case ('\b'):
      if (i > 0) {
        printf("\b \b");
        *(buf + i) = '\0';
        i--;
      }
      i--;
      break;
    default:
      putc(c);
      *(buf + i) = c;
      break;
    }
    i++;
  }
  return (i);
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    115e <gets+0x2e>
movl   $0xffffffff,-0xc(%rbp)
jmp    1163 <gets+0x33>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1179 <gets+0x49>
jmp    1249 <gets+0x119>
mov    $0x0,%al
call   1050 <getc@plt>
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11a1 <gets+0x71>
movsbl -0x15(%rbp),%eax
cmp    $0x8,%eax
je     11a1 <gets+0x71>
jmp    1163 <gets+0x33>
movsbl -0x15(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x8,%eax
je     11eb <gets+0xbb>
jmp    11b6 <gets+0x86>
mov    -0x1c(%rbp),%eax
sub    $0xd,%eax
jne    1224 <gets+0xf4>
jmp    11c7 <gets+0x97>
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x14(%rbp)
jmp    123b <gets+0x10b>
cmpl   $0x0,-0x10(%rbp)
jle    1216 <gets+0xe6>
lea    0xe07(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    123b <gets+0x10b>
movsbl -0x15(%rbp),%edi
call   1040 <putc@plt>
mov    -0x15(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1163 <gets+0x33>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
cmp    $0x1,%esi
sbb    %r14d,%r14d
or     %esi,%r14d
xor    %ebp,%ebp
lea    0xea4(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
jne    11e5 <gets+0xa5>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getc@plt>
cmp    %r14d,%ebp
jne    1186 <gets+0x46>
cmp    $0x8,%al
jne    1170 <gets+0x30>
movsbl %al,%r13d
cmp    $0x8,%r13d
je     11b0 <gets+0x70>
cmp    $0xd,%r13d
jne    11cf <gets+0x8f>
mov    %r15,%rdi
call   1030 <puts@plt>
movslq %ebp,%rax
movb   $0x0,(%r12,%rax,1)
mov    $0x1,%ebx
add    $0x1,%ebp
jmp    1170 <gets+0x30>
test   %ebp,%ebp
jle    11ca <gets+0x8a>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
mov    %ebp,%eax
movb   $0x0,(%r12,%rax,1)
add    $0xffffffff,%ebp
add    $0xffffffff,%ebp
jmp    11de <gets+0x9e>
mov    %r13d,%edi
call   1050 <putc@plt>
movslq %ebp,%rax
mov    %r13b,(%r12,%rax,1)
xor    %ebx,%ebx
add    $0x1,%ebp
jmp    1170 <gets+0x30>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str.1 = [4 x i8] c"\08 \08\00"
@str = [2 x i8] c"\0D\00"
define i32 @gets(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = select i1 %3, i32 -1, i32 %1
br label %5
5: ; preds = %36, %2
%6 = phi i32 [ %39, %36 ], [ 0, %2 ]
%7 = phi i32 [ %38, %36 ], [ 0, %2 ]
%8 = icmp eq i32 %6, %4
br label %9
9: ; preds = %5, %12
%10 = phi i32 [ 0, %12 ], [ %7, %5 ]
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %40
12: ; preds = %9
%13 = tail call signext i8 (...) @getc()
%14 = icmp ne i8 %13, 8
%15 = select i1 %8, i1 %14, i1 false
br i1 %15, label %9, label %16
16: ; preds = %12
%17 = sext i8 %13 to i32
switch i32 %17, label %32 [
i32 13, label %18
i32 8, label %22
]
18: ; preds = %16
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%20 = sext i32 %6 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
store i8 0, i8* %21
br label %36
22: ; preds = %16
%23 = icmp sgt i32 %6, 0
br i1 %23, label %24, label %29
24: ; preds = %22
%25 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%26 = zext i32 %6 to i64
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 0, i8* %27
%28 = add nsw i32 %6, -1
br label %29
29: ; preds = %24, %22
%30 = phi i32 [ %28, %24 ], [ %6, %22 ]
%31 = add nsw i32 %30, -1
br label %36
32: ; preds = %16
%33 = tail call i32 @putc(i8 signext %13)
%34 = sext i32 %6 to i64
%35 = getelementptr inbounds i8, i8* %0, i64 %34
store i8 %13, i8* %35
br label %36
36: ; preds = %32, %29, %18
%37 = phi i32 [ %6, %32 ], [ %31, %29 ], [ %6, %18 ]
%38 = phi i32 [ 0, %32 ], [ 0, %29 ], [ 1, %18 ]
%39 = add nsw i32 %37, 1
br label %5
40: ; preds = %9
ret i32 %6
}