#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*/ buffer_clean(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char get_char(int const);
char *malloc(int);
char *my_dup_free(char *);
char *my_realloc(char *, int);
char *get_next_line(const int fd) {
  char *buffer;
  int i;
  int j;
  char c;
  if ((buffer = malloc(sizeof(char) * (4096 + 1))) == NULL)
    return (NULL);
  i = 0;
  j = 1;
  buffer_clean(buffer, 4096 + 1, 0);
  while ((c = get_char(fd)) != -1 && c != '\0' && c != '\n') {
    if (i >= (j * 4096))
      if ((buffer = my_realloc(buffer, sizeof(char) * ((++j * 4096) + 1))) ==
          NULL)
        return (NULL);
    buffer[i++] = c;
  }
  if (c == -1 && buffer[0] == '\0') {
    free(buffer);
    return (NULL);
  }
  return (my_dup_free(buffer));
}


x86 assembly (clang, -O0):
<get_next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x1001,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1190 <get_next_line+0x30>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <get_next_line+0x147>
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1001,%esi
xor    %edx,%edx
call   1070 <buffer_clean@plt>
mov    -0xc(%rbp),%edi
call   1080 <get_char@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x22(%rbp)
je     11e9 <get_next_line+0x89>
movsbl -0x21(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x22(%rbp)
je     11e9 <get_next_line+0x89>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11f9 <get_next_line+0x99>
jmp    1267 <get_next_line+0x107>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0xc,%ecx
cmp    %ecx,%eax
jl     124a <get_next_line+0xea>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
shl    $0xc,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <my_realloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1245 <get_next_line+0xe5>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <get_next_line+0x147>
jmp    124a <get_next_line+0xea>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11ae <get_next_line+0x4e>
movsbl -0x21(%rbp),%eax
cmp    $0xffffffff,%eax
jne    129a <get_next_line+0x13a>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    129a <get_next_line+0x13a>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <get_next_line+0x147>
mov    -0x18(%rbp),%rdi
call   1060 <my_dup_free@plt>
mov    %rax,-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    $0x1001,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1206 <get_next_line+0xa6>
mov    %rax,%r15
xor    %r12d,%r12d
mov    %rax,%rdi
mov    $0x1001,%esi
xor    %edx,%edx
call   1070 <buffer_clean@plt>
mov    $0x1,%ebp
jmp    11a8 <get_next_line+0x48>
nopw   0x0(%rax,%rax,1)
mov    %bl,(%r15,%r12,1)
add    $0x1,%r12
mov    %r14d,%edi
call   1080 <get_char@plt>
cmp    $0xff,%al
je     11e8 <get_next_line+0x88>
mov    %eax,%ebx
test   %al,%al
je     11ee <get_next_line+0x8e>
cmp    $0xa,%bl
je     11ee <get_next_line+0x8e>
mov    %ebp,%eax
shl    $0xc,%eax
cltq
cmp    %rax,%r12
jl     11a0 <get_next_line+0x40>
add    $0x1,%ebp
mov    %ebp,%esi
shl    $0xc,%esi
or     $0x1,%esi
mov    %r15,%rdi
call   1050 <my_realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a0 <get_next_line+0x40>
jmp    1206 <get_next_line+0xa6>
cmpb   $0x0,(%r15)
je     11fe <get_next_line+0x9e>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <my_dup_free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @get_next_line(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %3
%8 = call i8* @malloc(i32 4097)
store i8* %8, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %70
11: ; preds = %1
store i32 0, i32* %5
store i32 1, i32* %6
%12 = load i8*, i8** %4
%13 = call i32 @buffer_clean(i8* %12, i32 4097, i32 0)
br label %14
14: ; preds = %47, %11
%15 = load i32, i32* %3
%16 = call signext i8 @get_char(i32 %15)
store i8 %16, i8* %7
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load i8, i8* %7
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i8, i8* %7
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 10
br label %27
27: ; preds = %23, %19, %14
%28 = phi i1 [ false, %19 ], [ false, %14 ], [ %26, %23 ]
br i1 %28, label %29, label %54
29: ; preds = %27
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = mul nsw i32 %31, 4096
%33 = icmp sge i32 %30, %32
br i1 %33, label %34, label %47
34: ; preds = %29
%35 = load i8*, i8** %4
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
%38 = mul nsw i32 %37, 4096
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = mul i64 1, %40
%42 = trunc i64 %41 to i32
%43 = call i8* @my_realloc(i8* %35, i32 %42)
store i8* %43, i8** %4
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %46
45: ; preds = %34
store i8* null, i8** %2
br label %70
46: ; preds = %34
br label %47
47: ; preds = %46, %29
%48 = load i8, i8* %7
%49 = load i8*, i8** %4
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
store i32 %51, i32* %5
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 %48, i8* %53
br label %14
54: ; preds = %27
%55 = load i8, i8* %7
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %67
58: ; preds = %54
%59 = load i8*, i8** %4
%60 = getelementptr inbounds i8, i8* %59, i64 0
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = load i8*, i8** %4
%66 = call i32 @free(i8* %65)
store i8* null, i8** %2
br label %70
67: ; preds = %58, %54
%68 = load i8*, i8** %4
%69 = call i8* @my_dup_free(i8* %68)
store i8* %69, i8** %2
br label %70
70: ; preds = %67, %64, %45, %10
%71 = load i8*, i8** %2
ret i8* %71
}

LLVM IR (-O2):
define i8* @get_next_line(i32 %0) {
%2 = tail call dereferenceable_or_null(4097) i8* @malloc(i32 4097)
%3 = icmp eq i8* %2, null
br i1 %3, label %33, label %4
4: ; preds = %1
%5 = tail call i32 @buffer_clean(i8* %2, i32 4097, i32 0)
br label %6
6: ; preds = %21, %4
%7 = phi i64 [ %24, %21 ], [ 0, %4 ]
%8 = phi i8* [ %22, %21 ], [ %2, %4 ]
%9 = phi i32 [ %23, %21 ], [ 1, %4 ]
%10 = tail call signext i8 @get_char(i32 %0)
switch i8 %10, label %11 [
i8 -1, label %26
i8 0, label %31
i8 10, label %31
]
11: ; preds = %6
%12 = shl nsw i32 %9, 12
%13 = sext i32 %12 to i64
%14 = icmp slt i64 %7, %13
br i1 %14, label %21, label %15
15: ; preds = %11
%16 = add nsw i32 %9, 1
%17 = shl nsw i32 %16, 12
%18 = or i32 %17, 1
%19 = tail call i8* @my_realloc(i8* %8, i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %33, label %21
21: ; preds = %15, %11
%22 = phi i8* [ %19, %15 ], [ %8, %11 ]
%23 = phi i32 [ %16, %15 ], [ %9, %11 ]
%24 = add nuw i64 %7, 1
%25 = getelementptr inbounds i8, i8* %22, i64 %7
store i8 %10, i8* %25
br label %6
26: ; preds = %6
%27 = load i8, i8* %8
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @free(i8* %8)
br label %33
31: ; preds = %6, %6, %26
%32 = tail call i8* @my_dup_free(i8* %8)
br label %33
33: ; preds = %15, %1, %31, %29
%34 = phi i8* [ null, %29 ], [ %32, %31 ], [ null, %1 ], [ null, %15 ]
ret i8* %34
}