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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, unsigned int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getsockname(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
void *htons(void *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int noly_udp_rand_socket(int *port) {
  int sock;
  struct sockaddr_in serv;
  memset(&serv, 0, sizeof(struct sockaddr_in));
  unsigned sin_len = sizeof(struct sockaddr_in);
  serv.sin_addr.s_addr = htonl(INADDR_ANY);
  serv.sin_port = htons(0);
  serv.sin_family = AF_INET;
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (bind(sock, (struct sockaddr *)&serv, sin_len) < 0) {
    fprintf(stderr, "bind socket port error\n");
  }
  if (getsockname(sock, (struct sockaddr *)&serv, &sin_len) < 0) {
    fprintf(stderr, "get socket name error\n");
  }
  int sport = htons(serv.sin_port);
  fprintf(stdout, "create udp random port %d\n", sport);
  *port = sport;
  return sock;
}


x86 assembly (clang, -O0):
<noly_udp_rand_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x10,-0x24(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1210 <noly_udp_rand_socket+0xa0>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <getsockname@plt>
cmp    $0x0,%rax
jge    1241 <noly_udp_rand_socket+0xd1>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<noly_udp_rand_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x10,0xc(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x1c(%rsp)
xor    %edi,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x18(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1206 <noly_udp_rand_socket+0x96>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1080 <getsockname@plt>
test   %rax,%rax
jns    1233 <noly_udp_rand_socket+0xc3>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x10(%rsp),%rdi
call   1030 <htons@plt>
mov    %rax,%rbx
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,(%r14)
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"bind socket port error\0A\00"
@.str.1 = [23 x i8] c"get socket name error\0A\00"
@stdout = global i32 0
@.str.2 = [27 x i8] c"create udp random port %d\0A\00"
define i32 @noly_udp_rand_socket(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %2
%7 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 16)
store i32 16, i32* %5
%8 = load i32, i32* @INADDR_ANY
%9 = call i32 @htonl(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = call i8* @htons(i8* null)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i8* %12, i8** %13
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = load i32, i32* @IPPROTO_UDP
%19 = call i32 @socket(i32 %16, i32 %17, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = load i32, i32* %5
%23 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %1
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %25, %1
%29 = load i32, i32* %3
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i64 @getsockname(i32 %29, %struct.sockaddr* %30, i32* %5)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %33, %28
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i8* @htons(i8* %38)
%40 = ptrtoint i8* %39 to i32
store i32 %40, i32* %6
%41 = load i32, i32* @stdout
%42 = load i32, i32* %6
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %6
%45 = load i32*, i32** %2
store i32 %44, i32* %45
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"bind socket port error\0A\00"
@.str.1 = [23 x i8] c"get socket name error\0A\00"
@stdout = global i32 0
@.str.2 = [27 x i8] c"create udp random port %d\0A\00"
define i32 @noly_udp_rand_socket(i32* nocapture writeonly %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 16)
%6 = bitcast i32* %3 to i8*
store i32 16, i32* %3
%7 = load i32, i32* @INADDR_ANY
%8 = call i32 @htonl(i32 %7)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
store i32 %8, i32* %9
%10 = call i8* @htons(i8* null)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i8* %10, i8** %11
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_UDP
%16 = call i32 @socket(i32 %12, i32 %14, i32 %15)
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i64 @bind(i32 %16, %struct.sockaddr* %17, i32 16)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %20, %1
%24 = call i64 @getsockname(i32 %16, %struct.sockaddr* %17, i32* %3)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26, %23
%30 = load i8*, i8** %11
%31 = call i8* @htons(i8* %30)
%32 = ptrtoint i8* %31 to i64
%33 = trunc i64 %32 to i32
%34 = load i32, i32* @stdout
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 %33, i32* %0
ret i32 %16
}