#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_family;
  int /*<<< orphan*/ sin_port;
  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*/);
int /*<<< orphan*/ htons(int);
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_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(port);
  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 eerror\n");
  }
  fprintf(stdout, "create udp random port %d\n", port);
  return sock;
}


x86 assembly (clang, -O0):
<noly_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
movl   $0xc,-0x1c(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    120d <noly_udp_socket+0x9d>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <getsockname@plt>
cmp    $0x0,%rax
jge    123e <noly_udp_socket+0xce>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<noly_udp_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
movl   $0xc,0xc(%rsp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1204 <noly_udp_socket+0x94>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%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    1231 <noly_udp_socket+0xc1>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, 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 = [24 x i8] c"get socket name eerror\0A\00"
@stdout = global i32 0
@.str.2 = [27 x i8] c"create udp random port %d\0A\00"
define i32 @noly_udp_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
store i32 12, i32* %5
%7 = load i32, i32* @INADDR_ANY
%8 = call i32 @htonl(i32 %7)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* %2
%12 = call i32 @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
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 ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %33, %28
%37 = load i32, i32* @stdout
%38 = load i32, i32* %2
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, 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 = [24 x i8] c"get socket name eerror\0A\00"
@stdout = global i32 0
@.str.2 = [27 x i8] c"create udp random port %d\0A\00"
define i32 @noly_udp_socket(i32 %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 12)
%6 = bitcast i32* %3 to i8*
store i32 12, 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 i32 @htons(i32 %0)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
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 12)
%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 ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26, %23
%30 = load i32, i32* @stdout
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %0)
ret i32 %16
}