#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 *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *path_extend(char *p, char *ext, int dispose_old) {
  int len = 0;
  if (p != NULL)
    len += strlen(p) + 1;
  len += strlen(ext) + 1;
  char *new_path = calloc(len, 1);
  if (new_path != NULL) {
    if (p != NULL) {
      strcpy(new_path, p);
      strcat(new_path, "/");
      if (dispose_old)
        free(p);
    }
    strcat(new_path, ext);
  }
  return new_path;
}


x86 assembly (clang, -O0):
<path_extend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     118f <path_extend+0x3f>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1212 <path_extend+0xc2>
cmpq   $0x0,-0x8(%rbp)
je     1205 <path_extend+0xb5>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1070 <strcat@plt>
cmpl   $0x0,-0x14(%rbp)
je     1200 <path_extend+0xb0>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
jmp    1205 <path_extend+0xb5>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_extend>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
je     1179 <path_extend+0x29>
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbp
add    $0x1,%rbp
jmp    117e <path_extend+0x2e>
mov    $0x1,%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %eax,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11d3 <path_extend+0x83>
test   %rbx,%rbx
je     11c8 <path_extend+0x78>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe4d(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcat@plt>
test   %r15d,%r15d
je     11c8 <path_extend+0x78>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @path_extend(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = tail call i64 @strlen(i8* %0)
%7 = shl i64 %6, 32
%8 = add i64 %7, 4294967296
%9 = ashr exact i64 %8, 32
%10 = add nsw i64 %9, 1
br label %11
11: ; preds = %5, %3
%12 = phi i64 [ %10, %5 ], [ 1, %3 ]
%13 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%14 = add i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = tail call i8* @calloc(i32 %15, i32 1)
%17 = icmp eq i8* %16, null
br i1 %17, label %27, label %18
18: ; preds = %11
br i1 %4, label %25, label %19
19: ; preds = %18
%20 = tail call i32 @strcpy(i8* %16, i8* %0)
%21 = tail call i32 @strcat(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i32 %2, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @free(i8* %0)
br label %25
25: ; preds = %19, %23, %18
%26 = tail call i32 @strcat(i8* %16, i8* %1)
br label %27
27: ; preds = %25, %11
ret i8* %16
}