#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(unsigned char *);
unsigned char *malloc(unsigned int);
unsigned char *realloc(unsigned char *, unsigned int);
void allocate(unsigned char **ein, unsigned int *length) {
  unsigned char *tmp;
  (*length) += 1;
  if (*ein == NULL) {
    tmp = malloc(((*length) + 1) * sizeof(unsigned char));
  } else
    tmp = realloc(*ein, (*length) * sizeof(unsigned char));
  assert(tmp);
  tmp[(*length) - 1] = '\0';
  *ein = tmp;
}


x86 assembly (clang, -O0):
<allocate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1178 <allocate+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    1194 <allocate+0x64>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <assert@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%edi
lea    0x1(%rdi),%esi
mov    %esi,(%r15)
mov    (%r14),%rax
test   %rax,%rax
je     1155 <allocate+0x25>
mov    %rax,%rdi
call   1050 <realloc@plt>
jmp    115d <allocate+0x2d>
add    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <assert@plt>
mov    (%r15),%eax
add    $0xffffffff,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @allocate(i8** %0, i32* %1) {
%3 = alloca i8**
%4 = alloca i32*
%5 = alloca i8*
store i8** %0, i8*** %3
store i32* %1, i32** %4
%6 = load i32*, i32** %4
%7 = load i32, i32* %6
%8 = add i32 %7, 1
store i32 %8, i32* %6
%9 = load i8**, i8*** %3
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32*, i32** %4
%14 = load i32, i32* %13
%15 = add i32 %14, 1
%16 = zext i32 %15 to i64
%17 = mul i64 %16, 1
%18 = trunc i64 %17 to i32
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %5
br label %29
20: ; preds = %2
%21 = load i8**, i8*** %3
%22 = load i8*, i8** %21
%23 = load i32*, i32** %4
%24 = load i32, i32* %23
%25 = zext i32 %24 to i64
%26 = mul i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i8* @realloc(i8* %22, i32 %27)
store i8* %28, i8** %5
br label %29
29: ; preds = %20, %12
%30 = load i8*, i8** %5
%31 = call i32 @assert(i8* %30)
%32 = load i8*, i8** %5
%33 = load i32*, i32** %4
%34 = load i32, i32* %33
%35 = sub i32 %34, 1
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %32, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %5
%39 = load i8**, i8*** %3
store i8* %38, i8** %39
ret void
}

LLVM IR (-O2):
define void @allocate(i8** nocapture %0, i32* nocapture %1) {
%3 = load i32, i32* %1
%4 = add i32 %3, 1
store i32 %4, i32* %1
%5 = load i8*, i8** %0
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = add i32 %3, 2
%9 = tail call i8* @malloc(i32 %8)
br label %12
10: ; preds = %2
%11 = tail call i8* @realloc(i8* %5, i32 %4)
br label %12
12: ; preds = %10, %7
%13 = phi i8* [ %9, %7 ], [ %11, %10 ]
%14 = tail call i32 @assert(i8* %13)
%15 = load i32, i32* %1
%16 = add i32 %15, -1
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %13, i64 %17
store i8 0, i8* %18
store i8* %13, i8** %0
ret void
}