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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ rand();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned short ILibGetStreamSocket(int local, int *TheSocket) {
  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 = socket(AF_INET, SOCK_STREAM, 0);
  do {
    PortNum = (unsigned short)(50000 + ((unsigned short)rand() % 15000));
    addr.sin_port = htons(PortNum);
  } while (bind(*TheSocket, (struct sockaddr *)&(addr), sizeof(addr)) < 0);
  return (PortNum);
}


x86 assembly (clang, -O0):
<ILibGetStreamSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movw   $0xffff,-0x12(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   1060 <rand@plt>
movzwl %ax,%eax
mov    $0x3a98,%ecx
cltd
idiv   %ecx
add    $0xc350,%edx
mov    %dx,%ax
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jl     11a8 <ILibGetStreamSocket+0x58>
movzwl -0x12(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ILibGetStreamSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    %ebp,0x4(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,(%rbx)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <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   1030 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11a0 <ILibGetStreamSocket+0x50>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define zeroext i16 @ILibGetStreamSocket(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32* %1, i32** %4
store i16 -1, i16* %5
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = call i32 @memset(i8* %7, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* %3
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = load i32*, i32** %4
store i32 %16, i32* %17
br label %18
18: ; preds = %28, %2
%19 = call i64 (...) @rand()
%20 = trunc i64 %19 to i16
%21 = zext i16 %20 to i32
%22 = srem i32 %21, 15000
%23 = add nsw i32 50000, %22
%24 = trunc i32 %23 to i16
store i16 %24, i16* %5
%25 = load i16, i16* %5
%26 = call i32 @htons(i16 zeroext %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %26, i32* %27
br label %28
28: ; preds = %18
%29 = load i32*, i32** %4
%30 = load i32, i32* %29
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %18, label %34
34: ; preds = %28
%35 = load i16, i16* %5
ret i16 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define zeroext i16 @ILibGetStreamSocket(i32 %0, i32* nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @memset(i8* %4, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %0, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %6, i32 %9, i32 0)
store i32 %10, i32* %1
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%12 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %13
13: ; preds = %13, %2
%14 = call i64 (...) @rand()
%15 = trunc i64 %14 to i16
%16 = urem i16 %15, 15000
%17 = add nuw nsw i16 %16, -15536
%18 = call i32 @htons(i16 zeroext %17)
store i32 %18, i32* %11
%19 = load i32, i32* %1
%20 = call i64 @bind(i32 %19, %struct.sockaddr* %12, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %13, label %22
22: ; preds = %13
ret i16 %17
}