#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 *ft_memalloc(int);
int /*<<< orphan*/ printf(char *, char *);
int read(int const, char *, int);
int /*<<< orphan*/ read_zero_or_eof(char *, int *);
char *str_terminate(int *, char *, int *);
int get_next_line(const int fd, char **line) {
  static int str_buff_index;
  static int str_line_index;
  static int read_bytes;
  static char buffer[1801];
  static char *in_line;
  if (str_buff_index == -1)
    return (-1);
  if (str_buff_index == 0) {
    if ((read_bytes = read(fd, buffer, 1801)) == 0) {
      read_zero_or_eof(in_line, &str_buff_index);
      return (1);
    }
  }
  if (str_line_index == 0 && !(in_line = ft_memalloc(1801)))
    return (-1);
  while (str_buff_index < read_bytes) {
    if (buffer[str_buff_index] == '\0' || buffer[str_buff_index] == '\n') {
      *line = str_terminate(&str_buff_index, in_line, &str_line_index);
      return (1);
    }
    in_line[str_line_index] = buffer[str_buff_index];
    str_buff_index = str_buff_index + 1;
    str_line_index = str_line_index + 1;
    printf("BUFFER\n%s", in_line);
  }
  str_buff_index = 0;
  get_next_line(fd, line);
  *line = in_line;
  return (1);
}


