#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int uint16_t;
struct TYPE_2__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
void *htonl(int /*<<< orphan*/);
void *htons(int);
scalar_t__ listen(int, int);
int random();
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int getListenSocket(uint16_t *desiredPort) {
  int sock, t;
  struct sockaddr_in addr;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  t = 1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &t, sizeof(int));
  if (*desiredPort != 0) {
    addr.sin_family = AF_INET;
    addr.sin_port = htons(*desiredPort);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
      return -1;
    }
  } else {
    t = 0;
    while (1) {
      addr.sin_family = AF_INET;
      *desiredPort = (random() % 64512) + 1024;
      addr.sin_port = htons(*desiredPort);
      addr.sin_addr.s_addr = htonl(INADDR_ANY);
      if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        if (t < 10) {
          ++t;
          continue;
        } else {
          return -1;
        }
      }
      break;
    }
  }
  if (listen(sock, 8) < 0) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<getListenSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ae <getListenSocket+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <getListenSocket+0x194>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     1241 <getListenSocket+0xd1>
mov    0x2df3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    123c <getListenSocket+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <getListenSocket+0x194>
jmp    12db <getListenSocket+0x16b>
movl   $0x0,-0x18(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    $0xfc00,%ecx
cltd
idiv   %ecx
add    $0x400,%edx
mov    -0x10(%rbp),%rax
mov    %edx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12d1 <getListenSocket+0x161>
cmpl   $0xa,-0x18(%rbp)
jge    12c5 <getListenSocket+0x155>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1248 <getListenSocket+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <getListenSocket+0x194>
jmp    12d6 <getListenSocket+0x166>
jmp    12db <getListenSocket+0x16b>
mov    -0x14(%rbp),%edi
mov    $0x8,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12fe <getListenSocket+0x18e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <getListenSocket+0x194>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getListenSocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbp
mov    0x2e58(%rip),%r12
mov    (%r12),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     12cd <getListenSocket+0x15d>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%edi
test   %edi,%edi
je     123e <getListenSocket+0xce>
mov    (%r12),%rax
mov    %rax,0x20(%rsp)
call   1040 <htons@plt>
mov    %rax,0x18(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     12cd <getListenSocket+0x15d>
mov    %ebx,%edi
mov    $0x8,%esi
call   1070 <listen@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
mov    %ebx,%r14d
jmp    12cd <getListenSocket+0x15d>
mov    0x2d93(%rip),%r13
lea    0x10(%rsp),%r15
xor    %eax,%eax
nopl   0x0(%rax)
mov    %eax,0xc(%rsp)
mov    (%r12),%rax
mov    %rax,0x20(%rsp)
xor    %eax,%eax
call   1060 <random@plt>
cltq
imul   $0xffffffff82082083,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0xf,%ecx
add    %edx,%ecx
imul   $0xfc00,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x400,%edi
mov    %edi,0x0(%rbp)
call   1040 <htons@plt>
mov    %rax,0x18(%rsp)
mov    0x0(%r13),%edi
call   1050 <htonl@plt>
mov    %rax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    121f <getListenSocket+0xaf>
mov    0xc(%rsp),%ecx
lea    0x1(%rcx),%eax
cmp    $0xa,%ecx
jl     1250 <getListenSocket+0xe0>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
define i32 @getListenSocket(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32* %0, i32** %3
%7 = load i8*, i8** @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i8* %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %73
12: ; preds = %1
store i32 1, i32* %5
%13 = load i32, i32* %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %5, i32 4)
%17 = load i32*, i32** %3
%18 = load i32, i32* %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %12
%21 = load i8*, i8** @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i8* %21, i8** %22
%23 = load i32*, i32** %3
%24 = load i32, i32* %23
%25 = call i8* @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i8* %25, i8** %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i8* @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i8* %28, i8** %30
%31 = load i32, i32* %4
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 24)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %20
store i32 -1, i32* %2
br label %73
36: ; preds = %20
br label %66
37: ; preds = %12
store i32 0, i32* %5
br label %38
38: ; preds = %37, %60
%39 = load i8*, i8** @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i8* %39, i8** %40
%41 = call i32 (...) @random()
%42 = srem i32 %41, 64512
%43 = add nsw i32 %42, 1024
%44 = load i32*, i32** %3
store i32 %43, i32* %44
%45 = load i32*, i32** %3
%46 = load i32, i32* %45
%47 = call i8* @htons(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i8* %47, i8** %48
%49 = load i32, i32* @INADDR_ANY
%50 = call i8* @htonl(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i8* %50, i8** %52
%53 = load i32, i32* %4
%54 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%55 = call i64 @bind(i32 %53, %struct.sockaddr* %54, i32 24)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %38
%58 = load i32, i32* %5
%59 = icmp slt i32 %58, 10
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load i32, i32* %5
%62 = add nsw i32 %61, 1
store i32 %62, i32* %5
br label %38
63: ; preds = %57
store i32 -1, i32* %2
br label %73
64: ; preds = %38
br label %65
65: ; preds = %64
br label %66
66: ; preds = %65, %36
%67 = load i32, i32* %4
%68 = call i64 @listen(i32 %67, i32 8)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %66
store i32 -1, i32* %2
br label %73
71: ; preds = %66
%72 = load i32, i32* %4
store i32 %72, i32* %2
br label %73
73: ; preds = %71, %70, %63, %35, %11
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
define i32 @getListenSocket(i32* nocapture %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i8*, i8** @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i8* %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %51, label %10
10: ; preds = %1
store i32 1, i32* %2
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = call i32 @setsockopt(i32 %8, i32 %11, i32 %12, i32* %2, i32 4)
%14 = load i32, i32* %0
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %32
21: ; preds = %10
%22 = load i8*, i8** @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i8* %22, i8** %23
%24 = call i8* @htons(i32 %14)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i8* %24, i8** %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i8* @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i8* %27, i8** %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @bind(i32 %8, %struct.sockaddr* %29, i32 24)
%31 = icmp slt i64 %30, 0
br i1 %31, label %51, label %47
32: ; preds = %43, %16
%33 = phi i32 [ %46, %43 ], [ 0, %16 ]
store i32 %33, i32* %2
%34 = load i8*, i8** @AF_INET
store i8* %34, i8** %17
%35 = call i32 (...) @random()
%36 = srem i32 %35, 64512
%37 = add nsw i32 %36, 1024
store i32 %37, i32* %0
%38 = call i8* @htons(i32 %37)
store i8* %38, i8** %18
%39 = load i32, i32* @INADDR_ANY
%40 = call i8* @htonl(i32 %39)
store i8* %40, i8** %19
%41 = call i64 @bind(i32 %8, %struct.sockaddr* %20, i32 24)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %32
%44 = load i32, i32* %2
%45 = icmp slt i32 %44, 10
%46 = add nsw i32 %44, 1
br i1 %45, label %32, label %51
47: ; preds = %32, %21
%48 = call i64 @listen(i32 %8, i32 8)
%49 = icmp slt i64 %48, 0
%50 = select i1 %49, i32 -1, i32 %8
br label %51
51: ; preds = %43, %47, %21, %1
%52 = phi i32 [ -1, %1 ], [ -1, %21 ], [ %50, %47 ], [ -1, %43 ]
ret i32 %52
}