#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 */
unsigned char getchar();
int /*<<< orphan*/ newline();
int /*<<< orphan*/ putchar(int);
unsigned char *gets(unsigned char *buff) {
  unsigned char ch;
  unsigned int ii = 0x0;
  while (ii < (80 - 1)) {
    ch = getchar();
    if (ch >= 0x20 && ch < 127) {
      buff[ii++] = ch;
      putchar(ch);
    } else if ((ch == '\n') || (ch == '\r')) {
      newline();
      break;
    } else if ((ch == 0x7f) || (ch == '\b')) {
      if (ii) {
        ii--;
        putchar(0x08);
        putchar(0x20);
        putchar(0x08);
      }
    }
  }
  buff[ii] = '\0';
  return buff;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x4f,-0x10(%rbp)
jae    121b <gets+0xeb>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0x20,%eax
jl     1196 <gets+0x66>
movzbl -0x9(%rbp),%eax
cmp    $0x7f,%eax
jge    1196 <gets+0x66>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
movzbl -0x9(%rbp),%edi
call   1040 <putchar@plt>
jmp    1216 <gets+0xe6>
movzbl -0x9(%rbp),%eax
cmp    $0xa,%eax
je     11b0 <gets+0x80>
movzbl -0x9(%rbp),%eax
cmp    $0xd,%eax
jne    11bc <gets+0x8c>
mov    $0x0,%al
call   1030 <newline@plt>
jmp    121b <gets+0xeb>
movzbl -0x9(%rbp),%eax
cmp    $0x7f,%eax
je     11d6 <gets+0xa6>
movzbl -0x9(%rbp),%eax
cmp    $0x8,%eax
jne    120c <gets+0xdc>
cmpl   $0x0,-0x10(%rbp)
je     1207 <gets+0xd7>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    $0x8,%edi
call   1040 <putchar@plt>
mov    $0x20,%edi
call   1040 <putchar@plt>
mov    $0x8,%edi
call   1040 <putchar@plt>
jmp    120c <gets+0xdc>
jmp    1211 <gets+0xe1>
jmp    1216 <gets+0xe6>
jmp    1143 <gets+0x13>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
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    %ebx,%ebx
jmp    1158 <gets+0x28>
nopl   0x0(%rax,%rax,1)
movzbl %al,%ebp
mov    %ebx,%ecx
add    $0x1,%ebx
mov    %al,(%r14,%rcx,1)
mov    %ebp,%edi
call   1040 <putchar@plt>
cmp    $0x4f,%ebx
jae    11a6 <gets+0x76>
xor    %eax,%eax
call   1050 <getchar@plt>
lea    -0x20(%rax),%ecx
cmp    $0x5e,%cl
jbe    1140 <gets+0x10>
cmp    $0xd,%al
je     119f <gets+0x6f>
cmp    $0xa,%al
je     119f <gets+0x6f>
cmp    $0x7f,%al
setne  %cl
cmp    $0x8,%al
setne  %al
test   %al,%cl
jne    1153 <gets+0x23>
test   %ebx,%ebx
je     1153 <gets+0x23>
add    $0xffffffff,%ebx
mov    $0x8,%ebp
mov    $0x8,%edi
call   1040 <putchar@plt>
mov    $0x20,%edi
call   1040 <putchar@plt>
jmp    114c <gets+0x1c>
xor    %eax,%eax
call   1030 <newline@plt>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
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 = %57, %1
%6 = load i32, i32* %4
%7 = icmp ult i32 %6, 79
br i1 %7, label %8, label %58
8: ; preds = %5
%9 = call zeroext i8 (...) @getchar()
store i8 %9, i8* %3
%10 = load i8, i8* %3
%11 = zext i8 %10 to i32
%12 = icmp sge i32 %11, 32
br i1 %12, label %13, label %27
13: ; preds = %8
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = icmp slt i32 %15, 127
br i1 %16, label %17, label %27
17: ; preds = %13
%18 = load i8, i8* %3
%19 = load i8*, i8** %2
%20 = load i32, i32* %4
%21 = add i32 %20, 1
store i32 %21, i32* %4
%22 = zext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 %18, i8* %23
%24 = load i8, i8* %3
%25 = zext i8 %24 to i32
%26 = call i32 @putchar(i32 %25)
br label %57
27: ; preds = %13, %8
%28 = load i8, i8* %3
%29 = zext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i8, i8* %3
%33 = zext i8 %32 to i32
%34 = icmp eq i32 %33, 13
br i1 %34, label %35, label %37
35: ; preds = %31, %27
%36 = call i32 (...) @newline()
br label %58
37: ; preds = %31
%38 = load i8, i8* %3
%39 = zext i8 %38 to i32
%40 = icmp eq i32 %39, 127
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = load i8, i8* %3
%43 = zext i8 %42 to i32
%44 = icmp eq i32 %43, 8
br i1 %44, label %45, label %55
45: ; preds = %41, %37
%46 = load i32, i32* %4
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* %4
%50 = add i32 %49, -1
store i32 %50, i32* %4
%51 = call i32 @putchar(i32 8)
%52 = call i32 @putchar(i32 32)
%53 = call i32 @putchar(i32 8)
br label %54
54: ; preds = %48, %45
br label %55
55: ; preds = %54, %41
br label %56
56: ; preds = %55
br label %57
57: ; preds = %56, %17
br label %5
58: ; preds = %35, %5
%59 = load i8*, i8** %2
%60 = load i32, i32* %4
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
store i8 0, i8* %62
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
define i8* @gets(i8* returned writeonly %0) {
br label %2
2: ; preds = %1, %29
%3 = phi i32 [ 0, %1 ], [ %30, %29 ]
%4 = tail call zeroext i8 (...) @getchar()
%5 = add i8 %4, -32
%6 = icmp ult i8 %5, 95
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = zext i8 %4 to i32
%9 = add nuw nsw i32 %3, 1
%10 = zext i32 %3 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 %4, i8* %11
br label %25
12: ; preds = %2
switch i8 %4, label %15 [
i8 13, label %13
i8 10, label %13
]
13: ; preds = %12, %12
%14 = tail call i32 (...) @newline()
br label %32
15: ; preds = %12
%16 = icmp eq i8 %4, 127
%17 = icmp eq i8 %4, 8
%18 = or i1 %16, %17
%19 = icmp ne i32 %3, 0
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %21, label %29
21: ; preds = %15
%22 = add nsw i32 %3, -1
%23 = tail call i32 @putchar(i32 8)
%24 = tail call i32 @putchar(i32 32)
br label %25
25: ; preds = %7, %21
%26 = phi i32 [ 8, %21 ], [ %8, %7 ]
%27 = phi i32 [ %22, %21 ], [ %9, %7 ]
%28 = tail call i32 @putchar(i32 %26)
br label %29
29: ; preds = %25, %15
%30 = phi i32 [ %3, %15 ], [ %27, %25 ]
%31 = icmp ult i32 %30, 79
br i1 %31, label %2, label %32
32: ; preds = %29, %13
%33 = phi i32 [ %3, %13 ], [ %30, %29 ]
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %0, i64 %34
store i8 0, i8* %35
ret i8* %0
}