#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 */

/* Type definitions */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_family;
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ PF_INET6;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ in6addr_any;
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_udp_socket(int port) {
  int sock_fd;
  struct sockaddr_in6 addr;
  memset(&addr, 0, sizeof(addr));
  if ((sock_fd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
    die("could not create socket");
  addr.sin6_port = htons(port);
  addr.sin6_family = AF_INET6;
  addr.sin6_addr = in6addr_any;
  if (bind(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    die("could not bind to UDP");
  return sock_fd;
}


x86 assembly (clang, -O0):
<get_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   1040 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    119c <get_udp_socket+0x4c>
lea    0xe69(%rip),%rdi
call   1060 <die@plt>
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11e6 <get_udp_socket+0x96>
lea    0xe37(%rip),%rdi
call   1060 <die@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_udp_socket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1194 <get_udp_socket+0x44>
lea    0xe71(%rip),%rdi
call   1060 <die@plt>
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %rax,%rax
jns    11db <get_udp_socket+0x8b>
lea    0xe42(%rip),%rdi
call   1060 <die@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [24 x i8] c"could not create socket\00"
@AF_INET6 = global i32 0
@in6addr_any = global i32 0
@.str.1 = [22 x i8] c"could not bind to UDP\00"
define i32 @get_udp_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in6
store i32 %0, i32* %2
%5 = call i32 @memset(%struct.sockaddr_in6* %4, i32 0, i32 12)
%6 = load i32, i32* @PF_INET6
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = load i32, i32* %2
%14 = call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET6
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @in6addr_any
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %12
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [24 x i8] c"could not create socket\00"
@AF_INET6 = global i32 0
@in6addr_any = global i32 0
@.str.1 = [22 x i8] c"could not bind to UDP\00"
define i32 @get_udp_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in6
%3 = bitcast %struct.sockaddr_in6* %2 to i8*
%4 = call i32 @memset(%struct.sockaddr_in6* %2, i32 0, i32 12)
%5 = load i32, i32* @PF_INET6
%6 = load i32, i32* @SOCK_DGRAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = call i32 @htons(i32 %0)
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET6
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @in6addr_any
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %7, %struct.sockaddr* %18, i32 12)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %11
ret i32 %7
}