#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ buf2;
typedef int /*<<< orphan*/ buf1;

/* Variables and functions */
int EIO;
int PATH_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int get_colocated_path(char *, char *, char *, int);
scalar_t__ memcmp(char *, char *, scalar_t__);
scalar_t__ simple_io_read_whole_file_zt(char *, char *, int);
int /*<<< orphan*/ stderr;
int test_get_colocated_path(char *argv0) {
  char buf1[4096], buf2[4096], path[PATH_MAX];
  ssize_t buf1_sz, buf2_sz;
  int ret;
  ret = get_colocated_path(argv0, "run_cmd_unit", path, sizeof(path));
  buf1_sz = simple_io_read_whole_file_zt(argv0, buf1, sizeof(buf1));
  if (buf1_sz < 0) {
    fprintf(stderr,
            "test_get_colocated_path: failed to read binary "
            "from '%s'\n",
            argv0);
    return buf1_sz;
  }
  buf2_sz = simple_io_read_whole_file_zt(path, buf2, sizeof(buf2));
  if (buf2_sz < 0) {
    fprintf(stderr,
            "test_get_colocated_path: failed to read binary "
            "from '%s'\n",
            argv0);
    return buf1_sz;
  }
  if ((buf1_sz != buf2_sz) || (memcmp(buf1, buf2, buf1_sz))) {
    fprintf(stderr,
            "test_get_colocated_path: the binaries read "
            "from '%s' and '%s' were not the same.\n",
            argv0, path);
    return -EIO;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_get_colocated_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x2018(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x2040(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x2020(%rbp)
mov    -0x10(%rbp),%rdi
mov    %eax,%ecx
lea    0xe6f(%rip),%rsi
call   1040 <get_colocated_path@plt>
mov    %eax,-0x2034(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <simple_io_read_whole_file_zt@plt>
mov    %rax,-0x2028(%rbp)
cmpq   $0x0,-0x2028(%rbp)
jge    11fa <test_get_colocated_path+0xba>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x2028(%rbp),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2038(%rbp)
jmp    12e3 <test_get_colocated_path+0x1a3>
mov    -0x2040(%rbp),%rdi
lea    -0x2010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <simple_io_read_whole_file_zt@plt>
mov    %rax,-0x2030(%rbp)
cmpq   $0x0,-0x2030(%rbp)
jge    125b <test_get_colocated_path+0x11b>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x2028(%rbp),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2038(%rbp)
jmp    12e3 <test_get_colocated_path+0x1a3>
mov    -0x2028(%rbp),%rax
cmp    -0x2030(%rbp),%rax
jne    1293 <test_get_colocated_path+0x153>
lea    -0x1010(%rbp),%rdi
lea    -0x2010(%rbp),%rsi
mov    -0x2028(%rbp),%rdx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     12d2 <test_get_colocated_path+0x192>
mov    -0x2040(%rbp),%rcx
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d1a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2038(%rbp)
jmp    12e3 <test_get_colocated_path+0x1a3>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2038(%rbp)
mov    -0x2018(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_get_colocated_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rdi,%r14
mov    %rsp,%r12
mov    0x2e91(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe84(%rip),%rsi
mov    %rbx,%rdx
call   1040 <get_colocated_path@plt>
lea    -0x2020(%rbp),%rsi
mov    %r14,%rdi
mov    $0x1000,%edx
call   1030 <simple_io_read_whole_file_zt@plt>
mov    %rax,%r15
test   %rax,%rax
js     1205 <test_get_colocated_path+0xc5>
lea    -0x1020(%rbp),%rsi
mov    %rbx,%rdi
mov    $0x1000,%edx
call   1030 <simple_io_read_whole_file_zt@plt>
test   %rax,%rax
js     1205 <test_get_colocated_path+0xc5>
cmp    %rax,%r15
jne    11d9 <test_get_colocated_path+0x99>
lea    -0x2020(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    %r15,%rdx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1232 <test_get_colocated_path+0xf2>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2dd8(%rip),%rax
sub    (%rax),%r15d
jmp    121f <test_get_colocated_path+0xdf>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12,%rsp
mov    %r15d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
jmp    121f <test_get_colocated_path+0xdf>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"run_cmd_unit\00"
@stderr = global i32 0
@.str.1 = [58 x i8] c"test_get_colocated_path: failed to read binary from '%s'\0A\00"
@.str.2 = [82 x i8] c"test_get_colocated_path: the binaries read from '%s' and '%s' were not the same.\0A\00"
@EIO = global i32 0
define i32 @test_get_colocated_path(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [4096 x i8]
%5 = alloca [4096 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i8*, i8** %3
%17 = trunc i64 %13 to i32
%18 = call i32 @get_colocated_path(i8* %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %17)
store i32 %18, i32* %10
%19 = load i8*, i8** %3
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%21 = call i64 @simple_io_read_whole_file_zt(i8* %19, i8* %20, i32 4096)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %1
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i64, i64* %8
%29 = trunc i64 %28 to i32
store i32 %29, i32* %2
store i32 1, i32* %11
br label %58
30: ; preds = %1
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%32 = call i64 @simple_io_read_whole_file_zt(i8* %15, i8* %31, i32 4096)
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %3
%38 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load i64, i64* %8
%40 = trunc i64 %39 to i32
store i32 %40, i32* %2
store i32 1, i32* %11
br label %58
41: ; preds = %30
%42 = load i64, i64* %8
%43 = load i64, i64* %9
%44 = icmp ne i64 %42, %43
br i1 %44, label %51, label %45
45: ; preds = %41
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%48 = load i64, i64* %8
%49 = call i64 @memcmp(i8* %46, i8* %47, i64 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %45, %41
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %3
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i8* %53, i8* %15)
%55 = load i32, i32* @EIO
%56 = sub nsw i32 0, %55
store i32 %56, i32* %2
store i32 1, i32* %11
br label %58
57: ; preds = %45
store i32 0, i32* %2
store i32 1, i32* %11
br label %58
58: ; preds = %57, %51, %35, %24
%59 = load i8*, i8** %6
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"run_cmd_unit\00"
@stderr = global i32 0
@.str.1 = [58 x i8] c"test_get_colocated_path: failed to read binary from '%s'\0A\00"
@.str.2 = [82 x i8] c"test_get_colocated_path: the binaries read from '%s' and '%s' were not the same.\0A\00"
@EIO = global i32 0
define i32 @test_get_colocated_path(i8* %0) {
%2 = alloca [4096 x i8]
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @get_colocated_path(i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %6)
%11 = call i64 @simple_io_read_whole_file_zt(i8* %0, i8* %4, i32 4096)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = trunc i64 %11 to i32
br label %34
17: ; preds = %1
%18 = call i64 @simple_io_read_whole_file_zt(i8* %9, i8* %5, i32 4096)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%23 = trunc i64 %11 to i32
br label %34
24: ; preds = %17
%25 = icmp eq i64 %11, %18
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = call i64 @memcmp(i8* %4, i8* %5, i64 %11)
%28 = icmp eq i64 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %26, %24
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %9)
%32 = load i32, i32* @EIO
%33 = sub nsw i32 0, %32
br label %34
34: ; preds = %26, %29, %20, %13
%35 = phi i32 [ %16, %13 ], [ %23, %20 ], [ %33, %29 ], [ 0, %26 ]
ret i32 %35
}