#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 *);
int /*<<< orphan*/ *ft_strchr(char *, char);
int gnl_append_to_backup(int, char *, char **);
scalar_t__ malloc(int);
int read(int const, char *, int);
int gnl_read_and_save(const int fd, char **rbak) {
  char *rbuf;
  int res;
  int go_on;
  go_on = 1;
  rbuf = (char *)malloc(sizeof(char) * (4096 + 1));
  if (!rbuf)
    return (-1);
  res = read(fd, rbuf, 4096);
  while (go_on && res > 0) {
    rbuf[res] = '\0';
    if (ft_strchr(rbuf, '\n') != NULL)
      go_on = 0;
    res = gnl_append_to_backup(res, rbuf, rbak);
    if (!res) {
      free(rbuf);
      return (-1);
    }
    res = (go_on ? read(fd, rbuf, 4096) : 1);
  }
  free(rbuf);
  return (res);
}


x86 assembly (clang, -O0):
<gnl_read_and_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x20(%rbp)
mov    $0x1001,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118b <gnl_read_and_save+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <gnl_read_and_save+0x11f>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
je     11b8 <gnl_read_and_save+0x68>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11c8 <gnl_read_and_save+0x78>
jmp    1260 <gnl_read_and_save+0x110>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    $0xa,%esi
call   1070 <ft_strchr@plt>
cmp    $0x0,%rax
je     11f3 <gnl_read_and_save+0xa3>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <gnl_append_to_backup@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1225 <gnl_read_and_save+0xd5>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <gnl_read_and_save+0x11f>
cmpl   $0x0,-0x20(%rbp)
je     1248 <gnl_read_and_save+0xf8>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
jmp    1255 <gnl_read_and_save+0x105>
mov    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1255 <gnl_read_and_save+0x105>
mov    -0x28(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    119f <gnl_read_and_save+0x4f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gnl_read_and_save>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0x1001,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11da <gnl_read_and_save+0x8a>
mov    %rax,%r12
mov    %r15d,%edi
mov    %rax,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11e9 <gnl_read_and_save+0x99>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%eax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rdi
mov    $0xa,%esi
call   1070 <ft_strchr@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <gnl_append_to_backup@plt>
test   %eax,%eax
je     11e1 <gnl_read_and_save+0x91>
test   %rbx,%rbx
jne    11e1 <gnl_read_and_save+0x91>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11e9 <gnl_read_and_save+0x99>
test   %rbx,%rbx
je     1190 <gnl_read_and_save+0x40>
jmp    11e9 <gnl_read_and_save+0x99>
mov    $0xffffffff,%ebp
jmp    11f1 <gnl_read_and_save+0xa1>
cmp    $0x1,%eax
sbb    %ebp,%ebp
or     $0x1,%ebp
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gnl_read_and_save(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%9 = call i64 @malloc(i32 4097)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 -1, i32* %3
br label %59
14: ; preds = %2
%15 = load i32, i32* %4
%16 = load i8*, i8** %6
%17 = call i32 @read(i32 %15, i8* %16, i32 4096)
store i32 %17, i32* %7
br label %18
18: ; preds = %53, %14
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %7
%23 = icmp sgt i32 %22, 0
br label %24
24: ; preds = %21, %18
%25 = phi i1 [ false, %18 ], [ %23, %21 ]
br i1 %25, label %26, label %55
26: ; preds = %24
%27 = load i8*, i8** %6
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load i8*, i8** %6
%32 = call i32* @ft_strchr(i8* %31, i8 signext 10)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %26
store i32 0, i32* %8
br label %35
35: ; preds = %34, %26
%36 = load i32, i32* %7
%37 = load i8*, i8** %6
%38 = load i8**, i8*** %5
%39 = call i32 @gnl_append_to_backup(i32 %36, i8* %37, i8** %38)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp ne i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %35
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
store i32 -1, i32* %3
br label %59
45: ; preds = %35
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i32, i32* %4
%50 = load i8*, i8** %6
%51 = call i32 @read(i32 %49, i8* %50, i32 4096)
br label %53
52: ; preds = %45
br label %53
53: ; preds = %52, %48
%54 = phi i32 [ %51, %48 ], [ 1, %52 ]
store i32 %54, i32* %7
br label %18
55: ; preds = %24
%56 = load i8*, i8** %6
%57 = call i32 @free(i8* %56)
%58 = load i32, i32* %7
store i32 %58, i32* %3
br label %59
59: ; preds = %55, %42, %13
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
define i32 @gnl_read_and_save(i32 %0, i8** %1) {
%3 = tail call i64 @malloc(i32 4097)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %28, label %6
6: ; preds = %2
%7 = tail call i32 @read(i32 %0, i8* %4, i32 4096)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %6, %19
%10 = phi i32 [ %20, %19 ], [ %7, %6 ]
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %4, i64 %11
store i8 0, i8* %12
%13 = tail call i32* @ft_strchr(i8* %4, i8 signext 10)
%14 = icmp eq i32* %13, null
%15 = tail call i32 @gnl_append_to_backup(i32 %10, i8* %4, i8** %1)
%16 = icmp eq i32 %15, 0
%17 = xor i1 %14, true
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %23, label %19
19: ; preds = %9
%20 = tail call i32 @read(i32 %0, i8* %4, i32 4096)
%21 = icmp sgt i32 %20, 0
%22 = and i1 %21, %14
br i1 %22, label %9, label %25
23: ; preds = %9
%24 = select i1 %16, i32 -1, i32 1
br label %25
25: ; preds = %23, %19, %6
%26 = phi i32 [ %7, %6 ], [ %24, %23 ], [ %20, %19 ]
%27 = tail call i32 @free(i8* %4)
br label %28
28: ; preds = %25, %2
%29 = phi i32 [ -1, %2 ], [ %26, %25 ]
ret i32 %29
}