#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*/ PATH_MAX;
int R_OK;
int X_OK;
scalar_t__ access(char *, int);
int /*<<< orphan*/ cctools_debug(long long, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ realpath(char *, char *);
char *rmonitor_helper_locate(char *);
int /*<<< orphan*/ rmonitor_server_open_socket(int *, int *);
int /*<<< orphan*/ setenv(char *, char *, int);
char *string_format(char *, ...);
int rmonitor_helper_init(char *lib_default_path, int *fd) {
  int port;
  char *helper_path = rmonitor_helper_locate(lib_default_path);
  char helper_absolute[PATH_MAX + 1];
  char *rmonitor_port;
  realpath(helper_path, helper_absolute);
  if (access(helper_absolute, R_OK | X_OK) == 0) {
    cctools_debug((1LL << 39), "found helper in %s\n", helper_absolute);
    rmonitor_server_open_socket(fd, &port);
  } else {
    cctools_debug((1LL << 39),
                  "couldn't find helper library %s but continuing anyway.",
                  helper_path);
    port = -1;
  }
  if (port > 0) {
    rmonitor_port = string_format("%d", port);
    char *prev_ldpreload = getenv("LD_PRELOAD");
    char *ld_preload =
        string_format("%s%s%s", helper_absolute, prev_ldpreload ? ":" : "",
                      prev_ldpreload ? prev_ldpreload : "");
    cctools_debug((1LL << 39), "setting LD_PRELOAD to %s\n", ld_preload);
    setenv("LD_PRELOAD", ld_preload, 1);
    cctools_debug((1LL << 39), "setting %s to %s\n",
                  "CCTOOLS_RESOURCE_MONITOR_INFO", rmonitor_port);
    setenv("CCTOOLS_RESOURCE_MONITOR_INFO", rmonitor_port, 1);
    free(ld_preload);
    free(rmonitor_port);
  } else {
    *fd = -1;
  }
  free(helper_path);
  return port;
}


x86 assembly (clang, -O0):
<rmonitor_helper_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <rmonitor_helper_locate@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x50(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <realpath@plt>
mov    -0x50(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
call   10b0 <access@plt>
cmp    $0x0,%rax
jne    1238 <rmonitor_helper_init+0xa8>
mov    -0x50(%rbp),%rdx
movabs $0x8000000000,%rdi
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1090 <cctools_debug@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1060 <rmonitor_server_open_socket@plt>
jmp    125b <rmonitor_helper_init+0xcb>
mov    -0x20(%rbp),%rdx
movabs $0x8000000000,%rdi
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1090 <cctools_debug@plt>
movl   $0xffffffff,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    136e <rmonitor_helper_init+0x1de>
mov    -0x14(%rbp),%esi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1070 <string_format@plt>
mov    %rax,-0x38(%rbp)
lea    0xdcd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdx
lea    0xd7e(%rip),%rax
lea    0xdc4(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12c0 <rmonitor_helper_init+0x130>
mov    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    12d0 <rmonitor_helper_init+0x140>
lea    0xd4c(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    12d0 <rmonitor_helper_init+0x140>
mov    -0x58(%rbp),%rdx
mov    -0x50(%rbp),%rsi
mov    -0x60(%rbp),%rcx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1070 <string_format@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdx
movabs $0x8000000000,%rdi
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   1090 <cctools_debug@plt>
mov    -0x48(%rbp),%rsi
lea    0xd39(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x38(%rbp),%rcx
movabs $0x8000000000,%rdi
lea    0xd48(%rip),%rsi
lea    0xd53(%rip),%rdx
mov    $0x0,%al
call   1090 <cctools_debug@plt>
mov    -0x38(%rbp),%rsi
lea    0xd41(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
jmp    1378 <rmonitor_helper_init+0x1e8>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x14(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmonitor_helper_init>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
call   1080 <rmonitor_helper_locate@plt>
mov    %rax,%r12
mov    %rsp,%r15
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r12,%rdi
mov    %rbx,%rsi
call   10a0 <realpath@plt>
mov    0x2df5(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   10b0 <access@plt>
test   %rax,%rax
je     1221 <rmonitor_helper_init+0x91>
lea    0xe1a(%rip),%rsi
movabs $0x8000000000,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <cctools_debug@plt>
movl   $0xffffffff,-0x2c(%rbp)
movl   $0xffffffff,(%r14)
jmp    1309 <rmonitor_helper_init+0x179>
movabs $0x8000000000,%r13
lea    0xdce(%rip),%rsi
mov    %r13,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <cctools_debug@plt>
lea    -0x2c(%rbp),%rsi
mov    %r14,%rdi
call   1060 <rmonitor_server_open_socket@plt>
mov    -0x2c(%rbp),%esi
test   %esi,%esi
jle    1215 <rmonitor_helper_init+0x85>
lea    0xdf2(%rip),%rdi
xor    %eax,%eax
call   1070 <string_format@plt>
mov    %rax,%r14
lea    0xde4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xd9a(%rip),%rcx
lea    0xde0(%rip),%rdx
cmove  %rcx,%rdx
cmovne %rax,%rcx
lea    0xdca(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <string_format@plt>
mov    %rax,%rbx
lea    0xdbf(%rip),%rsi
mov    %r13,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <cctools_debug@plt>
lea    0xd97(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xdb1(%rip),%rsi
lea    0xdbc(%rip),%r13
movabs $0x8000000000,%rdi
mov    %r13,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1090 <cctools_debug@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %r15,%rsp
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
@R_OK = global i32 0
@X_OK = global i32 0
@.str = [20 x i8] c"found helper in %s\0A\00"
@.str.1 = [55 x i8] c"couldn't find helper library %s but continuing anyway.\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [11 x i8] c"LD_PRELOAD\00"
@.str.4 = [7 x i8] c"%s%s%s\00"
@.str.5 = [2 x i8] c":\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [26 x i8] c"setting LD_PRELOAD to %s\0A\00"
@.str.8 = [18 x i8] c"setting %s to %s\0A\00"
@.str.9 = [30 x i8] c"CCTOOLS_RESOURCE_MONITOR_INFO\00"
define i32 @rmonitor_helper_init(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
%12 = load i8*, i8** %3
%13 = call i8* @rmonitor_helper_locate(i8* %12)
store i8* %13, i8** %6
%14 = load i32, i32* @PATH_MAX
%15 = add nsw i32 %14, 1
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %7
%18 = alloca i8, i64 %16
store i64 %16, i64* %8
%19 = load i8*, i8** %6
%20 = call i32 @realpath(i8* %19, i8* %18)
%21 = load i32, i32* @R_OK
%22 = load i32, i32* @X_OK
%23 = or i32 %21, %22
%24 = call i64 @access(i8* %18, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %2
%27 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %18)
%28 = load i32*, i32** %4
%29 = call i32 @rmonitor_server_open_socket(i32* %28, i32* %5)
br label %33
30: ; preds = %2
%31 = load i8*, i8** %6
%32 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %31)
store i32 -1, i32* %5
br label %33
33: ; preds = %30, %26
%34 = load i32, i32* %5
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %64
36: ; preds = %33
%37 = load i32, i32* %5
%38 = call i8* (i8*, ...) @string_format(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %37)
store i8* %38, i8** %9
%39 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = icmp ne i8* %40, null
%42 = zext i1 %41 to i64
%43 = select i1 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0)
%44 = load i8*, i8** %10
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %48
46: ; preds = %36
%47 = load i8*, i8** %10
br label %49
48: ; preds = %36
br label %49
49: ; preds = %48, %46
%50 = phi i8* [ %47, %46 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), %48 ]
%51 = call i8* (i8*, ...) @string_format(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %18, i8* %43, i8* %50)
store i8* %51, i8** %11
%52 = load i8*, i8** %11
%53 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %52)
%54 = load i8*, i8** %11
%55 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %54, i32 1)
%56 = load i8*, i8** %9
%57 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i8* %56)
%58 = load i8*, i8** %9
%59 = call i32 @setenv(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i8* %58, i32 1)
%60 = load i8*, i8** %11
%61 = call i32 @free(i8* %60)
%62 = load i8*, i8** %9
%63 = call i32 @free(i8* %62)
br label %66
64: ; preds = %33
%65 = load i32*, i32** %4
store i32 -1, i32* %65
br label %66
66: ; preds = %64, %49
%67 = load i8*, i8** %6
%68 = call i32 @free(i8* %67)
%69 = load i32, i32* %5
%70 = load i8*, i8** %7
ret i32 %69
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@R_OK = global i32 0
@X_OK = global i32 0
@.str = [20 x i8] c"found helper in %s\0A\00"
@.str.1 = [55 x i8] c"couldn't find helper library %s but continuing anyway.\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [11 x i8] c"LD_PRELOAD\00"
@.str.4 = [7 x i8] c"%s%s%s\00"
@.str.5 = [2 x i8] c":\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [26 x i8] c"setting LD_PRELOAD to %s\0A\00"
@.str.8 = [18 x i8] c"setting %s to %s\0A\00"
@.str.9 = [30 x i8] c"CCTOOLS_RESOURCE_MONITOR_INFO\00"
define i32 @rmonitor_helper_init(i8* %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i8* @rmonitor_helper_locate(i8* %0)
%6 = load i32, i32* @PATH_MAX
%7 = add nsw i32 %6, 1
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @realpath(i8* %5, i8* %10)
%12 = load i32, i32* @R_OK
%13 = load i32, i32* @X_OK
%14 = or i32 %13, %12
%15 = call i64 @access(i8* %10, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %5)
store i32 -1, i32* %3
br label %37
19: ; preds = %2
%20 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %10)
%21 = call i32 @rmonitor_server_open_socket(i32* %1, i32* %3)
%22 = load i32, i32* %3
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %37
24: ; preds = %19
%25 = call i8* (i8*, ...) @string_format(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%26 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i8* %26, null
%28 = select i1 %27, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0)
%29 = select i1 %27, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8* %26
%30 = call i8* (i8*, ...) @string_format(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %10, i8* %28, i8* %29)
%31 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %30)
%32 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %30, i32 1)
%33 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i8* %25)
%34 = call i32 @setenv(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i8* %25, i32 1)
%35 = call i32 @free(i8* %30)
%36 = call i32 @free(i8* %25)
br label %38
37: ; preds = %17, %19
store i32 -1, i32* %1
br label %38
38: ; preds = %37, %24
%39 = call i32 @free(i8* %5)
%40 = load i32, i32* %3
ret i32 %40
}