#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 */
char *xmalloc(int);
int xread(int const, char *, int);
char *get_next_line(const int fd) {
  static char buffer_read[500];
  static int i;
  static int cursor;
  int j;
  char *buffer_return;
  j = 0;
  buffer_return = xmalloc(sizeof(*buffer_return) * (500 + 1));
  cursor = (i == 0 ? xread(fd, buffer_read, 500) : cursor);
  while (i <= cursor && j <= 500) {
    if (j == 500)
      return (buffer_return);
    if (buffer_read[i] == '\n') {
      i++;
      buffer_return[j] = '\0';
      return (buffer_return);
    }
    cursor = (i >= cursor ? xread(fd, buffer_read, 500) : cursor);
    i = (i >= cursor ? 0 : i);
    buffer_return[j++] = buffer_read[i++];
  }
  return (0);
}


x86 assembly (clang, -O0):
<get_next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x1f5,%edi
call   1030 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,0x30ed(%rip)
jne    1169 <get_next_line+0x49>
mov    -0xc(%rbp),%edi
lea    0x2ee9(%rip),%rsi
mov    $0x1f4,%edx
call   1040 <xread@plt>
mov    %eax,-0x1c(%rbp)
jmp    1172 <get_next_line+0x52>
mov    0x30c9(%rip),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,0x30bd(%rip)
mov    0x30b3(%rip),%ecx
xor    %eax,%eax
cmp    0x30af(%rip),%ecx
mov    %al,-0x1d(%rbp)
jg     119f <get_next_line+0x7f>
cmpl   $0x1f4,-0x10(%rbp)
setle  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11af <get_next_line+0x8f>
jmp    12b2 <get_next_line+0x192>
cmpl   $0x1f4,-0x10(%rbp)
jne    11c9 <get_next_line+0xa9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ba <get_next_line+0x19a>
movslq 0x3064(%rip),%rcx
lea    0x2e69(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    120c <get_next_line+0xec>
mov    0x304a(%rip),%eax
add    $0x1,%eax
mov    %eax,0x3041(%rip)
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ba <get_next_line+0x19a>
mov    0x3022(%rip),%eax
cmp    0x3020(%rip),%eax
jl     123a <get_next_line+0x11a>
mov    -0xc(%rbp),%edi
lea    0x2e18(%rip),%rsi
mov    $0x1f4,%edx
call   1040 <xread@plt>
mov    %eax,-0x24(%rbp)
jmp    1243 <get_next_line+0x123>
mov    0x2ff8(%rip),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,0x2fec(%rip)
mov    0x2fe2(%rip),%eax
cmp    0x2fe0(%rip),%eax
jl     1268 <get_next_line+0x148>
xor    %eax,%eax
mov    %eax,-0x28(%rbp)
jmp    1271 <get_next_line+0x151>
mov    0x2fc6(%rip),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,0x2fba(%rip)
mov    0x2fb4(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x2fa9(%rip)
movslq %eax,%rcx
lea    0x2dab(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x18(%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    117b <get_next_line+0x5b>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_next_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    $0x1f5,%edi
call   1030 <xmalloc@plt>
mov    %rax,%r12
mov    0x30f6(%rip),%ecx
test   %ecx,%ecx
je     114a <get_next_line+0x2a>
mov    0x30f0(%rip),%eax
jmp    1164 <get_next_line+0x44>
lea    0x2eef(%rip),%rsi
mov    %r14d,%edi
mov    $0x1f4,%edx
call   1040 <xread@plt>
mov    0x30d0(%rip),%ecx
mov    %eax,0x30ce(%rip)
xor    %r15d,%r15d
cmp    %eax,%ecx
jle    1176 <get_next_line+0x56>
xor    %r12d,%r12d
jmp    11f4 <get_next_line+0xd4>
lea    0x2ec3(%rip),%rbp
xor    %ebx,%ebx
jmp    11b8 <get_next_line+0x98>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %eax,%ecx
cmovge %r15d,%ecx
mov    %eax,0x309c(%rip)
movslq %ecx,%rsi
add    $0x1,%ecx
mov    %ecx,0x308c(%rip)
movzbl (%rsi,%rbp,1),%edx
mov    %dl,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %eax,%esi
jge    1171 <get_next_line+0x51>
cmp    $0x1f4,%rbx
je     11f4 <get_next_line+0xd4>
movslq %ecx,%rdx
cmpb   $0xa,(%rdx,%rbp,1)
je     11e6 <get_next_line+0xc6>
cmp    %eax,%ecx
jl     1190 <get_next_line+0x70>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x1f4,%edx
call   1040 <xread@plt>
mov    0x3050(%rip),%ecx
jmp    1190 <get_next_line+0x70>
add    $0x1,%ecx
mov    %ecx,0x3045(%rip)
movb   $0x0,(%r12,%rbx,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@get_next_line.buffer_read = internal global [500 x i8] zeroinitializer
@get_next_line.i = internal global i32 0
@get_next_line.cursor = internal global i32 0
define i8* @get_next_line(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %4
%6 = call i8* @xmalloc(i32 501)
store i8* %6, i8** %5
%7 = load i32, i32* @get_next_line.i
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 @xread(i32 %10, i8* getelementptr inbounds ([500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 0), i32 500)
br label %14
12: ; preds = %1
%13 = load i32, i32* @get_next_line.cursor
br label %14
14: ; preds = %12, %9
%15 = phi i32 [ %11, %9 ], [ %13, %12 ]
store i32 %15, i32* @get_next_line.cursor
br label %16
16: ; preds = %62, %14
%17 = load i32, i32* @get_next_line.i
%18 = load i32, i32* @get_next_line.cursor
%19 = icmp sle i32 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %4
%22 = icmp sle i32 %21, 500
br label %23
23: ; preds = %20, %16
%24 = phi i1 [ false, %16 ], [ %22, %20 ]
br i1 %24, label %25, label %74
25: ; preds = %23
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 500
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i8*, i8** %5
store i8* %29, i8** %2
br label %75
30: ; preds = %25
%31 = load i32, i32* @get_next_line.i
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %37, label %45
37: ; preds = %30
%38 = load i32, i32* @get_next_line.i
%39 = add nsw i32 %38, 1
store i32 %39, i32* @get_next_line.i
%40 = load i8*, i8** %5
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %5
store i8* %44, i8** %2
br label %75
45: ; preds = %30
%46 = load i32, i32* @get_next_line.i
%47 = load i32, i32* @get_next_line.cursor
%48 = icmp sge i32 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* %3
%51 = call i32 @xread(i32 %50, i8* getelementptr inbounds ([500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 0), i32 500)
br label %54
52: ; preds = %45
%53 = load i32, i32* @get_next_line.cursor
br label %54
54: ; preds = %52, %49
%55 = phi i32 [ %51, %49 ], [ %53, %52 ]
store i32 %55, i32* @get_next_line.cursor
%56 = load i32, i32* @get_next_line.i
%57 = load i32, i32* @get_next_line.cursor
%58 = icmp sge i32 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %54
br label %62
60: ; preds = %54
%61 = load i32, i32* @get_next_line.i
br label %62
62: ; preds = %60, %59
%63 = phi i32 [ 0, %59 ], [ %61, %60 ]
store i32 %63, i32* @get_next_line.i
%64 = load i32, i32* @get_next_line.i
%65 = add nsw i32 %64, 1
store i32 %65, i32* @get_next_line.i
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds [500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 %66
%68 = load i8, i8* %67
%69 = load i8*, i8** %5
%70 = load i32, i32* %4
%71 = add nsw i32 %70, 1
store i32 %71, i32* %4
%72 = sext i32 %70 to i64
%73 = getelementptr inbounds i8, i8* %69, i64 %72
store i8 %68, i8* %73
br label %16
74: ; preds = %23
store i8* null, i8** %2
br label %75
75: ; preds = %74, %37, %28
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@get_next_line.buffer_read = internal global [500 x i8] zeroinitializer
@get_next_line.i = internal unnamed_addr global i32 0
@get_next_line.cursor = internal unnamed_addr global i32 0
define i8* @get_next_line(i32 %0) {
%2 = tail call i8* @xmalloc(i32 501)
%3 = load i32, i32* @get_next_line.i
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @xread(i32 %0, i8* getelementptr inbounds ([500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 0), i32 500)
%7 = load i32, i32* @get_next_line.i
br label %10
8: ; preds = %1
%9 = load i32, i32* @get_next_line.cursor
br label %10
10: ; preds = %8, %5
%11 = phi i32 [ %7, %5 ], [ %3, %8 ]
%12 = phi i32 [ %6, %5 ], [ %9, %8 ]
store i32 %12, i32* @get_next_line.cursor
%13 = icmp sgt i32 %11, %12
br i1 %13, label %45, label %14
14: ; preds = %10, %33
%15 = phi i64 [ %42, %33 ], [ 0, %10 ]
%16 = phi i32 [ %35, %33 ], [ %12, %10 ]
%17 = phi i32 [ %38, %33 ], [ %11, %10 ]
%18 = icmp eq i64 %15, 500
br i1 %18, label %45, label %19
19: ; preds = %14
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds [500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = add nsw i32 %17, 1
store i32 %25, i32* @get_next_line.i
%26 = and i64 %15, 4294967295
%27 = getelementptr inbounds i8, i8* %2, i64 %26
store i8 0, i8* %27
br label %45
28: ; preds = %19
%29 = icmp slt i32 %17, %16
br i1 %29, label %33, label %30
30: ; preds = %28
%31 = tail call i32 @xread(i32 %0, i8* getelementptr inbounds ([500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 0), i32 500)
%32 = load i32, i32* @get_next_line.i
br label %33
33: ; preds = %28, %30
%34 = phi i32 [ %32, %30 ], [ %17, %28 ]
%35 = phi i32 [ %31, %30 ], [ %16, %28 ]
store i32 %35, i32* @get_next_line.cursor
%36 = icmp slt i32 %34, %35
%37 = select i1 %36, i32 %34, i32 0
%38 = add nsw i32 %37, 1
store i32 %38, i32* @get_next_line.i
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds [500 x i8], [500 x i8]* @get_next_line.buffer_read, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = add nuw nsw i64 %15, 1
%43 = getelementptr inbounds i8, i8* %2, i64 %15
store i8 %41, i8* %43
%44 = icmp slt i32 %37, %35
br i1 %44, label %14, label %45
45: ; preds = %14, %33, %10, %24
%46 = phi i8* [ %2, %24 ], [ null, %10 ], [ %2, %14 ], [ null, %33 ]
ret i8* %46
}