#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_addrlen;
  int /*<<< orphan*/ ai_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char const *);
int /*<<< orphan*/ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ cctools_debug(long long, char *, ...);
int find_localhost_addr(int, struct addrinfo **);
int /*<<< orphan*/ free(struct addrinfo *);
char *getenv(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int rmonitor_server_open_socket(int *fd, int *port) {
  struct addrinfo *addr;
  int low = 1024;
  int high = 32767;
  const char *lowstr = getenv("TCP_LOW_PORT");
  if (lowstr)
    low = atoi(lowstr);
  const char *highstr = getenv("TCP_HIGH_PORT");
  if (highstr)
    high = atoi(highstr);
  if (high < low) {
    cctools_debug((1LL << 39), "high port %d is less than low port %d in range",
                  high, low);
    return 0;
  }
  *fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (*fd < 0) {
    cctools_debug((1LL << 39), "couldn't open socket for reading.");
    return 0;
  }
  for (*port = low; *port <= high; *port += 1) {
    int status = find_localhost_addr(*port, &addr);
    if (!bind(*fd, addr->ai_addr, addr->ai_addrlen)) {
      free(addr);
      cctools_debug((1LL << 39), "socket open at port %d\n", *port);
      return *port;
    }
    if (status == 0)
      free(addr);
  }
  cctools_debug((1LL << 39), "couldn't find open port for socket.");
  return 0;
}


x86 assembly (clang, -O0):
<rmonitor_server_open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x400,-0x24(%rbp)
movl   $0x7fff,-0x28(%rbp)
lea    0xe6b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11b5 <rmonitor_server_open_socket+0x45>
mov    -0x30(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x24(%rbp)
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11dc <rmonitor_server_open_socket+0x6c>
mov    -0x38(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1212 <rmonitor_server_open_socket+0xa2>
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
movabs $0x8000000000,%rdi
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1050 <cctools_debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <rmonitor_server_open_socket+0x1bb>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1264 <rmonitor_server_open_socket+0xf4>
movabs $0x8000000000,%rdi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1050 <cctools_debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <rmonitor_server_open_socket+0x1bb>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x28(%rbp),%eax
jg     130c <rmonitor_server_open_socket+0x19c>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <find_localhost_addr@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
cmp    $0x0,%eax
jne    12e4 <rmonitor_server_open_socket+0x174>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
movabs $0x8000000000,%rdi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1050 <cctools_debug@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <rmonitor_server_open_socket+0x1bb>
cmpl   $0x0,-0x3c(%rbp)
jne    12f7 <rmonitor_server_open_socket+0x187>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
jmp    12fc <rmonitor_server_open_socket+0x18c>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    126d <rmonitor_server_open_socket+0xfd>
movabs $0x8000000000,%rdi
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1050 <cctools_debug@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmonitor_server_open_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe78(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119e <rmonitor_server_open_socket+0x2e>
mov    %rax,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
jmp    11a3 <rmonitor_server_open_socket+0x33>
mov    $0x400,%ebp
lea    0xe63(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11e9 <rmonitor_server_open_socket+0x79>
mov    %rax,%rdi
call   1070 <atoi@plt>
mov    %eax,%r15d
cmp    %ebp,%r15d
jge    11f4 <rmonitor_server_open_socket+0x84>
lea    0xe50(%rip),%rsi
xor    %r13d,%r13d
movabs $0x8000000000,%rdi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <cctools_debug@plt>
jmp    12b4 <rmonitor_server_open_socket+0x144>
mov    $0x7fff,%r15d
cmp    %ebp,%r15d
jl     11c4 <rmonitor_server_open_socket+0x54>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %r13d,%r13d
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     126c <rmonitor_server_open_socket+0xfc>
movabs $0x8000000000,%r12
mov    %ebp,(%rbx)
mov    %rsp,%r13
jmp    123c <rmonitor_server_open_socket+0xcc>
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%eax
lea    0x1(%rax),%ebp
mov    %ebp,(%rbx)
cmp    %r15d,%eax
jge    127f <rmonitor_server_open_socket+0x10f>
mov    %ebp,%edi
mov    %r13,%rsi
call   1080 <find_localhost_addr@plt>
mov    %eax,%ebp
mov    (%r14),%edi
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1060 <bind@plt>
test   %eax,%eax
je     1295 <rmonitor_server_open_socket+0x125>
test   %ebp,%ebp
jne    1230 <rmonitor_server_open_socket+0xc0>
mov    (%rsp),%rdi
call   1040 <free@plt>
jmp    1230 <rmonitor_server_open_socket+0xc0>
lea    0xdd7(%rip),%rsi
movabs $0x8000000000,%rdi
jmp    128c <rmonitor_server_open_socket+0x11c>
lea    0xdfe(%rip),%rsi
xor    %r13d,%r13d
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <cctools_debug@plt>
jmp    12b4 <rmonitor_server_open_socket+0x144>
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    (%rbx),%edx
lea    0xdc5(%rip),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <cctools_debug@plt>
mov    (%rbx),%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@.str = [13 x i8] c"TCP_LOW_PORT\00"
@.str.1 = [14 x i8] c"TCP_HIGH_PORT\00"
@.str.2 = [47 x i8] c"high port %d is less than low port %d in range\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.3 = [34 x i8] c"couldn't open socket for reading.\00"
@.str.4 = [24 x i8] c"socket open at port %d\0A\00"
@.str.5 = [36 x i8] c"couldn't find open port for socket.\00"
define i32 @rmonitor_server_open_socket(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 1024, i32* %7
store i32 32767, i32* %8
%12 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %9
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %7
br label %18
18: ; preds = %15, %2
%19 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %10
%20 = load i8*, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %10
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %8
br label %25
25: ; preds = %22, %18
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %31)
store i32 0, i32* %3
br label %86
33: ; preds = %25
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_DGRAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
%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 %43
41: ; preds = %33
%42 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %86
43: ; preds = %33
%44 = load i32, i32* %7
%45 = load i32*, i32** %5
store i32 %44, i32* %45
br label %46
46: ; preds = %80, %43
%47 = load i32*, i32** %5
%48 = load i32, i32* %47
%49 = load i32, i32* %8
%50 = icmp sle i32 %48, %49
br i1 %50, label %51, label %84
51: ; preds = %46
%52 = load i32*, i32** %5
%53 = load i32, i32* %52
%54 = call i32 @find_localhost_addr(i32 %53, %struct.addrinfo** %6)
store i32 %54, i32* %11
%55 = load i32*, i32** %4
%56 = load i32, i32* %55
%57 = load %struct.addrinfo*, %struct.addrinfo** %6
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load %struct.addrinfo*, %struct.addrinfo** %6
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @bind(i32 %56, i32 %59, i32 %62)
%64 = icmp ne i32 %63, 0
br i1 %64, label %73, label %65
65: ; preds = %51
%66 = load %struct.addrinfo*, %struct.addrinfo** %6
%67 = call i32 @free(%struct.addrinfo* %66)
%68 = load i32*, i32** %5
%69 = load i32, i32* %68
%70 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %69)
%71 = load i32*, i32** %5
%72 = load i32, i32* %71
store i32 %72, i32* %3
br label %86
73: ; preds = %51
%74 = load i32, i32* %11
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load %struct.addrinfo*, %struct.addrinfo** %6
%78 = call i32 @free(%struct.addrinfo* %77)
br label %79
79: ; preds = %76, %73
br label %80
80: ; preds = %79
%81 = load i32*, i32** %5
%82 = load i32, i32* %81
%83 = add nsw i32 %82, 1
store i32 %83, i32* %81
br label %46
84: ; preds = %46
%85 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %86
86: ; preds = %84, %65, %41, %29
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@.str = [13 x i8] c"TCP_LOW_PORT\00"
@.str.1 = [14 x i8] c"TCP_HIGH_PORT\00"
@.str.2 = [47 x i8] c"high port %d is less than low port %d in range\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.3 = [34 x i8] c"couldn't open socket for reading.\00"
@.str.4 = [24 x i8] c"socket open at port %d\0A\00"
@.str.5 = [36 x i8] c"couldn't find open port for socket.\00"
define i32 @rmonitor_server_open_socket(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo** %3 to i8*
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @atoi(i8* %5)
br label %9
9: ; preds = %7, %2
%10 = phi i32 [ %8, %7 ], [ 1024, %2 ]
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @atoi(i8* %11)
br label %15
15: ; preds = %13, %9
%16 = phi i32 [ %14, %13 ], [ 32767, %9 ]
%17 = icmp slt i32 %16, %10
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 %10)
br label %56
20: ; preds = %15
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %0
%24 = icmp slt i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %20
store i32 %10, i32* %1
br label %28
26: ; preds = %20
%27 = tail call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %56
28: ; preds = %25, %50
%29 = phi i32 [ %52, %50 ], [ %10, %25 ]
%30 = call i32 @find_localhost_addr(i32 %29, %struct.addrinfo** %3)
%31 = load i32, i32* %0
%32 = load %struct.addrinfo*, %struct.addrinfo** %3
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @bind(i32 %31, i32 %34, i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %28
%40 = icmp eq i32 %30, 0
br i1 %40, label %41, label %50
41: ; preds = %39
%42 = load %struct.addrinfo*, %struct.addrinfo** %3
%43 = call i32 @free(%struct.addrinfo* %42)
br label %50
44: ; preds = %28
%45 = load %struct.addrinfo*, %struct.addrinfo** %3
%46 = call i32 @free(%struct.addrinfo* %45)
%47 = load i32, i32* %1
%48 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %1
br label %56
50: ; preds = %39, %41
%51 = load i32, i32* %1
%52 = add nsw i32 %51, 1
store i32 %52, i32* %1
%53 = icmp slt i32 %51, %16
br i1 %53, label %28, label %54
54: ; preds = %50
%55 = call i32 (i64, i8*, ...) @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %44, %54, %26, %18
%57 = phi i32 [ 0, %18 ], [ 0, %26 ], [ %49, %44 ], [ 0, %54 ]
ret i32 %57
}