#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(scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char const *, char);
char *buildpath(const char *path, const char *filename) {
  char *joint;
  const char *fpnt;
  if (path == NULL || filename == NULL)
    return NULL;
  fpnt = strrchr(filename, '/') + 1;
  if ((joint = malloc(strlen(path) + strlen(fpnt) + 1)) == NULL)
    return NULL;
  strcpy(joint, path);
  strcat(joint, fpnt);
  return joint;
}


x86 assembly (clang, -O0):
<buildpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1176 <buildpath+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1183 <buildpath+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11fc <buildpath+0xac>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11da <buildpath+0x8a>
movq   $0x0,-0x8(%rbp)
jmp    11fc <buildpath+0xac>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buildpath>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     11ba <buildpath+0x6a>
test   %rsi,%rsi
je     11ba <buildpath+0x6a>
mov    %rdi,%r14
mov    %rsi,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,%r15
add    $0x1,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b8 <buildpath+0x68>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
jmp    11ba <buildpath+0x6a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @buildpath(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %18, label %6
6: ; preds = %2
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 47)
%8 = getelementptr inbounds i8, i8* %7, i64 1
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = tail call i64 @strlen(i8* %8)
%11 = add i64 %9, 1
%12 = add i64 %11, %10
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %6
%16 = tail call i32 @strcpy(i8* %13, i8* %0)
%17 = tail call i32 @strcat(i8* %13, i8* %8)
br label %18
18: ; preds = %6, %2, %15
%19 = phi i8* [ %13, %15 ], [ null, %2 ], [ null, %6 ]
ret i8* %19
}