#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 */
char *realloc(char *, int);
int append_char(char **out, int *j, char c) {
  char *nout = realloc(*out, *j + 2);
  if (!nout) {
    return 0;
  }
  *out = nout;
  (*out)[*j] = c;
  *j = *j + 1;
  (*out)[*j] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
add    $0x2,%esi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1155 <append_char+0x45>
movl   $0x0,-0x4(%rbp)
jmp    119c <append_char+0x8c>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_char>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rdi),%rdi
mov    (%rsi),%esi
add    $0x2,%esi
call   1030 <realloc@plt>
test   %rax,%rax
je     1150 <append_char+0x40>
mov    %rax,(%r14)
movslq (%rbx),%rcx
mov    %bpl,(%rax,%rcx,1)
movslq (%rbx),%rax
lea    0x1(%rax),%rcx
mov    %ecx,(%rbx)
mov    (%r14),%rcx
movb   $0x0,0x1(%rcx,%rax,1)
mov    $0x1,%eax
jmp    1152 <append_char+0x42>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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