#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 EOF;
int getchar();
char *gets(char *dest) {
  int c = getchar();
  char *p = dest;
  while (c != EOF && c != '\n') {
    *p++ = c;
    c = getchar();
  }
  *p = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
xor    %eax,%eax
mov    0x2eae(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x19(%rbp)
je     114f <gets+0x3f>
cmpl   $0xa,-0xc(%rbp)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    115f <gets+0x4f>
jmp    1184 <gets+0x74>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0xc(%rbp)
jmp    112e <gets+0x1e>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %r14,%rbx
cmp    $0xa,%eax
je     1157 <gets+0x47>
mov    0x2eba(%rip),%r15
mov    %r14,%rbx
cmp    (%r15),%eax
je     1157 <gets+0x47>
mov    %r14,%rbx
nopl   0x0(%rax)
mov    %al,(%rbx)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     1157 <gets+0x47>
cmp    (%r15),%eax
jne    1140 <gets+0x30>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i32 0
define i8* @gets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call i32 (...) @getchar()
store i32 %5, i32* %3
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %16, %1
%8 = load i32, i32* %3
%9 = load i32, i32* @EOF
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 10
br label %14
14: ; preds = %11, %7
%15 = phi i1 [ false, %7 ], [ %13, %11 ]
br i1 %15, label %16, label %22
16: ; preds = %14
%17 = load i32, i32* %3
%18 = trunc i32 %17 to i8
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %4
store i8 %18, i8* %19
%21 = call i32 (...) @getchar()
store i32 %21, i32* %3
br label %7
22: ; preds = %14
%23 = load i8*, i8** %4
store i8 0, i8* %23
%24 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
@EOF = global i32 0
define i8* @gets(i8* returned writeonly %0) {
%2 = tail call i32 (...) @getchar()
%3 = load i32, i32* @EOF
%4 = icmp ne i32 %2, %3
%5 = icmp ne i32 %2, 10
%6 = and i1 %5, %4
br i1 %6, label %7, label %17
7: ; preds = %1, %7
%8 = phi i8* [ %11, %7 ], [ %0, %1 ]
%9 = phi i32 [ %12, %7 ], [ %2, %1 ]
%10 = trunc i32 %9 to i8
%11 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %10, i8* %8
%12 = tail call i32 (...) @getchar()
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
%15 = icmp ne i32 %12, 10
%16 = and i1 %15, %14
br i1 %16, label %7, label %17
17: ; preds = %7, %1
%18 = phi i8* [ %0, %1 ], [ %11, %7 ]
store i8 0, i8* %18
ret i8* %0
}