#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;
scalar_t__ feof(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
char *gets(char *s) {
  char *s0 = s;
  int c;
  if (feof(stdin)) {
    *s = '\0';
    return NULL;
  }
  for (;;) {
    c = fgetc(stdin);
    if (c == EOF)
      break;
    if (c == '\n')
      break;
    *s++ = c;
  }
  *s = '\0';
  return s0;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
call   1040 <feof@plt>
cmp    $0x0,%rax
je     1160 <gets+0x40>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11c6 <gets+0xa6>
jmp    1165 <gets+0x45>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%eax
jne    118d <gets+0x6d>
jmp    11b7 <gets+0x97>
cmpl   $0xa,-0x1c(%rbp)
jne    119c <gets+0x7c>
jmp    11b7 <gets+0x97>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    1165 <gets+0x45>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2eb6(%rip),%r15
mov    (%r15),%edi
call   1040 <feof@plt>
test   %rax,%rax
je     1147 <gets+0x27>
mov    %r14,%rbx
xor    %r14d,%r14d
jmp    118e <gets+0x6e>
mov    (%r15),%edi
call   1030 <fgetc@plt>
cmp    $0xa,%eax
je     118b <gets+0x6b>
mov    0x2e85(%rip),%r12
cmp    (%r12),%eax
je     118b <gets+0x6b>
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %al,(%rbx)
add    $0x1,%rbx
mov    (%r15),%edi
call   1030 <fgetc@plt>
cmp    $0xa,%eax
je     118e <gets+0x6e>
cmp    (%r12),%eax
jne    1170 <gets+0x50>
jmp    118e <gets+0x6e>
mov    %r14,%rbx
movb   $0x0,(%rbx)
mov    %r14,%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 i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
store i8* %6, i8** %4
%7 = load i32, i32* @stdin
%8 = call i64 @feof(i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** %3
store i8 0, i8* %11
store i8* null, i8** %2
br label %32
12: ; preds = %1
br label %13
13: ; preds = %24, %12
%14 = load i32, i32* @stdin
%15 = call i32 @fgetc(i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %13
br label %29
20: ; preds = %13
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %24
23: ; preds = %20
br label %29
24: ; preds = %20
%25 = load i32, i32* %5
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %3
store i8 %26, i8* %27
br label %13
29: ; preds = %23, %19
%30 = load i8*, i8** %3
store i8 0, i8* %30
%31 = load i8*, i8** %4
store i8* %31, i8** %2
br label %32
32: ; preds = %29, %10
%33 = load i8*, i8** %2
ret i8* %33
}

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