#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 /*<<< orphan*/ free(char *);
char *malloc(int);
char *my_first_line(char *);
char *my_ralloc_cat(char *, char *);
int my_search(char *, char);
int read(int const, char *, int);
char *get_next_line(const int fd) {
  char *reading;
  static char *buffer = NULL;
  int size_read;
  if (buffer == NULL)
    buffer = malloc(0);
  if (buffer == NULL)
    return (NULL);
  size_read = 1024;
  while (size_read == 1024) {
    reading = malloc((1024 + 1) * sizeof(char));
    if (reading == NULL)
      return (NULL);
    if ((size_read = read(fd, reading, 1024)) == -1)
      return (NULL);
    reading[size_read] = 0;
    buffer = my_ralloc_cat(buffer, reading);
    if (buffer == NULL)
      return (NULL);
    free(reading);
    if (my_search(buffer, '\n') != -1 || my_search(buffer, '\0') != -1)
      return (my_first_line(buffer));
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<get_next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpq   $0x0,0x2ee5(%rip)
jne    1187 <get_next_line+0x27>
xor    %edi,%edi
call   1070 <malloc@plt>
mov    %rax,0x2ed1(%rip)
cmpq   $0x0,0x2ec9(%rip)
jne    11a2 <get_next_line+0x42>
movq   $0x0,-0x8(%rbp)
jmp    12a0 <get_next_line+0x140>
movl   $0x400,-0x1c(%rbp)
cmpl   $0x400,-0x1c(%rbp)
jne    1298 <get_next_line+0x138>
mov    $0x401,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dc <get_next_line+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    12a0 <get_next_line+0x140>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1206 <get_next_line+0xa6>
movq   $0x0,-0x8(%rbp)
jmp    12a0 <get_next_line+0x140>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e3f(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <my_ralloc_cat@plt>
mov    %rax,0x2e2f(%rip)
cmpq   $0x0,0x2e27(%rip)
jne    1244 <get_next_line+0xe4>
movq   $0x0,-0x8(%rbp)
jmp    12a0 <get_next_line+0x140>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e04(%rip),%rdi
mov    $0xa,%esi
call   1040 <my_search@plt>
cmp    $0xffffffff,%eax
jne    127e <get_next_line+0x11e>
mov    0x2dea(%rip),%rdi
xor    %esi,%esi
call   1040 <my_search@plt>
cmp    $0xffffffff,%eax
je     1293 <get_next_line+0x133>
mov    0x2dd3(%rip),%rdi
call   1080 <my_first_line@plt>
mov    %rax,-0x8(%rbp)
jmp    12a0 <get_next_line+0x140>
jmp    11a9 <get_next_line+0x49>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_next_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
cmpq   $0x0,0x2ee6(%rip)
jne    118b <get_next_line+0x2b>
xor    %edi,%edi
call   1070 <malloc@plt>
mov    %rax,0x2ed6(%rip)
test   %rax,%rax
je     1246 <get_next_line+0xe6>
mov    $0x400,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x400,%r15d
jne    1246 <get_next_line+0xe6>
mov    $0x401,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1246 <get_next_line+0xe6>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1246 <get_next_line+0xe6>
mov    %eax,%r15d
cltq
movb   $0x0,(%rbx,%rax,1)
mov    0x2e70(%rip),%rdi
mov    %rbx,%rsi
call   1050 <my_ralloc_cat@plt>
mov    %rax,0x2e61(%rip)
test   %rax,%rax
je     1246 <get_next_line+0xe6>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
mov    $0xa,%esi
call   1040 <my_search@plt>
cmp    $0xffffffff,%eax
jne    1230 <get_next_line+0xd0>
mov    0x2e38(%rip),%rdi
xor    %esi,%esi
call   1040 <my_search@plt>
cmp    $0xffffffff,%eax
je     11a0 <get_next_line+0x40>
mov    0x2e21(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <my_first_line@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@get_next_line.buffer = internal global i8* null
define i8* @get_next_line(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i8*, i8** @get_next_line.buffer
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i8* @malloc(i32 0)
store i8* %9, i8** @get_next_line.buffer
br label %10
10: ; preds = %8, %1
%11 = load i8*, i8** @get_next_line.buffer
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10
store i8* null, i8** %2
br label %55
14: ; preds = %10
store i32 1024, i32* %5
br label %15
15: ; preds = %53, %14
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 1024
br i1 %17, label %18, label %54
18: ; preds = %15
%19 = call i8* @malloc(i32 1025)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %18
store i8* null, i8** %2
br label %55
23: ; preds = %18
%24 = load i32, i32* %3
%25 = load i8*, i8** %4
%26 = call i32 @read(i32 %24, i8* %25, i32 1024)
store i32 %26, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %23
store i8* null, i8** %2
br label %55
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** @get_next_line.buffer
%35 = load i8*, i8** %4
%36 = call i8* @my_ralloc_cat(i8* %34, i8* %35)
store i8* %36, i8** @get_next_line.buffer
%37 = load i8*, i8** @get_next_line.buffer
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %29
store i8* null, i8** %2
br label %55
40: ; preds = %29
%41 = load i8*, i8** %4
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** @get_next_line.buffer
%44 = call i32 @my_search(i8* %43, i8 signext 10)
%45 = icmp ne i32 %44, -1
br i1 %45, label %50, label %46
46: ; preds = %40
%47 = load i8*, i8** @get_next_line.buffer
%48 = call i32 @my_search(i8* %47, i8 signext 0)
%49 = icmp ne i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %46, %40
%51 = load i8*, i8** @get_next_line.buffer
%52 = call i8* @my_first_line(i8* %51)
store i8* %52, i8** %2
br label %55
53: ; preds = %46
br label %15
54: ; preds = %15
store i8* null, i8** %2
br label %55
55: ; preds = %54, %50, %39, %28, %22, %13
%56 = load i8*, i8** %2
ret i8* %56
}

LLVM IR (-O2):
@get_next_line.buffer = internal unnamed_addr global i8* null
define i8* @get_next_line(i32 %0) {
%2 = load i8*, i8** @get_next_line.buffer
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i8* @malloc(i32 0)
store i8* %5, i8** @get_next_line.buffer
%6 = icmp eq i8* %5, null
br i1 %6, label %34, label %7
7: ; preds = %1, %4
br label %8
8: ; preds = %7, %27
%9 = phi i32 [ %15, %27 ], [ 1024, %7 ]
%10 = icmp eq i32 %9, 1024
br i1 %10, label %11, label %34
11: ; preds = %8
%12 = tail call dereferenceable_or_null(1025) i8* @malloc(i32 1025)
%13 = icmp eq i8* %12, null
br i1 %13, label %34, label %14
14: ; preds = %11
%15 = tail call i32 @read(i32 %0, i8* %12, i32 1024)
%16 = icmp eq i32 %15, -1
br i1 %16, label %34, label %17
17: ; preds = %14
%18 = sext i32 %15 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
store i8 0, i8* %19
%20 = load i8*, i8** @get_next_line.buffer
%21 = tail call i8* @my_ralloc_cat(i8* %20, i8* %12)
store i8* %21, i8** @get_next_line.buffer
%22 = icmp eq i8* %21, null
br i1 %22, label %34, label %23
23: ; preds = %17
%24 = tail call i32 @free(i8* %12)
%25 = tail call i32 @my_search(i8* %21, i8 signext 10)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i8*, i8** @get_next_line.buffer
%29 = tail call i32 @my_search(i8* %28, i8 signext 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %8, label %31
31: ; preds = %27, %23
%32 = load i8*, i8** @get_next_line.buffer
%33 = tail call i8* @my_first_line(i8* %32)
br label %34
34: ; preds = %8, %17, %14, %11, %4, %31
%35 = phi i8* [ %33, %31 ], [ null, %4 ], [ null, %11 ], [ null, %14 ], [ null, %17 ], [ null, %8 ]
ret i8* %35
}