#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(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *, ...);
int strlen(char const *);
char *build_path(const char *dir, const char *component) {
  const int dir_len = strlen(dir);
  const int comp_len = strlen(component);
  char *buf;
  if (dir_len > 0 && dir[dir_len - 1] == '/') {
    buf = malloc(sizeof(char) * dir_len + comp_len + 1);
    sprintf(buf, "%s%s", dir, component);
  } else {
    buf =
        malloc(sizeof(char) * dir_len + comp_len + strlen("/") + comp_len + 1);
    sprintf(buf, "%s%s%s", dir, "/", component);
  }
  return buf;
}


x86 assembly (clang, -O0):
<build_path>:
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)
cmpl   $0x0,-0x14(%rbp)
jle    11b9 <build_path+0x89>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11b9 <build_path+0x89>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    121b <build_path+0xeb>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
lea    0xe2d(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%r8
lea    0xdf5(%rip),%rsi
lea    0xdec(%rip),%rcx
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_path>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
test   %ebx,%ebx
jle    1187 <build_path+0x57>
lea    -0x1(%rbx),%eax
cmpb   $0x2f,(%r15,%rax,1)
jne    1187 <build_path+0x57>
lea    (%rbx,%r12,1),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
lea    0xe8f(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    11c2 <build_path+0x92>
lea    0xe72(%rip),%r13
mov    %r13,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%r12,2),%ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
lea    0xe53(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r13,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"%s%s%s\00"
define noalias i8* @build_path(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = add nsw i32 %3, -1
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 47
br i1 %11, label %12, label %17
12: ; preds = %6
%13 = add nuw i32 %3, 1
%14 = add i32 %13, %4
%15 = tail call i8* @malloc(i32 %14)
%16 = tail call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %25
17: ; preds = %6, %2
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = shl i32 %4, 1
%20 = add i32 %3, 1
%21 = add i32 %20, %19
%22 = add i32 %21, %18
%23 = tail call i8* @malloc(i32 %22)
%24 = tail call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %23, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %25
25: ; preds = %17, %12
%26 = phi i8* [ %15, %12 ], [ %23, %17 ]
ret i8* %26
}