#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
char *emit(char *p, char *s) {
  if (p == NULL) {
    p = strdup(s);
    if (p == NULL) {
      {
        fprintf(stderr, "ERROR: ");
        fprintf(stderr, "emit strdup failed\n");
        exit(-1);
      };
    }
  } else {
    p = realloc(p, (strlen(p) + strlen(s) + 1) * sizeof(char));
    if (p == NULL) {
      {
        fprintf(stderr, "ERROR: ");
        fprintf(stderr, "emit realloc failed\n");
        exit(-1);
      };
    }
    p = strcat(p, s);
  }
  return p;
}


x86 assembly (clang, -O0):
<emit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11cc <emit+0x6c>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c7 <emit+0x67>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    1258 <emit+0xf8>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1247 <emit+0xe7>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<emit>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   %rdi,%rdi
je     11a4 <emit+0x44>
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
je     11bd <emit+0x5d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
jmp    11b4 <emit+0x54>
mov    %r14,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11dd <emit+0x7d>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e1c(%rip),%rbx
mov    (%rbx),%edi
lea    0xe33(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe41(%rip),%rsi
jmp    11fb <emit+0x9b>
mov    0x2dfc(%rip),%rbx
mov    (%rbx),%edi
lea    0xe13(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe0d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [8 x i8] c"ERROR: \00"
@.str.1 = [20 x i8] c"emit strdup failed\0A\00"
@.str.2 = [21 x i8] c"emit realloc failed\0A\00"
define i8* @emit(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %19
7: ; preds = %2
%8 = load i8*, i8** %4
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %7
br label %43
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* %21)
%23 = load i8*, i8** %4
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %22, %24
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i8* @realloc(i8* %20, i32 %29)
store i8* %30, i8** %3
%31 = load i8*, i8** %3
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %39
33: ; preds = %19
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %19
%40 = load i8*, i8** %3
%41 = load i8*, i8** %4
%42 = call i8* @strcat(i8* %40, i8* %41)
store i8* %42, i8** %3
br label %43
43: ; preds = %39, %18
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [8 x i8] c"ERROR: \00"
@.str.1 = [20 x i8] c"emit strdup failed\0A\00"
@.str.2 = [21 x i8] c"emit realloc failed\0A\00"
define i8* @emit(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %13
4: ; preds = %2
%5 = tail call i8* @strdup(i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %28
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = tail call i32 @strlen(i8* %0)
%15 = tail call i32 @strlen(i8* %1)
%16 = add i32 %14, 1
%17 = add i32 %16, %15
%18 = tail call i8* @realloc(i8* %0, i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %26
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %13
%27 = tail call i8* @strcat(i8* %18, i8* dereferenceable(1) %1)
br label %28
28: ; preds = %4, %26
%29 = phi i8* [ %5, %4 ], [ %18, %26 ]
ret i8* %29
}