#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc_i_cat(char *, char *, int, int *);
int read(int const, char *, int);
char *get_next_line(const int fd) {
  static char buff[(30) + 1];
  char *line;
  static ssize_t rd;
  static int j;
  int i;
  line = NULL;
  i = 0;
  while (1) {
    if (j >= rd) {
      j = 0;
      if ((rd = read(fd, buff, (30))) <= 0)
        return (line);
      i = 0;
    }
    if (buff[j + i] == '\n')
      return (malloc_i_cat(line, buff, i, &j));
    if (j + i == rd - 1)
      line = malloc_i_cat(line, buff, i + 1, &j);
    i++;
  }
  free(line);
  return (NULL);
}


x86 assembly (clang, -O0):
<get_next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2f24(%rip),%eax
cmp    0x2f1a(%rip),%eax
jl     118d <get_next_line+0x6d>
movl   $0x0,0x2f0e(%rip)
mov    -0xc(%rbp),%edi
lea    0x2ee0(%rip),%rsi
mov    $0x1e,%edx
call   1030 <read@plt>
mov    %eax,0x2ef0(%rip)
cmp    $0x0,%eax
jg     1186 <get_next_line+0x66>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1219 <get_next_line+0xf9>
movl   $0x0,-0x1c(%rbp)
mov    0x2ed1(%rip),%eax
add    -0x1c(%rbp),%eax
movslq %eax,%rcx
lea    0x2ea0(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11d0 <get_next_line+0xb0>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0x2e85(%rip),%rsi
lea    0x2ea2(%rip),%rcx
call   1040 <malloc_i_cat@plt>
mov    %rax,-0x8(%rbp)
jmp    1219 <get_next_line+0xf9>
mov    0x2e8e(%rip),%eax
add    -0x1c(%rbp),%eax
mov    0x2e81(%rip),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    120b <get_next_line+0xeb>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
lea    0x2e45(%rip),%rsi
lea    0x2e62(%rip),%rcx
call   1040 <malloc_i_cat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113a <get_next_line+0x1a>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_next_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r12d
xor    %r14d,%r14d
lea    0x2f0b(%rip),%rbx
lea    0x2f28(%rip),%r15
xor    %ebp,%ebp
mov    0x2f20(%rip),%ecx
mov    0x2f16(%rip),%eax
nopw   0x0(%rax,%rax,1)
cmp    %eax,%ecx
jl     1180 <get_next_line+0x60>
movl   $0x0,0x2f06(%rip)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
call   1030 <read@plt>
mov    %eax,0x2eec(%rip)
test   %eax,%eax
jle    11ce <get_next_line+0xae>
xor    %ebp,%ebp
mov    0x2ee4(%rip),%ecx
movslq %ecx,%rsi
movslq %ebp,%rdx
add    %rsi,%rdx
cmpb   $0xa,(%rbx,%rdx,1)
je     11ae <get_next_line+0x8e>
lea    -0x1(%rax),%esi
add    $0x1,%ebp
cmp    %esi,%edx
jne    1150 <get_next_line+0x30>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <malloc_i_cat@plt>
mov    %rax,%r14
jmp    113e <get_next_line+0x1e>
lea    0x2e8b(%rip),%rsi
lea    0x2ea8(%rip),%rcx
mov    %r14,%rdi
mov    %ebp,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <malloc_i_cat@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@get_next_line.buff = internal global [31 x i8] zeroinitializer
@get_next_line.rd = internal global i32 0
@get_next_line.j = internal global i32 0
define i8* @get_next_line(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
store i8* null, i8** %4
store i32 0, i32* %5
br label %6
6: ; preds = %1, %42
%7 = load i32, i32* @get_next_line.j
%8 = load i32, i32* @get_next_line.rd
%9 = icmp sge i32 %7, %8
br i1 %9, label %10, label %17
10: ; preds = %6
store i32 0, i32* @get_next_line.j
%11 = load i32, i32* %3
%12 = call i32 @read(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @get_next_line.buff, i64 0, i64 0), i32 30)
store i32 %12, i32* @get_next_line.rd
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i8*, i8** %4
store i8* %15, i8** %2
br label %45
16: ; preds = %10
store i32 0, i32* %5
br label %17
17: ; preds = %16, %6
%18 = load i32, i32* @get_next_line.j
%19 = load i32, i32* %5
%20 = add nsw i32 %18, %19
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [31 x i8], [31 x i8]* @get_next_line.buff, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = call i8* @malloc_i_cat(i8* %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @get_next_line.buff, i64 0, i64 0), i32 %28, i32* @get_next_line.j)
store i8* %29, i8** %2
br label %45
30: ; preds = %17
%31 = load i32, i32* @get_next_line.j
%32 = load i32, i32* %5
%33 = add nsw i32 %31, %32
%34 = load i32, i32* @get_next_line.rd
%35 = sub nsw i32 %34, 1
%36 = icmp eq i32 %33, %35
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
%41 = call i8* @malloc_i_cat(i8* %38, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @get_next_line.buff, i64 0, i64 0), i32 %40, i32* @get_next_line.j)
store i8* %41, i8** %4
br label %42
42: ; preds = %37, %30
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %6
45: ; preds = %26, %14
%46 = load i8*, i8** %2
ret i8* %46
}

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