#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 getchar();
int /*<<< orphan*/ stdin;
char *gets1(char *s) {
  int c, i = 0;
  while (((c = getchar()) != EOF) && (c != '\n'))
    s[i++] = c;
  s[i] = '\0';
  fflush(stdin);
  return s;
}


x86 assembly (clang, -O0):
<gets1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
mov    0x2e98(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x11(%rbp)
je     115d <gets1+0x3d>
cmpl   $0xa,-0xc(%rbp)
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    116d <gets1+0x4d>
jmp    118c <gets1+0x6c>
mov    -0xc(%rbp),%eax
mov    %al,%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)
jmp    1133 <gets1+0x13>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets1>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     116b <gets1+0x4b>
mov    0x2ea3(%rip),%r14
cmp    (%r14),%eax
je     116b <gets1+0x4b>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     1169 <gets1+0x49>
cmp    (%r14),%eax
jne    1150 <gets1+0x30>
mov    %ebx,%ebx
movb   $0x0,(%r15,%rbx,1)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@EOF = global i32 0
@stdin = global i32 0
define i8* @gets1(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 %20
7: ; preds = %1, %7
%8 = phi i64 [ %11, %7 ], [ 0, %1 ]
%9 = phi i32 [ %13, %7 ], [ %2, %1 ]
%10 = trunc i32 %9 to i8
%11 = add nuw i64 %8, 1
%12 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %10, i8* %12
%13 = tail call i32 (...) @getchar()
%14 = load i32, i32* @EOF
%15 = icmp ne i32 %13, %14
%16 = icmp ne i32 %13, 10
%17 = and i1 %16, %15
br i1 %17, label %7, label %18
18: ; preds = %7
%19 = and i64 %11, 4294967295
br label %20
20: ; preds = %18, %1
%21 = phi i64 [ 0, %1 ], [ %19, %18 ]
%22 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 0, i8* %22
%23 = load i32, i32* @stdin
%24 = tail call i32 @fflush(i32 %23)
ret i8* %0
}