#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 */
typedef scalar_t__ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
char *join_path(char *path, char *name) {
  uint32_t len;
  char *joined;
  joined = NULL;
  len = strlen(path) + strlen(name) + 2;
  joined = malloc(len);
  if (joined == NULL)
    goto fail;
  sprintf(joined, "%s/%s", path, name);
  return joined;
fail:
  if (joined != NULL)
    free(joined);
  return NULL;
}


x86 assembly (clang, -O0):
<join_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119d <join_path+0x5d>
jmp    11c2 <join_path+0x82>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe50(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11de <join_path+0x9e>
cmpq   $0x0,-0x28(%rbp)
je     11d6 <join_path+0x96>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join_path>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1175 <join_path+0x45>
lea    0xe99(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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