#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);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
char *strdup(char const *);
int strlen(char const *);
char *construct_full_path(const char *dir, const char *path) {
  int i;
  char *env, *p;
  if (path[0] != '\0' && path[0] == '~' && path[1] == '/') {
    env = getenv("HOME");
    i = strlen(env) + strlen(&path[1]) + 1;
    p = calloc(i, sizeof(char));
    snprintf(p, i, "%s%s", env, &path[1]);
  } else if (dir == NULL || path[0] == '/')
    p = strdup(path);
  else {
    i = strlen(dir) + 1 + strlen(path) + 1;
    p = calloc(i, sizeof(char));
    snprintf(p, i, "%s/%s", dir, path);
  }
  return p;
}


x86 assembly (clang, -O0):
<construct_full_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11fc <construct_full_path+0xac>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    11fc <construct_full_path+0xac>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    11fc <construct_full_path+0xac>
lea    0xe68(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
add    $0x1,%r8
lea    0xe13(%rip),%rdx
call   1050 <snprintf@plt>
jmp    127f <construct_full_path+0x12f>
cmpq   $0x0,-0x8(%rbp)
je     1217 <construct_full_path+0xc7>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1229 <construct_full_path+0xd9>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
jmp    127a <construct_full_path+0x12a>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd95(%rip),%rdx
call   1050 <snprintf@plt>
jmp    127f <construct_full_path+0x12f>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<construct_full_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%al
cmp    $0x7e,%al
jne    11b2 <construct_full_path+0x62>
cmpb   $0x2f,0x1(%rbx)
jne    1206 <construct_full_path+0xb6>
add    $0x1,%rbx
lea    0xe88(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%ebp
add    $0x1,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
lea    0xe55(%rip),%rdx
jmp    11e8 <construct_full_path+0x98>
test   %r14,%r14
je     120b <construct_full_path+0xbb>
cmp    $0x2f,%al
je     120b <construct_full_path+0xbb>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
add    $0x2,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
lea    0xe22(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rcx
mov    %rbx,%r8
call   1050 <snprintf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r14,%r14
jne    11bb <construct_full_path+0x6b>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <strdup@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @construct_full_path(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 126
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %1, i64 1
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 47
br i1 %8, label %9, label %21
9: ; preds = %5
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @strlen(i8* %10)
%12 = tail call i32 @strlen(i8* %6)
%13 = add i32 %11, 1
%14 = add i32 %13, %12
%15 = tail call i8* @calloc(i32 %14, i32 1)
%16 = tail call i32 @snprintf(i8* %15, i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %6)
br label %32
17: ; preds = %2
%18 = icmp eq i8* %0, null
%19 = icmp eq i8 %3, 47
%20 = select i1 %18, i1 true, i1 %19
br i1 %20, label %23, label %25
21: ; preds = %5
%22 = icmp eq i8* %0, null
br i1 %22, label %23, label %25
23: ; preds = %21, %17
%24 = tail call i8* @strdup(i8* %1)
br label %32
25: ; preds = %17, %21
%26 = tail call i32 @strlen(i8* %0)
%27 = tail call i32 @strlen(i8* %1)
%28 = add i32 %26, 2
%29 = add i32 %28, %27
%30 = tail call i8* @calloc(i32 %29, i32 1)
%31 = tail call i32 @snprintf(i8* %30, i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
br label %32
32: ; preds = %23, %25, %9
%33 = phi i8* [ %15, %9 ], [ %24, %23 ], [ %30, %25 ]
ret i8* %33
}