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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ failed(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void udp_listen(int port) {
  struct sockaddr_in addr_server, addr_client;
  int sock;
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    failed("socket()");
  }
  memset((char *)&addr_server, 0, sizeof(addr_server));
  addr_server.sin_family = AF_INET;
  addr_server.sin_port = htons(port);
  addr_server.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&addr_server, sizeof(addr_server)) == -1) {
    failed("bind()");
  }
  char buf[512];
  socklen_t clen = sizeof(addr_client);
  if (recvfrom(sock, buf, 512, 0, (struct sockaddr *)&addr_client, &clen) ==
      -1) {
    failed("recvfrom()");
  }
}


x86 assembly (clang, -O0):
<udp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11b6 <udp_listen+0x46>
lea    0xe4f(%rip),%rdi
call   1060 <failed@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1214 <udp_listen+0xa4>
lea    0xdfa(%rip),%rdi
call   1060 <failed@plt>
movl   $0xc,-0x234(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x234(%rbp),%r9
call   1070 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    1254 <udp_listen+0xe4>
lea    0xdc1(%rip),%rdi
call   1060 <failed@plt>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x238,%rsp
mov    %edi,%ebp
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11b3 <udp_listen+0x43>
lea    0xe52(%rip),%rdi
call   1060 <failed@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    120b <udp_listen+0x9b>
lea    0xe03(%rip),%rdi
call   1060 <failed@plt>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%rsi
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1070 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    1241 <udp_listen+0xd1>
lea    0xdd4(%rip),%rdi
call   1060 <failed@plt>
add    $0x238,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [9 x i8] c"socket()\00"
@INADDR_ANY = global i32 0
@.str.1 = [7 x i8] c"bind()\00"
@.str.2 = [11 x i8] c"recvfrom()\00"
define void @udp_listen(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @failed(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
%16 = bitcast %struct.sockaddr_in* %3 to i8*
%17 = call i32 @memset(i8* %16, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %2
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = call i32 @failed(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %15
store i32 12, i32* %7
%34 = load i32, i32* %5
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i32 @recvfrom(i32 %34, i8* %35, i32 512, i32 0, %struct.sockaddr* %36, i32* %7)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @failed(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [9 x i8] c"socket()\00"
@INADDR_ANY = global i32 0
@.str.1 = [7 x i8] c"bind()\00"
@.str.2 = [11 x i8] c"recvfrom()\00"
define void @udp_listen(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = tail call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = tail call i32 @failed(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
%16 = call i32 @memset(i8* %6, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%25 = call i32 @bind(i32 %11, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @failed(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %15
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%31 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @recvfrom(i32 %11, i8* %30, i32 512, i32 0, %struct.sockaddr* %32, i32* %5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @failed(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
ret void
}