#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 /*<<< orphan*/ 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*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
unsigned short ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned short get_random_port(void) {
  int sockfd = 0;
  struct sockaddr_in addr;
  unsigned short port = 0;
  int addr_len = sizeof(struct sockaddr_in);
  do {
    while ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
      perror("socket for switching port");
      sleep(1);
    }
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    while (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
      perror("bind for switching port");
      sleep(1);
    }
    while (getsockname(sockfd, (struct sockaddr *)&addr, &addr_len) != 0) {
      perror("ntp get sock name error");
      sleep(1);
    }
    port = ntohs(addr.sin_port);
    close(sockfd);
  } while (port < 1024);
  return (port);
}


x86 assembly (clang, -O0):
<get_random_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movw   $0x0,-0x12(%rbp)
movl   $0xc,-0x18(%rbp)
jmp    11b1 <get_random_port+0x21>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11f1 <get_random_port+0x61>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    11b1 <get_random_port+0x21>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1250 <get_random_port+0xc0>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    121a <get_random_port+0x8a>
jmp    1255 <get_random_port+0xc5>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1090 <getsockname@plt>
cmp    $0x0,%rax
je     128a <get_random_port+0xfa>
lea    0xdbc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1255 <get_random_port+0xc5>
mov    -0x10(%rbp),%edi
call   1060 <ntohs@plt>
mov    %ax,-0x12(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
movzwl -0x12(%rbp),%eax
cmp    $0x400,%eax
jl     11ac <get_random_port+0x1c>
movzwl -0x12(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_random_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0xc,0x14(%rsp)
xor    %r14d,%r14d
mov    0x2e38(%rip),%rbx
mov    0x2e21(%rip),%r13
mov    0x2e22(%rip),%r12
lea    0x8(%rsp),%r15
jmp    11f2 <get_random_port+0x62>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1050 <close@plt>
movzwl %r14w,%eax
cmp    $0x400,%eax
jae    12c8 <get_random_port+0x138>
mov    (%rbx),%edi
mov    0x0(%r13),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1242 <get_random_port+0xb2>
mov    %r12,%r15
mov    %r13,%r12
lea    0xdee(%rip),%r13
mov    %r13,%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    (%rbx),%edi
mov    (%r12),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1212 <get_random_port+0x82>
mov    %eax,%ebp
mov    %r12,%r13
mov    %r15,%r12
lea    0x8(%rsp),%r15
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
movzwl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r12),%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1290 <get_random_port+0x100>
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1262 <get_random_port+0xd2>
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r15,%rsi
lea    0x14(%rsp),%r14
mov    %r14,%rdx
call   1090 <getsockname@plt>
test   %rax,%rax
je     11d0 <get_random_port+0x40>
lea    0xd80(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
jmp    129a <get_random_port+0x10a>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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
@.str = [26 x i8] c"socket for switching port\00"
@INADDR_ANY = global i32 0
@.str.1 = [24 x i8] c"bind for switching port\00"
@.str.2 = [24 x i8] c"ntp get sock name error\00"
define zeroext i16 @get_random_port() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i16
%4 = alloca i32
store i32 0, i32* %1
store i16 0, i16* %3
store i32 12, i32* %4
br label %5
5: ; preds = %47, %0
br label %6
6: ; preds = %11, %5
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %1
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @sleep(i32 1)
br label %6
14: ; preds = %6
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i16, i16* %3
%18 = call i32 @htons(i16 zeroext %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
br label %24
24: ; preds = %29, %14
%25 = load i32, i32* %1
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i64 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @sleep(i32 1)
br label %24
32: ; preds = %24
br label %33
33: ; preds = %38, %32
%34 = load i32, i32* %1
%35 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%36 = call i64 @getsockname(i32 %34, %struct.sockaddr* %35, i32* %4)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @sleep(i32 1)
br label %33
41: ; preds = %33
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call zeroext i16 @ntohs(i32 %43)
store i16 %44, i16* %3
%45 = load i32, i32* %1
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %41
%48 = load i16, i16* %3
%49 = zext i16 %48 to i32
%50 = icmp slt i32 %49, 1024
br i1 %50, label %5, label %51
51: ; preds = %47
%52 = load i16, i16* %3
ret i16 %52
}

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
@.str = [26 x i8] c"socket for switching port\00"
@INADDR_ANY = global i32 0
@.str.1 = [24 x i8] c"bind for switching port\00"
@.str.2 = [24 x i8] c"ntp get sock name error\00"
define zeroext i16 @get_random_port() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 12, i32* %2
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
%8 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %9
9: ; preds = %43, %0
%10 = phi i16 [ 0, %0 ], [ %45, %43 ]
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %9, %15
%16 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @sleep(i32 1)
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %15, label %22
22: ; preds = %15, %9
%23 = phi i32 [ %13, %9 ], [ %20, %15 ]
%24 = load i32, i32* @AF_INET
store i32 %24, i32* %5
%25 = call i32 @htons(i16 zeroext %10)
store i32 %25, i32* %6
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
store i32 %27, i32* %7
%28 = call i64 @bind(i32 %23, %struct.sockaddr* %8, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %33, %22
%31 = call i64 @getsockname(i32 %23, %struct.sockaddr* %8, i32* %2)
%32 = icmp eq i64 %31, 0
br i1 %32, label %43, label %38
33: ; preds = %22, %33
%34 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @sleep(i32 1)
%36 = call i64 @bind(i32 %23, %struct.sockaddr* %8, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %33, label %30
38: ; preds = %30, %38
%39 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @sleep(i32 1)
%41 = call i64 @getsockname(i32 %23, %struct.sockaddr* %8, i32* %2)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %38
43: ; preds = %38, %30
%44 = load i32, i32* %6
%45 = call zeroext i16 @ntohs(i32 %44)
%46 = call i32 @close(i32 %23)
%47 = icmp ult i16 %45, 1024
br i1 %47, label %9, label %48
48: ; preds = %43
ret i16 %45
}