#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 */
struct addrinfo {
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ cctools_debug(long long, char *, ...);
int find_localhost_addr(int, struct addrinfo **);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *getenv(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int rmonitor_client_open_socket(int *fd, struct addrinfo **addr) {
  int port;
  char *socket_info;
  struct addrinfo *res;
  socket_info = getenv("CCTOOLS_RESOURCE_MONITOR_INFO");
  if (!socket_info) {
    cctools_debug((1LL << 39), "couldn't find socket info.\n");
    return -1;
  }
  sscanf(socket_info, "%d", &port);
  cctools_debug((1LL << 39), "found socket info at %d.\n", port);
  int status = find_localhost_addr(port, &res);
  if (status != 0) {
    cctools_debug((1LL << 39), "couldn't read socket information.");
    return -1;
  }
  *fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (*fd < 0) {
    cctools_debug((1LL << 39), "couldn't open socket for writing.");
    freeaddrinfo(res);
    return -1;
  }
  *addr = res;
  return 0;
}


x86 assembly (clang, -O0):
<rmonitor_client_open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11af <rmonitor_client_open_socket+0x4f>
movabs $0x8000000000,%rdi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1040 <cctools_debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <rmonitor_client_open_socket+0x128>
mov    -0x28(%rbp),%rdi
lea    0xe80(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x1c(%rbp),%edx
movabs $0x8000000000,%rdi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1040 <cctools_debug@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <find_localhost_addr@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     121b <rmonitor_client_open_socket+0xbb>
movabs $0x8000000000,%rdi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1040 <cctools_debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <rmonitor_client_open_socket+0x128>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1276 <rmonitor_client_open_socket+0x116>
movabs $0x8000000000,%rdi
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1040 <cctools_debug@plt>
mov    -0x30(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <rmonitor_client_open_socket+0x128>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmonitor_client_open_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe8a(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d1 <rmonitor_client_open_socket+0x71>
movabs $0x8000000000,%r14
lea    0xea9(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <sscanf@plt>
mov    0x4(%rsp),%edx
lea    0xe94(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <cctools_debug@plt>
mov    0x4(%rsp),%edi
lea    0x8(%rsp),%rsi
call   1060 <find_localhost_addr@plt>
test   %eax,%eax
je     11f8 <rmonitor_client_open_socket+0x98>
lea    0xe8b(%rip),%rsi
mov    %r14,%rdi
jmp    11e2 <rmonitor_client_open_socket+0x82>
lea    0xe46(%rip),%rsi
movabs $0x8000000000,%rdi
xor    %eax,%eax
call   1040 <cctools_debug@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%rax
mov    0x8(%rax),%edi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1080 <socket@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     121c <rmonitor_client_open_socket+0xbc>
mov    0x8(%rsp),%rax
mov    %rax,(%r15)
xor    %eax,%eax
jmp    11ee <rmonitor_client_open_socket+0x8e>
lea    0xe56(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <cctools_debug@plt>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
jmp    11e9 <rmonitor_client_open_socket+0x89>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@.str = [30 x i8] c"CCTOOLS_RESOURCE_MONITOR_INFO\00"
@.str.1 = [28 x i8] c"couldn't find socket info.\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [26 x i8] c"found socket info at %d.\0A\00"
@.str.4 = [34 x i8] c"couldn't read socket information.\00"
@.str.5 = [34 x i8] c"couldn't open socket for writing.\00"
define i32 @rmonitor_client_open_socket(i32* %0, %struct.addrinfo** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.addrinfo**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
store i32* %0, i32** %4
store %struct.addrinfo** %1, %struct.addrinfo*** %5
%10 = call i8* @getenv(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
15: ; preds = %2
%16 = load i8*, i8** %7
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%18 = load i32, i32* %6
%19 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %6
%21 = call i32 @find_localhost_addr(i32 %20, %struct.addrinfo** %8)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
26: ; preds = %15
%27 = load %struct.addrinfo*, %struct.addrinfo** %8
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = load %struct.addrinfo*, %struct.addrinfo** %8
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.addrinfo*, %struct.addrinfo** %8
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @socket(i32 %29, i32 %32, i32 %35)
%37 = load i32*, i32** %4
store i32 %36, i32* %37
%38 = load i32*, i32** %4
%39 = load i32, i32* %38
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %26
%42 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = call i32 @freeaddrinfo(%struct.addrinfo* %43)
store i32 -1, i32* %3
br label %48
45: ; preds = %26
%46 = load %struct.addrinfo*, %struct.addrinfo** %8
%47 = load %struct.addrinfo**, %struct.addrinfo*** %5
store %struct.addrinfo* %46, %struct.addrinfo** %47
store i32 0, i32* %3
br label %48
48: ; preds = %45, %41, %24, %13
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@.str = [30 x i8] c"CCTOOLS_RESOURCE_MONITOR_INFO\00"
@.str.1 = [28 x i8] c"couldn't find socket info.\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [26 x i8] c"found socket info at %d.\0A\00"
@.str.4 = [34 x i8] c"couldn't read socket information.\00"
@.str.5 = [34 x i8] c"couldn't open socket for writing.\00"
define i32 @rmonitor_client_open_socket(i32* nocapture writeonly %0, %struct.addrinfo** nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca %struct.addrinfo*
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %36
11: ; preds = %2
%12 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%13 = load i32, i32* %3
%14 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %3
%16 = call i32 @find_localhost_addr(i32 %15, %struct.addrinfo** %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %11
%19 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %36
20: ; preds = %11
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 2
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %23, i32 %25, i32 %27)
store i32 %28, i32* %0
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %20
%31 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%32 = load %struct.addrinfo*, %struct.addrinfo** %4
%33 = call i32 @freeaddrinfo(%struct.addrinfo* %32)
br label %36
34: ; preds = %20
%35 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %35, %struct.addrinfo** %1
br label %36
36: ; preds = %18, %30, %34, %9
%37 = phi i32 [ -1, %9 ], [ -1, %18 ], [ -1, %30 ], [ 0, %34 ]
ret i32 %37
}