#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 */
typedef int /*<<< orphan*/ wd;
typedef int /*<<< orphan*/ argv0d;

/* Variables and functions */
int ENAMETOOLONG;
int PATH_MAX;
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *rindex(char *, char);
int zsnprintf(char *, size_t, char *, char const *, ...);
int get_colocated_path(const char *argv0, const char *other, char *path,
                       size_t path_len) {
  int ret;
  char *slash;
  char wd[PATH_MAX];
  char argv0d[PATH_MAX];
  if (argv0[0] == '/') {
    wd[0] = '\0';
  } else {
    memset(wd, 0, sizeof(wd));
    if (getcwd(wd, sizeof(wd) - 1) == NULL)
      return -ENAMETOOLONG;
  }
  ret = zsnprintf(argv0d, sizeof(argv0d), "%s", argv0);
  if (ret)
    return ret;
  slash = rindex(argv0d, '/');
  if (slash == NULL)
    argv0d[0] = '\0';
  else
    slash[1] = '\0';
  ret = zsnprintf(path, path_len, "%s/%s/%s", wd, argv0d, other);
  if (ret)
    return -ENAMETOOLONG;
  return 0;
}


x86 assembly (clang, -O0):
<get_colocated_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x78(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x40(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x70(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x48(%rbp)
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11c9 <get_colocated_path+0x89>
mov    -0x70(%rbp),%rax
movb   $0x0,(%rax)
jmp    1216 <get_colocated_path+0xd6>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    1211 <get_colocated_path+0xd1>
xor    %eax,%eax
mov    0x2dd8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12d7 <get_colocated_path+0x197>
jmp    1216 <get_colocated_path+0xd6>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xddd(%rip),%rdx
mov    $0x0,%al
call   1050 <zsnprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     124f <get_colocated_path+0x10f>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12d7 <get_colocated_path+0x197>
mov    -0x60(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <rindex@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1278 <get_colocated_path+0x138>
mov    -0x60(%rbp),%rax
movb   $0x0,(%rax)
jmp    1280 <get_colocated_path+0x140>
mov    -0x38(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x60(%rbp),%r8
mov    -0x70(%rbp),%rcx
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%r9
lea    0xd65(%rip),%rdx
mov    $0x0,%al
call   1050 <zsnprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12c9 <get_colocated_path+0x189>
xor    %eax,%eax
mov    0x2d20(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12d7 <get_colocated_path+0x197>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_colocated_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,-0x38(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %rsi,-0x40(%rbp)
mov    %rdi,%r14
mov    0x2e89(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
cmpb   $0x2f,(%rdi)
jne    118f <get_colocated_path+0x4f>
movb   $0x0,(%rbx)
jmp    11b1 <get_colocated_path+0x71>
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1030 <memset@plt>
lea    -0x1(%r12),%esi
mov    %rbx,%rdi
call   1040 <getcwd@plt>
test   %rax,%rax
je     11e7 <get_colocated_path+0xa7>
lea    0xe4e(%rip),%rdx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <zsnprintf@plt>
mov    %eax,%r15d
test   %eax,%eax
jne    1229 <get_colocated_path+0xe9>
mov    %r13,%rdi
mov    $0x2f,%esi
call   1060 <rindex@plt>
test   %rax,%rax
je     11f0 <get_colocated_path+0xb0>
movb   $0x0,0x1(%rax)
jmp    11f5 <get_colocated_path+0xb5>
mov    0x2dea(%rip),%rax
jmp    1226 <get_colocated_path+0xe6>
movb   $0x0,0x0(%r13)
lea    0xe04(%rip),%rdx
xor    %r15d,%r15d
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rbx,%rcx
mov    %r13,%r8
mov    -0x40(%rbp),%r9
xor    %eax,%eax
call   1050 <zsnprintf@plt>
test   %eax,%eax
je     1229 <get_colocated_path+0xe9>
mov    0x2db5(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@ENAMETOOLONG = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [9 x i8] c"%s/%s/%s\00"
define i32 @get_colocated_path(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = alloca i8, i64 %6
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 47
br i1 %10, label %11, label %12
11: ; preds = %4
store i8 0, i8* %7
br label %20
12: ; preds = %4
%13 = call i32 @memset(i8* %7, i32 0, i32 %5)
%14 = add i32 %5, -1
%15 = call i32* @getcwd(i8* %7, i32 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @ENAMETOOLONG
%19 = sub nsw i32 0, %18
br label %35
20: ; preds = %12, %11
%21 = call i32 (i8*, i64, i8*, i8*, ...) @zsnprintf(i8* %8, i64 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = call i8* @rindex(i8* %8, i8 signext 47)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %23
store i8 0, i8* %8
br label %29
27: ; preds = %23
%28 = getelementptr inbounds i8, i8* %24, i64 1
store i8 0, i8* %28
br label %29
29: ; preds = %27, %26
%30 = call i32 (i8*, i64, i8*, i8*, ...) @zsnprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %8, i8* %1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @ENAMETOOLONG
%34 = sub nsw i32 0, %33
br label %35
35: ; preds = %29, %20, %32, %17
%36 = phi i32 [ %34, %32 ], [ %19, %17 ], [ %21, %20 ], [ 0, %29 ]
ret i32 %36
}