#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_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ rand();
scalar_t__ socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned short ILibGetDGramSocket(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 = (int)socket(AF_INET, SOCK_DGRAM, 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);
  return (PortNum);
}


x86 assembly (clang, -O0):
<ILibGetDGramSocket>:
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    0x2e49(%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 <ILibGetDGramSocket+0x58>
movzwl -0x12(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ILibGetDGramSocket>:
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    0x2e59(%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 <ILibGetDGramSocket+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_DGRAM = global i32 0
define zeroext i16 @ILibGetDGramSocket(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_DGRAM
%16 = call i64 @socket(i32 %14, i32 %15, i32 0)
%17 = trunc i64 %16 to i32
%18 = load i32*, i32** %4
store i32 %17, i32* %18
br label %19
19: ; preds = %29, %2
%20 = call i64 (...) @rand()
%21 = trunc i64 %20 to i16
%22 = zext i16 %21 to i32
%23 = srem i32 %22, 15000
%24 = add nsw i32 50000, %23
%25 = trunc i32 %24 to i16
store i16 %25, i16* %5
%26 = load i16, i16* %5
%27 = call i32 @htons(i16 zeroext %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %27, i32* %28
br label %29
29: ; preds = %19
%30 = load i32*, i32** %4
%31 = load i32, i32* %30
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %19, label %35
35: ; preds = %29
%36 = load i16, i16* %5
ret i16 %36
}

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_DGRAM = global i32 0
define zeroext i16 @ILibGetDGramSocket(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_DGRAM
%10 = call i64 @socket(i32 %6, i32 %9, i32 0)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %1
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%13 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %14
14: ; preds = %14, %2
%15 = call i64 (...) @rand()
%16 = trunc i64 %15 to i16
%17 = urem i16 %16, 15000
%18 = add nuw nsw i16 %17, -15536
%19 = call i32 @htons(i16 zeroext %18)
store i32 %19, i32* %12
%20 = load i32, i32* %1
%21 = call i64 @bind(i32 %20, %struct.sockaddr* %13, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %14, label %23
23: ; preds = %14
ret i16 %18
}