#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_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ util_dump_addr(char *, struct sockaddr_in *);
int open_udp_socket(int port) {
  int socket_fd, res, err;
  struct sockaddr_in addr;
  socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (socket_fd == -1) {
    perror("socket: socket_fd");
  }
  memset((void *)&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  util_dump_addr("server", &addr);
  res = bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr));
  if (res == -1) {
    perror("bind:");
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<open_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119d <open_udp_socket+0x3d>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
lea    0xe47(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <util_dump_addr@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fe <open_udp_socket+0x9e>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_udp_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e77(%rip),%r15
mov    (%r15),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11df <open_udp_socket+0x7f>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
lea    0xe5c(%rip),%rdi
mov    %rbp,%rsi
call   1050 <util_dump_addr@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11ed <open_udp_socket+0x8d>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
jmp    118b <open_udp_socket+0x2b>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
jmp    11d2 <open_udp_socket+0x72>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"socket: socket_fd\00"
@.str.1 = [7 x i8] c"server\00"
@.str.2 = [6 x i8] c"bind:\00"
define i32 @open_udp_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %2
%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* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = bitcast %struct.sockaddr_in* %6 to i8*
%16 = call i32 @memset(i8* %15, i32 0, i32 8)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* %2
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %20, i32* %21
%22 = call i32 @util_dump_addr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.sockaddr_in* %6)
%23 = load i32, i32* %3
%24 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 8)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %14
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"socket: socket_fd\00"
@.str.1 = [7 x i8] c"server\00"
@.str.2 = [6 x i8] c"bind:\00"
define i32 @open_udp_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = call i32 @memset(i8* %3, i32 0, i32 8)
%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 = call i32 @htons(i32 %0)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %14, i32* %15
%16 = call i32 @util_dump_addr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.sockaddr_in* %2)
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i32 @bind(i32 %6, %struct.sockaddr* %17, i32 8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %10
ret i32 %6
}