#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 */
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
char *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, scalar_t__);
char *strrchr(char *, char);
char *strstr(char *, char *);
char *find_file_path(char *file_name) {
  char *path, *tmp = NULL, *pos;
  int len = 0;
  path = getcwd(NULL, 0);
  if (path == NULL)
    printf("cannot get working directory!");
  len = strrchr(path, '/') - path;
  tmp = malloc(len + strlen(file_name) + 1);
  pos = strstr(path, "build");
  if (pos) {
    --pos;
    *pos = '\0';
  }
  strcpy(tmp, path);
  strncat(tmp, file_name, len + strlen(file_name) + 1);
  free(path);
  return tmp;
}


x86 assembly (clang, -O0):
<find_file_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1080 <getcwd@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <find_file_path+0x41>
lea    0xe34(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1240 <find_file_path+0xb0>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    %rax,%rdx
add    $0x1,%rdx
call   1090 <strncat@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_file_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
xor    %esi,%esi
call   1080 <getcwd@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b8 <find_file_path+0x28>
lea    0xe4d(%rip),%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
sub    %r15d,%eax
movslq %eax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
add    $0x1,%rbx
call   10a0 <malloc@plt>
mov    %rax,%r12
lea    0xe30(%rip),%rsi
mov    %r15,%rdi
call   10b0 <strstr@plt>
test   %rax,%rax
je     11ff <find_file_path+0x6f>
movb   $0x0,-0x1(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1090 <strncat@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [30 x i8] c"cannot get working directory!\00"
@.str.1 = [6 x i8] c"build\00"
define i8* @find_file_path(i8* %0) {
%2 = tail call i8* @getcwd(i32* null, i32 0)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 47)
%8 = ptrtoint i8* %7 to i64
%9 = ptrtoint i8* %2 to i64
%10 = sub i64 %8, %9
%11 = shl i64 %10, 32
%12 = ashr exact i64 %11, 32
%13 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%14 = add nsw i64 %12, 1
%15 = add i64 %14, %13
%16 = tail call i8* @malloc(i64 %15)
%17 = tail call i8* @strstr(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %6
%20 = getelementptr inbounds i8, i8* %17, i64 -1
store i8 0, i8* %20
br label %21
21: ; preds = %19, %6
%22 = tail call i32 @strcpy(i8* %16, i8* %2)
%23 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%24 = add i64 %14, %23
%25 = tail call i32 @strncat(i8* %16, i8* %0, i64 %24)
%26 = tail call i32 @free(i8* %2)
ret i8* %16
}