#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*/ free(char *);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int build_string(char *sin, char **sout, int *slen, int *block) {
  *slen = strlen(sin) + strlen(*sout) + 1 + 1;
  if (*slen > *block) {
    char **o = sout;
    *block *= 2;
    *sout = (char *)realloc(*sout, sizeof(char) * (*block));
    if (*sout == NULL) {
      free(*o);
      return 2;
    }
  }
  strcat(*sout, sin);
  strcat(*sout, " ");
  return 0;
}


x86 assembly (clang, -O0):
<build_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%ecx
add    %eax,%ecx
add    $0x1,%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%eax
jle    11f4 <build_string+0xb4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11ef <build_string+0xaf>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    121e <build_string+0xde>
jmp    11f4 <build_string+0xb4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdee(%rip),%rsi
call   1060 <strcat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x0(%r13),%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
mov    %eax,(%r12)
mov    (%r15),%esi
cmp    %esi,%eax
jle    119d <build_string+0x5d>
add    %esi,%esi
mov    %esi,(%r15)
mov    0x0(%r13),%rdi
call   1050 <realloc@plt>
mov    %rax,%rdi
mov    %rax,0x0(%r13)
test   %rax,%rax
jne    11a1 <build_string+0x61>
xor    %edi,%edi
call   1030 <free@plt>
mov    $0x2,%eax
jmp    11bb <build_string+0x7b>
mov    0x0(%r13),%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    0x0(%r13),%rdi
lea    0xe4c(%rip),%rsi
call   1060 <strcat@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i32 @build_string(i8* %0, i8** nocapture %1, i32* nocapture writeonly %2, i32* nocapture %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = load i8*, i8** %1
%7 = tail call i32 @strlen(i8* %6)
%8 = add i32 %5, 2
%9 = add i32 %8, %7
store i32 %9, i32* %2
%10 = load i32, i32* %3
%11 = icmp sgt i32 %9, %10
br i1 %11, label %14, label %12
12: ; preds = %4
%13 = load i8*, i8** %1
br label %22
14: ; preds = %4
%15 = shl nsw i32 %10, 1
store i32 %15, i32* %3
%16 = load i8*, i8** %1
%17 = tail call i64 @realloc(i8* %16, i32 %15)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %1
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @free(i8* null)
br label %27
22: ; preds = %12, %14
%23 = phi i8* [ %13, %12 ], [ %18, %14 ]
%24 = tail call i32 @strcat(i8* %23, i8* %0)
%25 = load i8*, i8** %1
%26 = tail call i32 @strcat(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %20, %22
%28 = phi i32 [ 2, %20 ], [ 0, %22 ]
ret i32 %28
}