#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*/ close(int);
int /*<<< orphan*/ free(char *);
char *ft_strcat(char *, char *);
char *ft_strcpy_allo(char *, char *);
scalar_t__ malloc(int);
int read(int, char *, int);
char *ft_getinput(int fd) {
  char *out;
  int ret;
  char buf[4096 + 1];
  char *tmp;
  long t_len;
  if (fd == -1)
    return ("0\n");
  t_len = 0;
  out = (char *)malloc(10);
  *out = '\0';
  while ((ret = read(fd, buf, 4096)) > 0) {
    tmp = ft_strcpy_allo(tmp, out);
    free(out);
    t_len += ret;
    out = (char *)malloc(sizeof(char) * (t_len + 1));
    *out = '\0';
    buf[ret] = '\0';
    out = ft_strcat(out, tmp);
    out = ft_strcat(out, buf);
    free(tmp);
  }
  close(fd);
  return (out);
}


x86 assembly (clang, -O0):
<ft_getinput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1188 <ft_getinput+0x28>
lea    0xe81(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1270 <ft_getinput+0x110>
movq   $0x0,-0x1030(%rbp)
mov    $0xa,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1260 <ft_getinput+0x100>
mov    -0x1028(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <ft_strcpy_allo@plt>
mov    %rax,-0x1028(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movslq -0x1c(%rbp),%rax
add    -0x1030(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
mov    -0x1028(%rbp),%rsi
call   1040 <ft_strcat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1040 <ft_strcat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
jmp    11a8 <ft_getinput+0x48>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_getinput>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
cmp    $0xffffffff,%edi
je     1169 <ft_getinput+0x39>
mov    %edi,%ebx
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    1170 <ft_getinput+0x40>
lea    0xe90(%rip),%r14
mov    %r14,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0\0A\00"
define i8* @ft_getinput(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4097 x i8]
%7 = alloca i8*
%8 = alloca i64
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %2
br label %53
12: ; preds = %1
store i64 0, i64* %8
%13 = call i64 @malloc(i32 10)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %4
%15 = load i8*, i8** %4
store i8 0, i8* %15
br label %16
16: ; preds = %21, %12
%17 = load i32, i32* %3
%18 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 4096)
store i32 %19, i32* %5
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %49
21: ; preds = %16
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = call i8* @ft_strcpy_allo(i8* %22, i8* %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %4
%26 = call i32 @free(i8* %25)
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = load i64, i64* %8
%30 = add nsw i64 %29, %28
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = add nsw i64 %31, 1
%33 = mul i64 1, %32
%34 = trunc i64 %33 to i32
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %4
%37 = load i8*, i8** %4
store i8 0, i8* %37
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %4
%42 = load i8*, i8** %7
%43 = call i8* @ft_strcat(i8* %41, i8* %42)
store i8* %43, i8** %4
%44 = load i8*, i8** %4
%45 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 0
%46 = call i8* @ft_strcat(i8* %44, i8* %45)
store i8* %46, i8** %4
%47 = load i8*, i8** %7
%48 = call i32 @free(i8* %47)
br label %16
49: ; preds = %16
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %53
53: ; preds = %49, %11
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"0\0A\00"
define i8* @ft_getinput(i32 %0) {
%2 = alloca [4097 x i8]
%3 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%4 = icmp eq i32 %0, -1
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = tail call i64 @malloc(i32 10)
%7 = inttoptr i64 %6 to i8*
store i8 0, i8* %7
%8 = call i32 @read(i32 %0, i8* %3, i32 4096)
%9 = icmp slt i32 %8, 1
%10 = call i32 @close(i32 %0)
br label %11
11: ; preds = %1, %5
%12 = phi i8* [ %7, %5 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %1 ]
ret i8* %12
}