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

/* Variables and functions */
scalar_t__ _talloc_array(void const *, int, unsigned int, char *);
scalar_t__ _talloc_realloc_array(void const *, char *, int, unsigned int,
                                 char *);
scalar_t__ fgets(char *, unsigned int, int /*<<< orphan*/ *);
scalar_t__ strlen(char *);
char *talloc_grab_file(const void *ctx, FILE *file) {
  unsigned int off = 0, len = 10;
  char *ret;
  ret = (char *)_talloc_array(ctx, sizeof(char), len, "char");
  ret[0] = '\0';
  while (fgets(ret + off, len - off, file)) {
    off += strlen(ret + off);
    if (off == len - 1)
      ret = (char *)_talloc_realloc_array(ctx, ret, sizeof(char), len *= 2,
                                          "char");
  }
  return ret;
}


x86 assembly (clang, -O0):
<talloc_grab_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xa,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x1,%esi
lea    0xe8f(%rip),%rcx
call   1050 <_talloc_array@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
sub    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11fb <talloc_grab_file+0xbb>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x14(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11f6 <talloc_grab_file+0xb6>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
mov    $0x1,%edx
lea    0xe13(%rip),%r8
call   1060 <_talloc_realloc_array@plt>
mov    %rax,-0x20(%rbp)
jmp    1181 <talloc_grab_file+0x41>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<talloc_grab_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
lea    0xeab(%rip),%rcx
mov    $0xa,%ebx
mov    %rdi,(%rsp)
mov    $0x1,%esi
mov    $0xa,%edx
call   1050 <_talloc_array@plt>
mov    %rax,%r13
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    $0xa,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11e3 <talloc_grab_file+0xa3>
xor    %ebp,%ebp
mov    %r13,%r12
jmp    11aa <talloc_grab_file+0x6a>
nop
mov    %ebp,%r12d
add    %r13,%r12
mov    %ebx,%esi
sub    %ebp,%esi
mov    %r12,%rdi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11e3 <talloc_grab_file+0xa3>
mov    %ebp,%r14d
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
add    %r14d,%ebp
lea    -0x1(%rbx),%eax
cmp    %eax,%ebp
jne    1190 <talloc_grab_file+0x50>
add    %ebx,%ebx
mov    (%rsp),%rdi
mov    %r13,%rsi
mov    $0x1,%edx
mov    %ebx,%ecx
lea    0xe27(%rip),%r8
call   1060 <_talloc_realloc_array@plt>
mov    %rax,%r13
jmp    1190 <talloc_grab_file+0x50>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"char\00"
define i8* @talloc_grab_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i32 10, i32* %6
%8 = load i8*, i8** %3
%9 = load i32, i32* %6
%10 = call i64 @_talloc_array(i8* %8, i32 1, i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
br label %14
14: ; preds = %46, %2
%15 = load i8*, i8** %7
%16 = load i32, i32* %5
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i32, i32* %6
%20 = load i32, i32* %5
%21 = sub i32 %19, %20
%22 = load i32*, i32** %4
%23 = call i64 @fgets(i8* %18, i32 %21, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %14
%26 = load i8*, i8** %7
%27 = load i32, i32* %5
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = call i64 @strlen(i8* %29)
%31 = load i32, i32* %5
%32 = zext i32 %31 to i64
%33 = add nsw i64 %32, %30
%34 = trunc i64 %33 to i32
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = sub i32 %36, 1
%38 = icmp eq i32 %35, %37
br i1 %38, label %39, label %46
39: ; preds = %25
%40 = load i8*, i8** %3
%41 = load i8*, i8** %7
%42 = load i32, i32* %6
%43 = mul i32 %42, 2
store i32 %43, i32* %6
%44 = call i64 @_talloc_realloc_array(i8* %40, i8* %41, i32 1, i32 %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %7
br label %46
46: ; preds = %39, %25
br label %14
47: ; preds = %14
%48 = load i8*, i8** %7
ret i8* %48
}

LLVM IR (-O2):
@.str = [5 x i8] c"char\00"
define i8* @talloc_grab_file(i8* %0, i32* nocapture %1) {
%3 = tail call i64 @_talloc_array(i8* %0, i32 1, i32 10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = inttoptr i64 %3 to i8*
store i8 0, i8* %4
%5 = tail call i64 @fgets(i8* %4, i32 10, i32* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %29, label %7
7: ; preds = %2, %21
%8 = phi i8* [ %25, %21 ], [ %4, %2 ]
%9 = phi i8* [ %23, %21 ], [ %4, %2 ]
%10 = phi i32 [ %22, %21 ], [ 10, %2 ]
%11 = phi i32 [ %14, %21 ], [ 0, %2 ]
%12 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%13 = trunc i64 %12 to i32
%14 = add i32 %11, %13
%15 = add i32 %10, -1
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %7
%18 = shl i32 %10, 1
%19 = tail call i64 @_talloc_realloc_array(i8* %0, i8* %9, i32 1, i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = inttoptr i64 %19 to i8*
br label %21
21: ; preds = %17, %7
%22 = phi i32 [ %18, %17 ], [ %10, %7 ]
%23 = phi i8* [ %20, %17 ], [ %9, %7 ]
%24 = zext i32 %14 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = sub i32 %22, %14
%27 = tail call i64 @fgets(i8* %25, i32 %26, i32* %1)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %7
29: ; preds = %21, %2
%30 = phi i8* [ %4, %2 ], [ %23, %21 ]
ret i8* %30
}