#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 *basename(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ isdir(char const *);
char *realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char const *);
int strlen(char *);
char *build_dst(const char *src, const char *dst) {
  if (isdir(dst)) {
    char *src_copy = strdup(src);
    char *path = strdup(dst);
    char *file = basename(src_copy);
    if (!path || !file)
      return NULL;
    path = realloc(path, strlen(path) + strlen(file) + 2);
    if (!path)
      return NULL;
    if (path[strlen(path) - 1] != '/') {
      strcat(path, "/");
    }
    strcat(path, file);
    free(src_copy);
    return path;
  } else {
    return strdup(dst);
  }
}


x86 assembly (clang, -O0):
<build_dst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <isdir@plt>
cmp    $0x0,%rax
je     1284 <build_dst+0x114>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <basename@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d0 <build_dst+0x60>
cmpq   $0x0,-0x30(%rbp)
jne    11dd <build_dst+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    1291 <build_dst+0x121>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdi
add    %eax,%esi
add    $0x2,%esi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1227 <build_dst+0xb7>
movq   $0x0,-0x8(%rbp)
jmp    1291 <build_dst+0x121>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1261 <build_dst+0xf1>
mov    -0x28(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1291 <build_dst+0x121>
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_dst>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <isdir@plt>
test   %rax,%rax
je     1219 <build_dst+0xa9>
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1050 <basename@plt>
xor    %ebx,%ebx
test   %r12,%r12
je     122e <build_dst+0xbe>
mov    %rax,%r15
test   %rax,%rax
je     122e <build_dst+0xbe>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%esi
add    $0x2,%esi
mov    %r12,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     122c <build_dst+0xbc>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     1204 <build_dst+0x94>
lea    0xe04(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    122e <build_dst+0xbe>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1090 <strdup@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @build_dst(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i64 @isdir(i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %57
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %5
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %6
%18 = call i8* @basename(i8* %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21, %12
store i8* null, i8** %3
br label %60
25: ; preds = %21
%26 = load i8*, i8** %7
%27 = load i8*, i8** %7
%28 = call i32 @strlen(i8* %27)
%29 = load i8*, i8** %8
%30 = call i32 @strlen(i8* %29)
%31 = add nsw i32 %28, %30
%32 = add nsw i32 %31, 2
%33 = call i8* @realloc(i8* %26, i32 %32)
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %37, label %36
36: ; preds = %25
store i8* null, i8** %3
br label %60
37: ; preds = %25
%38 = load i8*, i8** %7
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %38, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 47
br i1 %46, label %47, label %50
47: ; preds = %37
%48 = load i8*, i8** %7
%49 = call i32 @strcat(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %47, %37
%51 = load i8*, i8** %7
%52 = load i8*, i8** %8
%53 = call i32 @strcat(i8* %51, i8* %52)
%54 = load i8*, i8** %6
%55 = call i32 @free(i8* %54)
%56 = load i8*, i8** %7
store i8* %56, i8** %3
br label %60
57: ; preds = %2
%58 = load i8*, i8** %5
%59 = call i8* @strdup(i8* %58)
store i8* %59, i8** %3
br label %60
60: ; preds = %57, %50, %36, %24
%61 = load i8*, i8** %3
ret i8* %61
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @build_dst(i8* nocapture %0, i8* %1) {
%3 = tail call i64 @isdir(i8* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %31, label %5
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %0)
%7 = tail call i8* @strdup(i8* %1)
%8 = tail call i8* @basename(i8* %6)
%9 = icmp ne i8* %7, null
%10 = icmp ne i8* %8, null
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %12, label %33
12: ; preds = %5
%13 = tail call i32 @strlen(i8* %7)
%14 = tail call i32 @strlen(i8* %8)
%15 = add i32 %13, 2
%16 = add i32 %15, %14
%17 = tail call i8* @realloc(i8* %7, i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %33, label %19
19: ; preds = %12
%20 = tail call i32 @strlen(i8* %17)
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %17, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 47
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = tail call i32 @strcat(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = tail call i32 @strcat(i8* %17, i8* %8)
%30 = tail call i32 @free(i8* %6)
br label %33
31: ; preds = %2
%32 = tail call i8* @strdup(i8* %1)
br label %33
33: ; preds = %28, %5, %12, %31
%34 = phi i8* [ %32, %31 ], [ %17, %28 ], [ null, %5 ], [ null, %12 ]
ret i8* %34
}