#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 */
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  void *sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ra;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
void *htons(unsigned short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ rand();
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
scalar_t__ socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned short ILibGetStreamSocket(int local, unsigned short PortNumber,
                                   int *TheSocket) {
  int ra = 1;
  unsigned short PortNum = -1;
  struct sockaddr_in addr;
  memset((char *)&(addr), 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = local;
  *TheSocket = (int)socket(AF_INET, SOCK_STREAM, 0);
  if (PortNumber == 0) {
    do {
      PortNum = (unsigned short)(50000 + ((unsigned short)rand() % 15000));
      addr.sin_port = htons(PortNum);
    } while (bind((int)*TheSocket, (struct sockaddr *)&(addr), sizeof(addr)) <
             0);
  } else {
    addr.sin_port = htons(PortNumber);
    if (setsockopt((int)*TheSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&ra,
                   sizeof(ra)) < 0) {
    }
    PortNum =
        bind((int)*TheSocket, (struct sockaddr *)&(addr), sizeof(addr)) < 0
            ? 0
            : PortNumber;
  }
  return (PortNum);
}


x86 assembly (clang, -O0):
<ILibGetStreamSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movw   $0xffff,-0x16(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movzwl -0x6(%rbp),%eax
cmp    $0x0,%eax
jne    1227 <ILibGetStreamSocket+0xc7>
jmp    11d8 <ILibGetStreamSocket+0x78>
mov    $0x0,%al
call   1070 <rand@plt>
movzwl %ax,%eax
mov    $0x3a98,%ecx
cltd
idiv   %ecx
add    $0xc350,%edx
mov    %dx,%ax
mov    %ax,-0x16(%rbp)
movzwl -0x16(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jl     11d8 <ILibGetStreamSocket+0x78>
jmp    12a0 <ILibGetStreamSocket+0x140>
movzwl -0x6(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    126a <ILibGetStreamSocket+0x10a>
jmp    126a <ILibGetStreamSocket+0x10a>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1292 <ILibGetStreamSocket+0x132>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    1299 <ILibGetStreamSocket+0x139>
movzwl -0x6(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %ax,-0x16(%rbp)
movzwl -0x16(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ILibGetStreamSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%ebp
movl   $0x1,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x14(%rsp)
mov    %ebp,0x10(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,(%rbx)
test   %r14d,%r14d
je     1204 <ILibGetStreamSocket+0xa4>
movzwl %r14w,%ebp
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
test   %rax,%rax
cmovs  %r15d,%ebp
jmp    1258 <ILibGetStreamSocket+0xf8>
lea    0x8(%rsp),%r14
nopl   0x0(%rax)
xor    %eax,%eax
call   1070 <rand@plt>
movzwl %ax,%ecx
shr    $0x3,%ecx
imul   $0x8bd,%ecx,%ecx
shr    $0x16,%ecx
imul   $0x3a98,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebp
add    $0xffffc350,%ebp
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rsp)
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     1210 <ILibGetStreamSocket+0xb0>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define zeroext i16 @ILibGetStreamSocket(i32 %0, i16 zeroext %1, i32* nocapture %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = call i32 @memset(i8* %7, i32 0, i32 16)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %0, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = call i64 @socket(i32 %9, i32 %12, i32 0)
%14 = trunc i64 %13 to i32
store i32 %14, i32* %2
%15 = icmp eq i16 %1, 0
br i1 %15, label %16, label %28
16: ; preds = %3
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%18 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %19
19: ; preds = %16, %19
%20 = call i64 (...) @rand()
%21 = trunc i64 %20 to i16
%22 = urem i16 %21, 15000
%23 = add nuw nsw i16 %22, -15536
%24 = call i8* @htons(i16 zeroext %23)
store i8* %24, i8** %17
%25 = load i32, i32* %2
%26 = call i64 @bind(i32 %25, %struct.sockaddr* %18, i32 16)
%27 = icmp slt i64 %26, 0
br i1 %27, label %19, label %40
28: ; preds = %3
%29 = call i8* @htons(i16 zeroext %1)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i8* %29, i8** %30
%31 = load i32, i32* %2
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i64 @setsockopt(i32 %31, i32 %32, i32 %33, i8* %6, i32 4)
%35 = load i32, i32* %2
%36 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%37 = call i64 @bind(i32 %35, %struct.sockaddr* %36, i32 16)
%38 = icmp slt i64 %37, 0
%39 = select i1 %38, i16 0, i16 %1
br label %40
40: ; preds = %19, %28
%41 = phi i16 [ %39, %28 ], [ %23, %19 ]
ret i16 %41
}