#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 dgetc();
char *dgets(char *as) {
  register unsigned n = 500;
  register char *s;
  register int c;
  s = as;
  while (--n > 0 && (c = dgetc()) != EOF)
    if ((*s++ = c) == '\n')
      break;
  *s = '\0';
  return (c == EOF && s == as ? NULL : as);
}


x86 assembly (clang, -O0):
<dgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1f4,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
jbe    115b <dgets+0x4b>
mov    $0x0,%al
call   1030 <dgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e95(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    116b <dgets+0x5b>
jmp    1195 <dgets+0x85>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0xa,%eax
jne    1190 <dgets+0x80>
jmp    1195 <dgets+0x85>
jmp    112b <dgets+0x1b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%eax
jne    11c7 <dgets+0xb7>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11c7 <dgets+0xb7>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11cf <dgets+0xbf>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dgets>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
mov    0x2ec9(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <dgetc@plt>
cmp    (%r14),%eax
je     1158 <dgets+0x48>
mov    %al,(%r15,%rbx,1)
lea    0x1(%rbx),%rdx
cmp    $0xa,%al
je     1153 <dgets+0x43>
cmp    $0x1f2,%ebx
mov    %rdx,%rbx
jne    1130 <dgets+0x20>
add    %r15,%rdx
jmp    115e <dgets+0x4e>
add    %r15,%rbx
mov    %rbx,%rdx
movb   $0x0,(%rdx)
xor    %ecx,%ecx
cmp    (%r14),%eax
cmovne %r15,%rcx
cmp    %r15,%rdx
cmovne %r15,%rcx
mov    %rcx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i32 0
define i8* @dgets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
store i32 500, i32* %3
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %25, %1
%8 = load i32, i32* %3
%9 = add i32 %8, -1
store i32 %9, i32* %3
%10 = icmp ugt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = call i32 (...) @dgetc()
store i32 %12, i32* %5
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br label %15
15: ; preds = %11, %7
%16 = phi i1 [ false, %7 ], [ %14, %11 ]
br i1 %16, label %17, label %26
17: ; preds = %15
%18 = load i32, i32* %5
%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
%22 = sext i8 %19 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %17
br label %26
25: ; preds = %17
br label %7
26: ; preds = %24, %15
%27 = load i8*, i8** %4
store i8 0, i8* %27
%28 = load i32, i32* %5
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i8*, i8** %4
%33 = load i8*, i8** %2
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
br label %38
36: ; preds = %31, %26
%37 = load i8*, i8** %2
br label %38
38: ; preds = %36, %35
%39 = phi i8* [ null, %35 ], [ %37, %36 ]
ret i8* %39
}

LLVM IR (-O2):
@EOF = global i32 0
define i8* @dgets(i8* writeonly %0) {
br label %2
2: ; preds = %8, %1
%3 = phi i32 [ 499, %1 ], [ %13, %8 ]
%4 = phi i8* [ %0, %1 ], [ %10, %8 ]
%5 = tail call i32 (...) @dgetc()
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %16, label %8
8: ; preds = %2
%9 = trunc i32 %5 to i8
%10 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %9, i8* %4
%11 = and i32 %5, 255
%12 = icmp eq i32 %11, 10
%13 = add nsw i32 %3, -1
%14 = icmp eq i32 %13, 0
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %2
16: ; preds = %8, %2
%17 = phi i8* [ %10, %8 ], [ %4, %2 ]
store i8 0, i8* %17
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %5, %18
%20 = icmp eq i8* %17, %0
%21 = and i1 %20, %19
%22 = select i1 %21, i8* null, i8* %0
ret i8* %22
}