x86 assembly (clang, -O0):
<get_next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0xffffffff,0x2eea(%rip)
jne    1188 <get_next_line+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <get_next_line+0x1b0>
cmpl   $0x0,0x2ed1(%rip)
jne    11dc <get_next_line+0x7c>
mov    -0x8(%rbp),%edi
lea    0x2ed1(%rip),%rsi
mov    $0x709,%edx
call   1040 <read@plt>
mov    %eax,0x2eb9(%rip)
cmp    $0x0,%eax
jne    11d7 <get_next_line+0x77>
mov    0x35c1(%rip),%rdi
lea    0x2e9a(%rip),%rsi
call   1070 <read_zero_or_eof@plt>
movl   $0x1,-0x4(%rbp)
jmp    1310 <get_next_line+0x1b0>
jmp    11dc <get_next_line+0x7c>
cmpl   $0x0,0x2e81(%rip)
jne    1210 <get_next_line+0xb0>
mov    $0x709,%edi
call   1050 <ft_memalloc@plt>
mov    %rax,0x3586(%rip)
cmp    $0x0,%rax
jne    1210 <get_next_line+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <get_next_line+0x1b0>
jmp    1215 <get_next_line+0xb5>
mov    0x2e45(%rip),%eax
cmp    0x2e47(%rip),%eax
jge    12e5 <get_next_line+0x185>
movslq 0x2e32(%rip),%rcx
lea    0x2e3b(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     125d <get_next_line+0xfd>
movslq 0x2e17(%rip),%rcx
lea    0x2e20(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    128d <get_next_line+0x12d>
mov    0x351c(%rip),%rsi
lea    0x2df5(%rip),%rdi
lea    0x2df2(%rip),%rdx
call   1060 <str_terminate@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1310 <get_next_line+0x1b0>
movslq 0x2dcc(%rip),%rcx
lea    0x2dd5(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    0x34db(%rip),%rax
movslq 0x2db8(%rip),%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2dab(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2da2(%rip)
mov    0x2da0(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d97(%rip)
mov    0x34ac(%rip),%rsi
lea    0xd25(%rip),%rdi
call   1030 <printf@plt>
jmp    1215 <get_next_line+0xb5>
movl   $0x0,0x2d71(%rip)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <get_next_line@plt>
mov    0x347e(%rip),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_next_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ef3(%rip),%eax
cmp    $0xffffffff,%eax
je     1288 <get_next_line+0x128>
mov    %rsi,%r14
mov    %edi,%r15d
test   %eax,%eax
jne    11a2 <get_next_line+0x42>
lea    0x2ee9(%rip),%rsi
mov    %r15d,%edi
mov    $0x709,%edx
call   1040 <read@plt>
mov    %eax,0x2ece(%rip)
test   %eax,%eax
je     1270 <get_next_line+0x110>
cmpl   $0x0,0x2ebb(%rip)
jne    11c5 <get_next_line+0x65>
mov    $0x709,%edi
call   1050 <ft_memalloc@plt>
mov    %rax,0x35c4(%rip)
test   %rax,%rax
je     1293 <get_next_line+0x133>
mov    0x2e95(%rip),%eax
cmp    0x2e97(%rip),%eax
jge    1233 <get_next_line+0xd3>
lea    0x2e96(%rip),%rbp
lea    0xe1f(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cltq
movzbl (%rax,%rbp,1),%eax
cmp    $0xa,%al
je     1251 <get_next_line+0xf1>
test   %al,%al
je     1251 <get_next_line+0xf1>
mov    0x357b(%rip),%rsi
movslq 0x2e58(%rip),%rcx
mov    %al,(%rsi,%rcx,1)
addl   $0x1,0x2e4a(%rip)
addl   $0x1,0x2e47(%rip)
mov    %rbx,%rdi
call   1030 <printf@plt>
mov    0x2e35(%rip),%eax
cmp    0x2e37(%rip),%eax
jl     11f0 <get_next_line+0x90>
movl   $0x0,0x2e23(%rip)
mov    %r15d,%edi
mov    %r14,%rsi
call   1080 <get_next_line@plt>
mov    0x3531(%rip),%rax
jmp    126b <get_next_line+0x10b>
mov    0x3528(%rip),%rsi
lea    0x2e01(%rip),%rdi
lea    0x2dfe(%rip),%rdx
call   1060 <str_terminate@plt>
mov    %rax,(%r14)
jmp    1283 <get_next_line+0x123>
mov    0x3509(%rip),%rdi
lea    0x2de2(%rip),%rsi
call   1070 <read_zero_or_eof@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1288 <get_next_line+0x128>

LLVM IR (-O0):
@get_next_line.str_buff_index = internal global i32 0
@get_next_line.str_line_index = internal global i32 0
@get_next_line.read_bytes = internal global i32 0
@get_next_line.buffer = internal global [1801 x i8] zeroinitializer
@get_next_line.in_line = internal global i8* null
@.str = [10 x i8] c"BUFFER\0A%s\00"
define i32 @get_next_line(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* @get_next_line.str_buff_index
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %2
store i32 -1, i32* %3
br label %71
9: ; preds = %2
%10 = load i32, i32* @get_next_line.str_buff_index
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i32, i32* %4
%14 = call i32 @read(i32 %13, i8* getelementptr inbounds ([1801 x i8], [1801 x i8]* @get_next_line.buffer, i64 0, i64 0), i32 1801)
store i32 %14, i32* @get_next_line.read_bytes
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** @get_next_line.in_line
%18 = call i32 @read_zero_or_eof(i8* %17, i32* @get_next_line.str_buff_index)
store i32 1, i32* %3
br label %71
19: ; preds = %12
br label %20
20: ; preds = %19, %9
%21 = load i32, i32* @get_next_line.str_line_index
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = call i8* @ft_memalloc(i32 1801)
store i8* %24, i8** @get_next_line.in_line
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %23
store i32 -1, i32* %3
br label %71
27: ; preds = %23, %20
br label %28
28: ; preds = %50, %27
%29 = load i32, i32* @get_next_line.str_buff_index
%30 = load i32, i32* @get_next_line.read_bytes
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %65
32: ; preds = %28
%33 = load i32, i32* @get_next_line.str_buff_index
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1801 x i8], [1801 x i8]* @get_next_line.buffer, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %32
%40 = load i32, i32* @get_next_line.str_buff_index
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [1801 x i8], [1801 x i8]* @get_next_line.buffer, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 10
br i1 %45, label %46, label %50
46: ; preds = %39, %32
%47 = load i8*, i8** @get_next_line.in_line
%48 = call i8* @str_terminate(i32* @get_next_line.str_buff_index, i8* %47, i32* @get_next_line.str_line_index)
%49 = load i8**, i8*** %5
store i8* %48, i8** %49
store i32 1, i32* %3
br label %71
50: ; preds = %39
%51 = load i32, i32* @get_next_line.str_buff_index
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1801 x i8], [1801 x i8]* @get_next_line.buffer, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = load i8*, i8** @get_next_line.in_line
%56 = load i32, i32* @get_next_line.str_line_index
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 %54, i8* %58
%59 = load i32, i32* @get_next_line.str_buff_index
%60 = add nsw i32 %59, 1
store i32 %60, i32* @get_next_line.str_buff_index
%61 = load i32, i32* @get_next_line.str_line_index
%62 = add nsw i32 %61, 1
store i32 %62, i32* @get_next_line.str_line_index
%63 = load i8*, i8** @get_next_line.in_line
%64 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %63)
br label %28
65: ; preds = %28
store i32 0, i32* @get_next_line.str_buff_index
%66 = load i32, i32* %4
%67 = load i8**, i8*** %5
%68 = call i32 @get_next_line(i32 %66, i8** %67)
%69 = load i8*, i8** @get_next_line.in_line
%70 = load i8**, i8*** %5
store i8* %69, i8** %70
store i32 1, i32* %3
br label %71
71: ; preds = %65, %46, %26, %16, %8
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@get_next_line.str_buff_index = internal global i32 0
@get_next_line.str_line_index = internal global i32 0
@get_next_line.read_bytes = internal unnamed_addr global i32 0
@get_next_line.buffer = internal global [1801 x i8] zeroinitializer
@get_next_line.in_line = internal unnamed_addr global i8* null
@.str = [10 x i8] c"BUFFER\0A%s\00"
define i32 @get_next_line(i32 %0, i8** nocapture writeonly %1) {
%3 = load i32, i32* @get_next_line.str_buff_index
switch i32 %3, label %10 [
i32 -1, label %41
i32 0, label %4
]
4: ; preds = %2
%5 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([1801 x i8], [1801 x i8]* @get_next_line.buffer, i64 0, i64 0), i32 1801)
store i32 %5, i32* @get_next_line.read_bytes
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i8*, i8** @get_next_line.in_line
%9 = tail call i32 @read_zero_or_eof(i8* %8, i32* @get_next_line.str_buff_index)
br label %41
10: ; preds = %2, %4
%11 = load i32, i32* @get_next_line.str_line_index
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i8* @ft_memalloc(i32 1801)
store i8* %14, i8** @get_next_line.in_line
%15 = icmp eq i8* %14, null
br i1 %15, label %41, label %16
16: ; preds = %13, %10
%17 = load i32, i32* @get_next_line.str_buff_index
%18 = load i32, i32* @get_next_line.read_bytes
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %43
20: ; preds = %16, %28
%21 = phi i32 [ %38, %28 ], [ %17, %16 ]
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [1801 x i8], [1801 x i8]* @get_next_line.buffer, i64 0, i64 %22
%24 = load i8, i8* %23
switch i8 %24, label %28 [
i8 0, label %25
i8 10, label %25
]
25: ; preds = %20, %20
%26 = load i8*, i8** @get_next_line.in_line
%27 = tail call i8* @str_terminate(i32* @get_next_line.str_buff_index, i8* %26, i32* @get_next_line.str_line_index)
store i8* %27, i8** %1
br label %41
28: ; preds = %20
%29 = load i8*, i8** @get_next_line.in_line
%30 = load i32, i32* @get_next_line.str_line_index
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %24, i8* %32
%33 = load i32, i32* @get_next_line.str_buff_index
%34 = add nsw i32 %33, 1
store i32 %34, i32* @get_next_line.str_buff_index
%35 = load i32, i32* @get_next_line.str_line_index
%36 = add nsw i32 %35, 1
store i32 %36, i32* @get_next_line.str_line_index
%37 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %29)
%38 = load i32, i32* @get_next_line.str_buff_index
%39 = load i32, i32* @get_next_line.read_bytes
%40 = icmp slt i32 %38, %39
br i1 %40, label %20, label %43
41: ; preds = %7, %25, %2, %13, %43
%42 = phi i32 [ 1, %43 ], [ 1, %7 ], [ 1, %25 ], [ %3, %2 ], [ -1, %13 ]
ret i32 %42
43: ; preds = %28, %16
store i32 0, i32* @get_next_line.str_buff_index
%44 = tail call i32 @get_next_line(i32 %0, i8** %1)
%45 = load i8*, i8** @get_next_line.in_line
store i8* %45, i8** %1
br label %41
}