#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*/ assert(int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *strchr(char *, char);
int strlen(char *);
char *readLineHelper(char *prefix, int n) {
  int prefixlen = strlen(prefix);
  char *result = (char *)calloc(n, 1);
  if (result == NULL)
    error("out of memory");
  assert(prefixlen < n);
  memcpy(result, prefix, prefixlen);
  if (fgets(result + prefixlen, n - prefixlen, stdin) == NULL)
    return NULL;
  if (strchr(result, '\n') == NULL)
    return readLineHelper(result, 2 * n);
  free(prefix);
  return result;
}


x86 assembly (clang, -O0):
<readLineHelper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <readLineHelper+0x43>
lea    0xe32(%rip),%rdi
call   10b0 <error@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   10a0 <memcpy@plt>
mov    -0x20(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x18(%rbp),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    122c <readLineHelper+0x9c>
movq   $0x0,-0x8(%rbp)
jmp    126d <readLineHelper+0xdd>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
jne    125c <readLineHelper+0xcc>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
shl    $0x1,%esi
call   1030 <readLineHelper@plt>
mov    %rax,-0x8(%rbp)
jmp    126d <readLineHelper+0xdd>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLineHelper>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r13
mov    0x2e50(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r13,%r12
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14d,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11ce <readLineHelper+0x4e>
lea    0xe37(%rip),%rdi
call   10a0 <error@plt>
xor    %edi,%edi
mov    %r14d,%ebp
sub    %ebx,%ebp
setg   %dil
call   1080 <assert@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1090 <memcpy@plt>
movslq %ebx,%rdi
add    %r13,%rdi
mov    (%r15),%edx
mov    %ebp,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1222 <readLineHelper+0xa2>
mov    %r13,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
add    %r14d,%r14d
test   %rax,%rax
je     11a0 <readLineHelper+0x20>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rax
jmp    1224 <readLineHelper+0xa4>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"out of memory\00"
@stdin = global i32 0
define i8* @readLineHelper(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %5
%11 = call i64 @calloc(i32 %10, i32 1)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* %6
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8*, i8** %7
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = call i32 @memcpy(i8* %23, i8* %24, i32 %25)
%27 = load i8*, i8** %7
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = sub nsw i32 %31, %32
%34 = load i32, i32* @stdin
%35 = call i32* @fgets(i8* %30, i32 %33, i32 %34)
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %38
37: ; preds = %17
store i8* null, i8** %3
br label %51
38: ; preds = %17
%39 = load i8*, i8** %7
%40 = call i32* @strchr(i8* %39, i8 signext 10)
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i8*, i8** %7
%44 = load i32, i32* %5
%45 = mul nsw i32 2, %44
%46 = call i8* @readLineHelper(i8* %43, i32 %45)
store i8* %46, i8** %3
br label %51
47: ; preds = %38
%48 = load i8*, i8** %4
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %7
store i8* %50, i8** %3
br label %51
51: ; preds = %47, %42, %37
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
@.str = [14 x i8] c"out of memory\00"
@stdin = global i32 0
define i8* @readLineHelper(i8* %0, i32 %1) {
br label %3
3: ; preds = %23, %2
%4 = phi i8* [ %0, %2 ], [ %8, %23 ]
%5 = phi i32 [ %1, %2 ], [ %26, %23 ]
%6 = tail call i32 @strlen(i8* %4)
%7 = tail call i64 @calloc(i32 %5, i32 1)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
%13 = icmp sgt i32 %5, %6
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @memcpy(i8* %8, i8* %4, i32 %6)
%17 = sext i32 %6 to i64
%18 = getelementptr inbounds i8, i8* %8, i64 %17
%19 = sub nsw i32 %5, %6
%20 = load i32, i32* @stdin
%21 = tail call i32* @fgets(i8* %18, i32 %19, i32 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %30, label %23
23: ; preds = %12
%24 = tail call i32* @strchr(i8* %8, i8 signext 10)
%25 = icmp eq i32* %24, null
%26 = shl nsw i32 %5, 1
br i1 %25, label %3, label %27
27: ; preds = %23
%28 = inttoptr i64 %7 to i8*
%29 = tail call i32 @free(i8* %4)
br label %30
30: ; preds = %12, %27
%31 = phi i8* [ %28, %27 ], [ null, %12 ]
ret i8* %31
}