#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*/ pathjoin_helper(char *, int *, int, char const *);
char *sink_malloc_safe(int);
scalar_t__ strlen(char const *);
char *pathjoin(const char *prev, const char *next) {
  int prev_len = (int)strlen(prev);
  int next_len = (int)strlen(next);
  int len = prev_len + next_len + 4;
  char *res = sink_malloc_safe(sizeof(char) * len);
  int r = 0;
  pathjoin_helper(res, &r, prev_len, prev);
  pathjoin_helper(res, &r, next_len, next);
  res[r++] = 0;
  return res;
}


x86 assembly (clang, -O0):
<pathjoin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <sink_malloc_safe@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rcx
lea    -0x2c(%rbp),%rsi
call   1040 <pathjoin_helper@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    -0x2c(%rbp),%rsi
call   1040 <pathjoin_helper@plt>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathjoin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
lea    (%rax,%r13,1),%edi
add    $0x4,%edi
call   1050 <sink_malloc_safe@plt>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rbp
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
mov    %r15,%rcx
call   1040 <pathjoin_helper@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
mov    %r14,%rcx
call   1040 <pathjoin_helper@plt>
movslq 0x4(%rsp),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @pathjoin(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i32
store i32 %12, i32* %5
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
%15 = trunc i64 %14 to i32
store i32 %15, i32* %6
%16 = load i32, i32* %5
%17 = load i32, i32* %6
%18 = add nsw i32 %16, %17
%19 = add nsw i32 %18, 4
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i8* @sink_malloc_safe(i32 %23)
store i8* %24, i8** %8
store i32 0, i32* %9
%25 = load i8*, i8** %8
%26 = load i32, i32* %5
%27 = load i8*, i8** %3
%28 = call i32 @pathjoin_helper(i8* %25, i32* %9, i32 %26, i8* %27)
%29 = load i8*, i8** %8
%30 = load i32, i32* %6
%31 = load i8*, i8** %4
%32 = call i32 @pathjoin_helper(i8* %29, i32* %9, i32 %30, i8* %31)
%33 = load i8*, i8** %8
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %8
ret i8* %38
}

LLVM IR (-O2):
define i8* @pathjoin(i8* %0, i8* %1) {
%3 = alloca i32
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = trunc i64 %4 to i32
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = trunc i64 %6 to i32
%8 = add i32 %5, 4
%9 = add i32 %8, %7
%10 = tail call i8* @sink_malloc_safe(i32 %9)
%11 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%12 = call i32 @pathjoin_helper(i8* %10, i32* %3, i32 %5, i8* %0)
%13 = call i32 @pathjoin_helper(i8* %10, i32* %3, i32 %7, i8* %1)
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %10, i64 %15
store i8 0, i8* %16
ret i8* %10
}