#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 *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char *, char *);
size_t strlen(char *);
char *strjoin(char *str1, char *str2, char *delim) {
  char *new;
  size_t str1_len, str2_len, delim_len, new_size;
  str1_len = strlen(str1);
  str2_len = strlen(str2);
  delim_len = strlen(delim);
  new_size = str1_len + str2_len + delim_len + 1;
  new = malloc(new_size);
  if (new == NULL)
    return NULL;
  snprintf(new, new_size, "%s%s%s", str1, delim, str2);
  return new;
}


x86 assembly (clang, -O0):
<strjoin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
add    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a4 <strjoin+0x74>
movq   $0x0,-0x8(%rbp)
jmp    11cc <strjoin+0x9c>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x18(%rbp),%r9
lea    0xe41(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strjoin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %r13,%rbx
lea    (%rax,%rbx,1),%r13
add    $0x1,%r13
mov    %r13,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1193 <strjoin+0x63>
lea    0xe81(%rip),%rdx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rcx
mov    %r14,%r8
mov    %r15,%r9
call   1040 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @strjoin(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %9
%15 = load i8*, i8** %6
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %10
%17 = load i8*, i8** %7
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %11
%19 = load i64, i64* %9
%20 = load i64, i64* %10
%21 = add i64 %19, %20
%22 = load i64, i64* %11
%23 = add i64 %21, %22
%24 = add i64 %23, 1
store i64 %24, i64* %12
%25 = load i64, i64* %12
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %3
store i8* null, i8** %4
br label %38
30: ; preds = %3
%31 = load i8*, i8** %8
%32 = load i64, i64* %12
%33 = load i8*, i8** %5
%34 = load i8*, i8** %7
%35 = load i8*, i8** %6
%36 = call i32 @snprintf(i8* %31, i64 %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %34, i8* %35)
%37 = load i8*, i8** %8
store i8* %37, i8** %4
br label %38
38: ; preds = %30, %29
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @strjoin(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = add i64 %4, 1
%8 = add i64 %7, %5
%9 = add i64 %8, %6
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = tail call i32 @snprintf(i8* %10, i64 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %2, i8* %1)
br label %14
14: ; preds = %3, %12
ret i8* %10
}