#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *freadline(int ifd, char *line) {
  static char buffer[513];
  static char *bptr;
  static int nbytes;
  static int done;
  register char *p = line;
  while (!done) {
    if (nbytes == 0) {
      nbytes = read(ifd, buffer, sizeof(buffer) - 1);
      if (nbytes < 0) {
        fprintf(stderr, "Error reading input fd.\n");
        exit(-1);
      }
      if (nbytes < sizeof(buffer) - 1) {
        done = 1;
      }
      buffer[nbytes] = 0;
      bptr = buffer;
    }
    while (*bptr) {
      nbytes--;
      if (*bptr == '\n') {
        bptr++;
        *p = '\0';
        return line;
      }
      *p++ = *bptr++;
    }
  }
  *buffer = '\0';
  bptr = NULL;
  nbytes = 0;
  done = 0;
  return NULL;
}


x86 assembly (clang, -O0):
<freadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,0x3116(%rip)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1160 <freadline+0x30>
jmp    127f <freadline+0x14f>
cmpl   $0x0,0x30f9(%rip)
jne    11f0 <freadline+0xc0>
mov    -0xc(%rbp),%edi
lea    0x2ed9(%rip),%rsi
mov    $0x200,%edx
call   1030 <read@plt>
mov    %eax,0x30d9(%rip)
cmpl   $0x0,0x30d2(%rip)
jge    11b3 <freadline+0x83>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
movslq 0x30a6(%rip),%rax
cmp    $0x200,%rax
jae    11d0 <freadline+0xa0>
movl   $0x1,0x3094(%rip)
movslq 0x3089(%rip),%rcx
lea    0x2e72(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e67(%rip),%rax
mov    %rax,0x3068(%rip)
jmp    11f5 <freadline+0xc5>
mov    0x305c(%rip),%rax
cmpb   $0x0,(%rax)
je     127a <freadline+0x14a>
mov    0x3055(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x304c(%rip)
mov    0x303d(%rip),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    124d <freadline+0x11d>
mov    0x302a(%rip),%rax
add    $0x1,%rax
mov    %rax,0x301f(%rip)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <freadline+0x17d>
mov    0x3004(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x2ff6(%rip)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    11f5 <freadline+0xc5>
jmp    1147 <freadline+0x17>
movb   $0x0,0x2dca(%rip)
movq   $0x0,0x2fc7(%rip)
movl   $0x0,0x2fc5(%rip)
movl   $0x0,0x2fbf(%rip)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freadline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,0x3126(%rip)
je     116a <freadline+0x3a>
movb   $0x0,0x2f09(%rip)
movq   $0x0,0x3106(%rip)
movl   $0x0,0x3104(%rip)
movb   $0x0,0x3101(%rip)
xor    %ebp,%ebp
jmp    123f <freadline+0x10f>
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x30ea(%rip),%eax
lea    0x2ed3(%rip),%r14
mov    %rsi,%rbx
jmp    1199 <freadline+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpb   $0x0,0x30cd(%rip)
jne    1140 <freadline+0x10>
test   %eax,%eax
je     11b0 <freadline+0x80>
mov    0x30b4(%rip),%rcx
jmp    11ed <freadline+0xbd>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1030 <read@plt>
mov    %eax,0x309a(%rip)
test   %eax,%eax
js     124d <freadline+0x11d>
cmp    $0x1ff,%eax
ja     11dc <freadline+0xac>
movb   $0x1,0x3088(%rip)
mov    %eax,%ecx
movb   $0x0,(%rcx,%r14,1)
mov    %r14,0x306e(%rip)
mov    %r14,%rcx
mov    (%rcx),%dl
test   %dl,%dl
je     1190 <freadline+0x60>
add    $0x1,%rcx
neg    %eax
nopl   0x0(%rax)
cmp    $0xa,%dl
je     122d <freadline+0xfd>
mov    %rcx,0x304c(%rip)
mov    %dl,(%rbx)
add    $0x1,%rbx
movzbl (%rcx),%edx
add    $0x1,%rcx
add    $0x1,%eax
test   %dl,%dl
jne    1200 <freadline+0xd0>
neg    %eax
mov    %eax,0x3038(%rip)
jmp    1190 <freadline+0x60>
not    %eax
mov    %eax,0x302b(%rip)
mov    %rcx,0x301c(%rip)
movb   $0x0,(%rbx)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@freadline.buffer = internal global [513 x i8] zeroinitializer
@freadline.bptr = internal global i8* null
@freadline.nbytes = internal global i32 0
@freadline.done = internal global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error reading input fd.\0A\00"
define i8* @freadline(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
store i8* %7, i8** %6
br label %8
8: ; preds = %56, %2
%9 = load i32, i32* @freadline.done
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %57
12: ; preds = %8
%13 = load i32, i32* @freadline.nbytes
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %12
%16 = load i32, i32* %4
%17 = call i32 @read(i32 %16, i8* getelementptr inbounds ([513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 0), i32 512)
store i32 %17, i32* @freadline.nbytes
%18 = load i32, i32* @freadline.nbytes
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %15
%25 = load i32, i32* @freadline.nbytes
%26 = sext i32 %25 to i64
%27 = icmp ult i64 %26, 512
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 1, i32* @freadline.done
br label %29
29: ; preds = %28, %24
%30 = load i32, i32* @freadline.nbytes
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 %31
store i8 0, i8* %32
store i8* getelementptr inbounds ([513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 0), i8** @freadline.bptr
br label %33
33: ; preds = %29, %12
br label %34
34: ; preds = %50, %33
%35 = load i8*, i8** @freadline.bptr
%36 = load i8, i8* %35
%37 = icmp ne i8 %36, 0
br i1 %37, label %38, label %56
38: ; preds = %34
%39 = load i32, i32* @freadline.nbytes
%40 = add nsw i32 %39, -1
store i32 %40, i32* @freadline.nbytes
%41 = load i8*, i8** @freadline.bptr
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 10
br i1 %44, label %45, label %50
45: ; preds = %38
%46 = load i8*, i8** @freadline.bptr
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** @freadline.bptr
%48 = load i8*, i8** %6
store i8 0, i8* %48
%49 = load i8*, i8** %5
store i8* %49, i8** %3
br label %58
50: ; preds = %38
%51 = load i8*, i8** @freadline.bptr
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** @freadline.bptr
%53 = load i8, i8* %51
%54 = load i8*, i8** %6
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %6
store i8 %53, i8* %54
br label %34
56: ; preds = %34
br label %8
57: ; preds = %8
store i8 0, i8* getelementptr inbounds ([513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 0)
store i8* null, i8** @freadline.bptr
store i32 0, i32* @freadline.nbytes
store i32 0, i32* @freadline.done
store i8* null, i8** %3
br label %58
58: ; preds = %57, %45
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
@freadline.buffer = internal global [513 x i8] zeroinitializer
@freadline.bptr = internal unnamed_addr global i8* null
@freadline.nbytes = internal unnamed_addr global i32 0
@freadline.done = internal unnamed_addr global i1 false
@stderr = global i32 0
@.str = [25 x i8] c"Error reading input fd.\0A\00"
define i8* @freadline(i32 %0, i8* writeonly %1) {
%3 = load i1, i1* @freadline.done
br i1 %3, label %49, label %4
4: ; preds = %2
%5 = load i32, i32* @freadline.nbytes
br label %11
6: ; preds = %44
store i32 %40, i32* @freadline.nbytes
br label %7
7: ; preds = %6, %30
%8 = phi i32 [ %40, %6 ], [ %31, %30 ]
%9 = phi i8* [ %46, %6 ], [ %13, %30 ]
%10 = load i1, i1* @freadline.done
br i1 %10, label %49, label %11
11: ; preds = %4, %7
%12 = phi i32 [ %8, %7 ], [ %5, %4 ]
%13 = phi i8* [ %9, %7 ], [ %1, %4 ]
%14 = icmp eq i32 %12, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = load i8*, i8** @freadline.bptr
br label %30
17: ; preds = %11
%18 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 0), i32 512)
store i32 %18, i32* @freadline.nbytes
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %17
%25 = icmp ult i32 %18, 512
br i1 %25, label %26, label %27
26: ; preds = %24
store i1 true, i1* @freadline.done
br label %27
27: ; preds = %26, %24
%28 = zext i32 %18 to i64
%29 = getelementptr inbounds [513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 %28
store i8 0, i8* %29
store i8* getelementptr inbounds ([513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 0), i8** @freadline.bptr
br label %30
30: ; preds = %15, %27
%31 = phi i32 [ %12, %15 ], [ %18, %27 ]
%32 = phi i8* [ %16, %15 ], [ getelementptr inbounds ([513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 0), %27 ]
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %7, label %35
35: ; preds = %30, %44
%36 = phi i8 [ %47, %44 ], [ %33, %30 ]
%37 = phi i8* [ %46, %44 ], [ %13, %30 ]
%38 = phi i8* [ %45, %44 ], [ %32, %30 ]
%39 = phi i32 [ %40, %44 ], [ %31, %30 ]
%40 = add nsw i32 %39, -1
%41 = icmp eq i8 %36, 10
br i1 %41, label %42, label %44
42: ; preds = %35
store i32 %40, i32* @freadline.nbytes
%43 = getelementptr inbounds i8, i8* %38, i64 1
store i8* %43, i8** @freadline.bptr
store i8 0, i8* %37
br label %50
44: ; preds = %35
%45 = getelementptr inbounds i8, i8* %38, i64 1
store i8* %45, i8** @freadline.bptr
%46 = getelementptr inbounds i8, i8* %37, i64 1
store i8 %36, i8* %37
%47 = load i8, i8* %45
%48 = icmp eq i8 %47, 0
br i1 %48, label %6, label %35
49: ; preds = %7, %2
store i8 0, i8* getelementptr inbounds ([513 x i8], [513 x i8]* @freadline.buffer, i64 0, i64 0)
store i8* null, i8** @freadline.bptr
store i32 0, i32* @freadline.nbytes
store i1 false, i1* @freadline.done
br label %50
50: ; preds = %49, %42
%51 = phi i8* [ %1, %42 ], [ null, %49 ]
ret i8* %51
}