#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 BUFSIZ;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *dirname(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *, int);
int getpid();
int readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int findInterposeLibrary(char *path, int pathsize) {
  char kickstart[BUFSIZ];
  char lib[BUFSIZ];
  int size = readlink("/proc/self/exe", kickstart, BUFSIZ);
  if (size < 0) {
    fprintf(stderr,
            "kickstart[%d]: %s[%d]: "
            "Unable to readlink /proc/self/exe",
            getpid(),
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/pegasus-isi/pegasus/refs/heads/master/"
            "src/tools/pegasus-kickstart/mysystem.c",
            43);
    return -1;
  }
  char *dir = dirname(kickstart);
  strcpy(lib, dir);
  strcat(lib, "/libinterpose.so");
  if (access(lib, R_OK) == 0) {
    strncpy(path, lib, pathsize);
    return 0;
  }
  char *homedir = dirname(dir);
  strcpy(lib, homedir);
  strcat(lib, "/lib/pegasus/libinterpose.so");
  if (access(lib, R_OK) == 0) {
    strncpy(path, lib, pathsize);
    return 0;
  }
  strcpy(lib, homedir);
  strcat(lib, "/lib64/pegasus/libinterpose.so");
  if (access(lib, R_OK) == 0) {
    strncpy(path, lib, pathsize);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<findInterposeLibrary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rsi
sub    %rdx,%rsi
mov    %rsi,-0x58(%rbp)
mov    %rsi,%rsp
mov    %rcx,-0x28(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    0xe41(%rip),%rdi
call   1050 <readlink@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    123f <findInterposeLibrary+0xbf>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x5c(%rbp),%edi
mov    %eax,%edx
lea    0xde6(%rip),%rsi
lea    0xe18(%rip),%rcx
mov    $0x2b,%r8d
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    137b <findInterposeLibrary+0x1fb>
mov    -0x58(%rbp),%rdi
call   1070 <dirname@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x50(%rbp),%rdi
lea    0xea7(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x50(%rbp),%rdi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    12a8 <findInterposeLibrary+0x128>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    137b <findInterposeLibrary+0x1fb>
mov    -0x40(%rbp),%rdi
call   1070 <dirname@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x50(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x50(%rbp),%rdi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    1311 <findInterposeLibrary+0x191>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    137b <findInterposeLibrary+0x1fb>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x50(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x50(%rbp),%rdi
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    136d <findInterposeLibrary+0x1ed>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    137b <findInterposeLibrary+0x1fb>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findInterposeLibrary>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe69(%rip),%rdi
mov    %rbx,%rsi
call   1050 <readlink@plt>
test   %eax,%eax
js     1283 <findInterposeLibrary+0x103>
mov    %rbx,%rdi
call   1070 <dirname@plt>
mov    %rax,%r12
mov    %r13,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
lea    0xf1d(%rip),%rsi
mov    %r13,%rdi
call   10a0 <strcat@plt>
mov    0x2dd3(%rip),%rbx
mov    (%rbx),%esi
mov    %r13,%rdi
call   1090 <access@plt>
test   %rax,%rax
je     1271 <findInterposeLibrary+0xf1>
mov    %r12,%rdi
call   1070 <dirname@plt>
mov    %rax,%r12
mov    %r13,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
lea    0xeb7(%rip),%rsi
mov    %r13,%rdi
call   10a0 <strcat@plt>
mov    (%rbx),%esi
mov    %r13,%rdi
call   1090 <access@plt>
test   %rax,%rax
je     1271 <findInterposeLibrary+0xf1>
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
lea    0xeab(%rip),%rsi
mov    %r13,%rdi
call   10a0 <strcat@plt>
mov    (%rbx),%esi
mov    %r13,%rdi
call   1090 <access@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    12b5 <findInterposeLibrary+0x135>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
jmp    12b5 <findInterposeLibrary+0x135>
mov    0x2d56(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd66(%rip),%rsi
lea    0xd98(%rip),%rcx
mov    %ebx,%edi
mov    %eax,%edx
mov    $0x2b,%r8d
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [57 x i8] c"kickstart[%d]: %s[%d]: Unable to readlink /proc/self/exe\00"
@.str.2 = [167 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/pegasus-isi/pegasus/refs/heads/master/src/tools/pegasus-kickstart/mysystem.c\00"
@.str.3 = [17 x i8] c"/libinterpose.so\00"
@R_OK = global i32 0
@.str.4 = [29 x i8] c"/lib/pegasus/libinterpose.so\00"
@.str.5 = [31 x i8] c"/lib64/pegasus/libinterpose.so\00"
define i32 @findInterposeLibrary(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
%17 = load i32, i32* @BUFSIZ
%18 = zext i32 %17 to i64
%19 = alloca i8, i64 %18
store i64 %18, i64* %8
%20 = load i32, i32* @BUFSIZ
%21 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = call i32 (...) @getpid()
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %26, i8* getelementptr inbounds ([167 x i8], [167 x i8]* @.str.2, i64 0, i64 0), i32 43)
store i32 -1, i32* %3
store i32 1, i32* %10
br label %65
28: ; preds = %2
%29 = call i8* @dirname(i8* %16)
store i8* %29, i8** %11
%30 = load i8*, i8** %11
%31 = call i32 @strcpy(i8* %19, i8* %30)
%32 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @R_OK
%34 = call i64 @access(i8* %19, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = call i32 @strncpy(i8* %37, i8* %19, i32 %38)
store i32 0, i32* %3
store i32 1, i32* %10
br label %65
40: ; preds = %28
%41 = load i8*, i8** %11
%42 = call i8* @dirname(i8* %41)
store i8* %42, i8** %12
%43 = load i8*, i8** %12
%44 = call i32 @strcpy(i8* %19, i8* %43)
%45 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* @R_OK
%47 = call i64 @access(i8* %19, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %40
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = call i32 @strncpy(i8* %50, i8* %19, i32 %51)
store i32 0, i32* %3
store i32 1, i32* %10
br label %65
53: ; preds = %40
%54 = load i8*, i8** %12
%55 = call i32 @strcpy(i8* %19, i8* %54)
%56 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32, i32* @R_OK
%58 = call i64 @access(i8* %19, i32 %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %53
%61 = load i8*, i8** %4
%62 = load i32, i32* %5
%63 = call i32 @strncpy(i8* %61, i8* %19, i32 %62)
store i32 0, i32* %3
store i32 1, i32* %10
br label %65
64: ; preds = %53
store i32 -1, i32* %3
store i32 1, i32* %10
br label %65
65: ; preds = %64, %60, %49, %36, %24
%66 = load i8*, i8** %6
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [57 x i8] c"kickstart[%d]: %s[%d]: Unable to readlink /proc/self/exe\00"
@.str.2 = [167 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/pegasus-isi/pegasus/refs/heads/master/src/tools/pegasus-kickstart/mysystem.c\00"
@.str.3 = [17 x i8] c"/libinterpose.so\00"
@R_OK = global i32 0
@.str.4 = [29 x i8] c"/lib/pegasus/libinterpose.so\00"
@.str.5 = [31 x i8] c"/lib64/pegasus/libinterpose.so\00"
define i32 @findInterposeLibrary(i8* %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %11, i8* getelementptr inbounds ([167 x i8], [167 x i8]* @.str.2, i64 0, i64 0), i32 43)
br label %39
13: ; preds = %2
%14 = call i8* @dirname(i8* %5)
%15 = call i32 @strcpy(i8* %6, i8* %14)
%16 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* @R_OK
%18 = call i64 @access(i8* %6, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @strncpy(i8* %0, i8* %6, i32 %1)
br label %39
22: ; preds = %13
%23 = call i8* @dirname(i8* %14)
%24 = call i32 @strcpy(i8* %6, i8* %23)
%25 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%26 = load i32, i32* @R_OK
%27 = call i64 @access(i8* %6, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @strncpy(i8* %0, i8* %6, i32 %1)
br label %39
31: ; preds = %22
%32 = call i32 @strcpy(i8* %6, i8* %23)
%33 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%34 = load i32, i32* @R_OK
%35 = call i64 @access(i8* %6, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @strncpy(i8* %0, i8* %6, i32 %1)
br label %39
39: ; preds = %20, %31, %37, %29, %9
%40 = phi i32 [ -1, %9 ], [ 0, %20 ], [ 0, %29 ], [ 0, %37 ], [ -1, %31 ]
ret i32 %40
}