#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 fgetc_cons();
int /*<<< orphan*/ fputc_cons(unsigned char);
char *gets(unsigned char *str) {
  unsigned char c;
  int ptr;
  ptr = 0;
  while (1) {
    c = fgetc_cons();
    if (c == '\n' || c == '\r') {
      fputc_cons(13);
      return str;
    }
    if (c == 12 || c == 8) {
      if (ptr > 0) {
        str[--ptr] = 0;
        fputc_cons(8);
        fputc_cons(32);
        fputc_cons(8);
      }
    } else {
      str[ptr++] = c;
      str[ptr] = 0;
      fputc_cons(c);
    }
  }
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <fgetc_cons@plt>
mov    %al,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0xa,%eax
je     1157 <gets+0x37>
movzbl -0x9(%rbp),%eax
cmp    $0xd,%eax
jne    116b <gets+0x4b>
mov    $0xd,%edi
call   1040 <fputc_cons@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret
movzbl -0x9(%rbp),%eax
cmp    $0xc,%eax
je     1185 <gets+0x65>
movzbl -0x9(%rbp),%eax
cmp    $0x8,%eax
jne    11c6 <gets+0xa6>
cmpl   $0x0,-0x10(%rbp)
jle    11c1 <gets+0xa1>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x8,%edi
call   1040 <fputc_cons@plt>
mov    $0x20,%edi
call   1040 <fputc_cons@plt>
mov    $0x8,%edi
call   1040 <fputc_cons@plt>
jmp    11f3 <gets+0xd3>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movzbl -0x9(%rbp),%edi
call   1040 <fputc_cons@plt>
jmp    1133 <gets+0x13>

x86 assembly (clang, -O2):
<gets>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <fgetc_cons@plt>
cmp    $0xd,%al
je     1188 <gets+0x68>
cmp    $0xa,%al
je     1188 <gets+0x68>
mov    %eax,%ecx
and    $0xfb,%cl
cmp    $0x8,%cl
jne    116d <gets+0x4d>
test   %ebx,%ebx
jle    1130 <gets+0x10>
add    $0xffffffff,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    $0x8,%edi
call   1040 <fputc_cons@plt>
mov    $0x20,%edi
call   1040 <fputc_cons@plt>
mov    $0x8,%al
jmp    117e <gets+0x5e>
movslq %ebx,%rcx
lea    0x1(%rcx),%rbx
mov    %al,(%r14,%rcx,1)
movb   $0x0,0x1(%r14,%rcx,1)
movzbl %al,%edi
call   1040 <fputc_cons@plt>
jmp    1130 <gets+0x10>
mov    $0xd,%edi
call   1040 <fputc_cons@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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, %51
%6 = call zeroext i8 (...) @fgetc_cons()
store i8 %6, i8* %3
%7 = load i8, i8* %3
%8 = zext i8 %7 to i32
%9 = icmp eq i32 %8, 10
br i1 %9, label %14, label %10
10: ; preds = %5
%11 = load i8, i8* %3
%12 = zext i8 %11 to i32
%13 = icmp eq i32 %12, 13
br i1 %13, label %14, label %17
14: ; preds = %10, %5
%15 = call i32 @fputc_cons(i8 zeroext 13)
%16 = load i8*, i8** %2
ret i8* %16
17: ; preds = %10
%18 = load i8, i8* %3
%19 = zext i8 %18 to i32
%20 = icmp eq i32 %19, 12
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i8, i8* %3
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 8
br i1 %24, label %25, label %38
25: ; preds = %21, %17
%26 = load i32, i32* %4
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i8*, i8** %2
%30 = load i32, i32* %4
%31 = add nsw i32 %30, -1
store i32 %31, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 0, i8* %33
%34 = call i32 @fputc_cons(i8 zeroext 8)
%35 = call i32 @fputc_cons(i8 zeroext 32)
%36 = call i32 @fputc_cons(i8 zeroext 8)
br label %37
37: ; preds = %28, %25
br label %51
38: ; preds = %21
%39 = load i8, i8* %3
%40 = load i8*, i8** %2
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 %39, i8* %44
%45 = load i8*, i8** %2
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i8, i8* %3
%50 = call i32 @fputc_cons(i8 zeroext %49)
br label %51
51: ; preds = %38, %37
br label %5
}

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