#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 */
typedef size_t uint32_t;

/* Variables and functions */
char getchar();
char *gets(char *str) {
  uint32_t i = 0;
  char c;
  while (1) {
    c = getchar();
    if (c == '\0') {
      break;
    } else if (c == '\n' || c == '\r') {
      break;
    } else {
      str[i++] = c;
    }
  }
  str[i] = '\0';
  return str;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    1140 <gets+0x30>
jmp    1182 <gets+0x72>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
je     115a <gets+0x4a>
movsbl -0x11(%rbp),%eax
cmp    $0xd,%eax
jne    115f <gets+0x4f>
jmp    1182 <gets+0x72>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x10(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    117d <gets+0x6d>
jmp    1124 <gets+0x14>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2401,%r15d
mov    %rdi,%rbx
jmp    1136 <gets+0x26>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %al,(%rbx)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xd,%al
ja     1130 <gets+0x20>
movzbl %al,%ecx
bt     %rcx,%r15
jae    1130 <gets+0x20>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @gets(i8* returned writeonly %0) {
br label %2
2: ; preds = %5, %1
%3 = phi i64 [ 0, %1 ], [ %6, %5 ]
%4 = tail call signext i8 (...) @getchar()
switch i8 %4, label %5 [
i8 0, label %8
i8 13, label %8
i8 10, label %8
]
5: ; preds = %2
%6 = add i64 %3, 1
%7 = getelementptr inbounds i8, i8* %0, i64 %3
store i8 %4, i8* %7
br label %2
8: ; preds = %2, %2, %2
%9 = getelementptr inbounds i8, i8* %0, i64 %3
store i8 0, i8* %9
ret i8* %0
}