#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 /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
char *gets(char *s) {
  int c;
  char *buf;
  fflush(NULL);
  buf = s;
  while ((c = getc(stdin)) != EOF && c != '\n') {
    *buf++ = c;
  }
  *buf = '\0';
  return (c == EOF && buf == s) ? NULL : s;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1030 <fflush@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
call   1040 <getc@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
mov    0x2e87(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x19(%rbp)
je     116e <gets+0x4e>
cmpl   $0xa,-0xc(%rbp)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    117e <gets+0x5e>
jmp    1199 <gets+0x79>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    113d <gets+0x1d>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xc(%rbp),%eax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%eax
jne    11cb <gets+0xab>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11cb <gets+0xab>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11d3 <gets+0xb3>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
call   1030 <fflush@plt>
mov    0x2eaf(%rip),%r12
mov    (%r12),%edi
call   1040 <getc@plt>
mov    0x2e97(%rip),%r15
cmp    $0xa,%eax
je     117b <gets+0x5b>
cmp    (%r15),%eax
je     117b <gets+0x5b>
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %al,(%rbx)
add    $0x1,%rbx
mov    (%r12),%edi
call   1040 <getc@plt>
cmp    $0xa,%eax
je     117e <gets+0x5e>
cmp    (%r15),%eax
jne    1160 <gets+0x40>
jmp    117e <gets+0x5e>
mov    %r14,%rbx
movb   $0x0,(%rbx)
xor    %ecx,%ecx
cmp    %r14,%rbx
cmovne %r14,%rcx
cmp    (%r15),%eax
cmovne %r14,%rcx
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@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 @fflush(i32* null)
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %17, %1
%8 = load i32, i32* @stdin
%9 = call i32 @getc(i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* @EOF
%11 = icmp ne i32 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 10
br label %15
15: ; preds = %12, %7
%16 = phi i1 [ false, %7 ], [ %14, %12 ]
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = load i32, i32* %3
%19 = trunc i32 %18 to i8
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
store i8 %19, i8* %20
br label %7
22: ; preds = %15
%23 = load i8*, i8** %4
store i8 0, i8* %23
%24 = load i32, i32* %3
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = load i8*, i8** %2
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %34
32: ; preds = %27, %22
%33 = load i8*, i8** %2
br label %34
34: ; preds = %32, %31
%35 = phi i8* [ null, %31 ], [ %33, %32 ]
ret i8* %35
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
define i8* @gets(i8* writeonly %0) {
%2 = tail call i32 @fflush(i32* null)
%3 = load i32, i32* @stdin
%4 = tail call i32 @getc(i32 %3)
%5 = load i32, i32* @EOF
%6 = icmp ne i32 %4, %5
%7 = icmp ne i32 %4, 10
%8 = and i1 %7, %6
br i1 %8, label %9, label %20
9: ; preds = %1, %9
%10 = phi i32 [ %15, %9 ], [ %4, %1 ]
%11 = phi i8* [ %13, %9 ], [ %0, %1 ]
%12 = trunc i32 %10 to i8
%13 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %12, i8* %11
%14 = load i32, i32* @stdin
%15 = tail call i32 @getc(i32 %14)
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
%18 = icmp ne i32 %15, 10
%19 = and i1 %18, %17
br i1 %19, label %9, label %20
20: ; preds = %9, %1
%21 = phi i8* [ %0, %1 ], [ %13, %9 ]
%22 = phi i32 [ %4, %1 ], [ %15, %9 ]
store i8 0, i8* %21
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
%25 = icmp eq i8* %21, %0
%26 = and i1 %24, %25
%27 = select i1 %26, i8* null, i8* %0
ret i8* %27
}