#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 uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ ensure_alloc(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
char *realloc(char *, int);
int strlen(char *);
char *read(FILE *input) {
  uint32_t buff_size = 4096;
  char *line_buff = calloc(buff_size, sizeof(char));
  ensure_alloc(line_buff);
  fgets(line_buff, buff_size, input);
  char *result = realloc(line_buff, strlen(line_buff) * sizeof(char));
  ensure_alloc(line_buff);
  return result;
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1000,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <ensure_alloc@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1040 <fgets@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <ensure_alloc@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1000,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <ensure_alloc@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1070 <ensure_alloc@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @read(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32* %0, i32** %2
store i32 4096, i32* %3
%6 = load i32, i32* %3
%7 = call i8* @calloc(i32 %6, i32 1)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @ensure_alloc(i8* %8)
%10 = load i8*, i8** %4
%11 = load i32, i32* %3
%12 = load i32*, i32** %2
%13 = call i32 @fgets(i8* %10, i32 %11, i32* %12)
%14 = load i8*, i8** %4
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i8* @realloc(i8* %14, i32 %19)
store i8* %20, i8** %5
%21 = load i8*, i8** %4
%22 = call i32 @ensure_alloc(i8* %21)
%23 = load i8*, i8** %5
ret i8* %23
}

LLVM IR (-O2):
define i8* @read(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(4096) i8* @calloc(i32 4096, i32 1)
%3 = tail call i32 @ensure_alloc(i8* %2)
%4 = tail call i32 @fgets(i8* %2, i32 4096, i32* %0)
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i8* @realloc(i8* %2, i32 %5)
%7 = tail call i32 @ensure_alloc(i8* %2)
ret i8* %6
}