#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 *getenv(char *);
char *malloc(int);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *build_path(char *word, int *n) {
  char *path = malloc((strlen(word) + 3) * sizeof(char));
  if (word[strlen(word) - 1] == '/') {
    if (strncmp(word, "/", 1) && strncmp(word, "./", 2) &&
        strncmp(word, "../", 3)) {
      strncpy(path, "./", 2);
    } else {
      strncpy(path, "\0", 1);
    }
    strncat(path, word, strlen(word) + 1);
    *n = 1;
    return path;
  } else {
    return getenv("PATH");
  }
}


x86 assembly (clang, -O0):
<build_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x3,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1285 <build_path+0x125>
mov    -0x10(%rbp),%rdi
lea    0xe40(%rip),%rsi
mov    $0x1,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     122e <build_path+0xce>
mov    -0x10(%rbp),%rdi
lea    0xe20(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     122e <build_path+0xce>
mov    -0x10(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x3,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     122e <build_path+0xce>
mov    -0x20(%rbp),%rdi
lea    0xde2(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncpy@plt>
jmp    1243 <build_path+0xe3>
mov    -0x20(%rbp),%rdi
lea    0xdd0(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1070 <strncat@plt>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1295 <build_path+0x135>
lea    0xd78(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_path>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <strlen@plt>
lea    0x3(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    11e8 <build_path+0x88>
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     11f9 <build_path+0x99>
lea    0xe55(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     11f9 <build_path+0x99>
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     11f9 <build_path+0x99>
lea    0xe23(%rip),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
jmp    1208 <build_path+0xa8>
lea    0xe15(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <getenv@plt>
lea    0xe09(%rip),%rsi
mov    %r15,%rdi
mov    $0x1,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <strncat@plt>
movl   $0x1,(%r14)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [4 x i8] c"../\00"
@.str.3 = [2 x i8] zeroinitializer
@.str.4 = [5 x i8] c"PATH\00"
define i8* @build_path(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 3
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %6, -1
%8 = sext 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 %29
12: ; preds = %2
%13 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @strncpy(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
br label %25
23: ; preds = %18, %15, %12
%24 = tail call i32 @strncpy(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %25
25: ; preds = %23, %21
%26 = tail call i32 @strlen(i8* %0)
%27 = add nsw i32 %26, 1
%28 = tail call i32 @strncat(i8* %5, i8* %0, i32 %27)
store i32 1, i32* %1
br label %31
29: ; preds = %2
%30 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = phi i8* [ %5, %25 ], [ %30, %29 ]
ret i8* %32
}