#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*/ delete_char();
char getchar();
int /*<<< orphan*/ putchar(char);
char *gets(int size) {
  char res[size];
  char lc = 0;
  int i = 0;
  while (1) {
    char c = getchar();
    if (c != lc) {
      res[i] = c;
      lc = c;
      switch (c) {
      case 0x8:
        res[i - 1] = 0;
        delete_char();
        i--;
        break;
      case '\n':
        if (i == 0) {
          i--;
          break;
        }
        res[i + 1] = 0;
        return res;
      default:
        putchar(c);
      }
      if (i + 1 < size) {
        i++;
      } else {
        res[i + 1] = 0;
        return res;
      }
    }
  }
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    1171 <gets+0x41>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
movsbl -0x21(%rbp),%ecx
cmp    %ecx,%eax
je     1270 <gets+0x140>
mov    -0x38(%rbp),%rax
mov    -0x29(%rbp),%dl
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x29(%rbp),%al
mov    %al,-0x21(%rbp)
movsbl -0x29(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x8,%eax
je     11c5 <gets+0x95>
jmp    11b4 <gets+0x84>
mov    -0x3c(%rbp),%eax
sub    $0xa,%eax
je     11eb <gets+0xbb>
jmp    1224 <gets+0xf4>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x0,%al
call   1050 <delete_char@plt>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    122d <gets+0xfd>
cmpl   $0x0,-0x28(%rbp)
jne    1203 <gets+0xd3>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    122d <gets+0xfd>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1275 <gets+0x145>
movsbl -0x29(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jge    124a <gets+0x11a>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    126b <gets+0x13b>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1275 <gets+0x145>
jmp    1270 <gets+0x140>
jmp    116c <gets+0x3c>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    %edi,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
xor    %ebx,%ebx
xor    %r12d,%r12d
jmp    1170 <gets+0x40>
call   1030 <putchar@plt>
mov    %r12d,%r13d
add    $0x1,%r13d
mov    %r13d,%r12d
cmp    %r14d,%r13d
jge    11b6 <gets+0x86>
nop
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    %bl,%al
je     1170 <gets+0x40>
mov    %eax,%ebx
movsbl %al,%edi
movslq %r12d,%r13
mov    %al,(%r15,%r13,1)
cmp    $0xa,%edi
je     11a4 <gets+0x74>
cmp    $0x8,%edi
jne    115b <gets+0x2b>
movb   $0x0,-0x1(%r15,%r13,1)
add    $0xffffffffffffffff,%r13
xor    %eax,%eax
call   1050 <delete_char@plt>
jmp    1163 <gets+0x33>
mov    $0xffffffff,%r13d
test   %r12d,%r12d
je     1163 <gets+0x33>
add    $0x1,%r12d
mov    %r12d,%r13d
movslq %r13d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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