#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_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ flags;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ debug_perror(char *);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int dns_prepare_socket(int port) {
  struct sockaddr_in sa;
  int fd = -1;
  int flags;
  memset((void *)&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = INADDR_ANY;
  sa.sin_port = htons(port);
  fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (fd < 0)
    return fd;
  fcntl(fd, F_SETFD, FD_CLOEXEC);
  flags = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&flags, sizeof(flags)) <
      0) {
    printf("Could not set reuse option\n");
    debug_perror("setsockopt");
    goto err;
  }
  flags = fcntl(fd, F_GETFL);
  if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
    printf("Could not set socket nonblock\n");
    debug_perror("fcntl");
    goto err;
  }
  if (bind(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0) {
    printf("Could not bind socket to port %d\n", port);
    debug_perror("bind");
    goto err;
  }
  return fd;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<dns_prepare_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    120d <dns_prepare_socket+0x7d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133b <dns_prepare_socket+0x1ab>
mov    -0x1c(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1050 <fcntl@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    127d <dns_prepare_socket+0xed>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xda9(%rip),%rdi
call   1030 <debug_perror@plt>
jmp    132c <dns_prepare_socket+0x19c>
mov    -0x1c(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     12c5 <dns_prepare_socket+0x135>
mov    -0x1c(%rbp),%edi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2d25(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1050 <fcntl@plt>
cmp    $0x0,%eax
jge    12e4 <dns_prepare_socket+0x154>
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xd6c(%rip),%rdi
call   1030 <debug_perror@plt>
jmp    132c <dns_prepare_socket+0x19c>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1321 <dns_prepare_socket+0x191>
mov    -0x8(%rbp),%esi
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xd57(%rip),%rdi
call   1030 <debug_perror@plt>
jmp    132c <dns_prepare_socket+0x19c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133b <dns_prepare_socket+0x1ab>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dns_prepare_socket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2e20(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1070 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12f0 <dns_prepare_socket+0x150>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <fcntl@plt>
movl   $0x1,0x4(%rsp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
test   %rax,%rax
js     12cc <dns_prepare_socket+0x12c>
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <fcntl@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     12b7 <dns_prepare_socket+0x117>
mov    0x2d42(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d61(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fcntl@plt>
test   %eax,%eax
js     12b7 <dns_prepare_socket+0x117>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    12f0 <dns_prepare_socket+0x150>
lea    0xd6c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xd7e(%rip),%rdi
jmp    12df <dns_prepare_socket+0x13f>
lea    0xd7a(%rip),%rdi
call   1040 <puts@plt>
lea    0xd41(%rip),%rdi
jmp    12df <dns_prepare_socket+0x13f>
lea    0xd83(%rip),%rdi
call   1040 <puts@plt>
lea    0xd21(%rip),%rdi
call   1030 <debug_perror@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [28 x i8] c"Could not set reuse option\0A\00"
@.str.1 = [11 x i8] c"setsockopt\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [31 x i8] c"Could not set socket nonblock\0A\00"
@.str.3 = [6 x i8] c"fcntl\00"
@.str.4 = [34 x i8] c"Could not bind socket to port %d\0A\00"
@.str.5 = [5 x i8] c"bind\00"
define i32 @dns_prepare_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %5
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = call i32 @memset(i8* %7, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* %3
%15 = call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = load i32, i32* @IPPROTO_UDP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = load i32, i32* %5
store i32 %24, i32* %2
br label %70
25: ; preds = %1
%26 = load i32, i32* %5
%27 = load i32, i32* @F_SETFD
%28 = load i32, i32* @FD_CLOEXEC
%29 = call i32 (i32, i32, ...) @fcntl(i32 %26, i32 %27, i32 %28)
store i32 1, i32* %6
%30 = load i32, i32* %5
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_REUSEADDR
%33 = bitcast i32* %6 to i8*
%34 = call i64 @setsockopt(i32 %30, i32 %31, i32 %32, i8* %33, i32 4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %25
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%38 = call i32 @debug_perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %67
39: ; preds = %25
%40 = load i32, i32* %5
%41 = load i32, i32* @F_GETFL
%42 = call i32 (i32, i32, ...) @fcntl(i32 %40, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %53, label %45
45: ; preds = %39
%46 = load i32, i32* %5
%47 = load i32, i32* @F_SETFL
%48 = load i32, i32* %6
%49 = load i32, i32* @O_NONBLOCK
%50 = or i32 %48, %49
%51 = call i32 (i32, i32, ...) @fcntl(i32 %46, i32 %47, i32 %50)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %45, %39
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @debug_perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %67
56: ; preds = %45
%57 = load i32, i32* %5
%58 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%59 = call i64 @bind(i32 %57, %struct.sockaddr* %58, i32 4)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %56
%62 = load i32, i32* %3
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = call i32 @debug_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %67
65: ; preds = %56
%66 = load i32, i32* %5
store i32 %66, i32* %2
br label %70
67: ; preds = %61, %53, %36
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %2
br label %70
70: ; preds = %67, %65, %23
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [6 x i8] c"fcntl\00"
@.str.4 = [34 x i8] c"Could not bind socket to port %d\0A\00"
@.str.5 = [5 x i8] c"bind\00"
@str = [30 x i8] c"Could not set socket nonblock\00"
@str.6 = [27 x i8] c"Could not set reuse option\00"
define i32 @dns_prepare_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @memset(i8* %4, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = call i32 @htons(i32 %0)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_UDP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %50, label %18
18: ; preds = %1
%19 = load i32, i32* @F_SETFD
%20 = load i32, i32* @FD_CLOEXEC
%21 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %19, i32 %20)
store i32 1, i32* %3
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i64 @setsockopt(i32 %16, i32 %22, i32 %23, i8* %5, i32 4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
br label %46
28: ; preds = %18
%29 = load i32, i32* @F_GETFL
%30 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %29)
store i32 %30, i32* %3
%31 = icmp slt i32 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %28
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* @O_NONBLOCK
%35 = or i32 %34, %30
%36 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %33, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32, %28
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %46
40: ; preds = %32
%41 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%42 = call i64 @bind(i32 %16, %struct.sockaddr* %41, i32 4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br label %46
46: ; preds = %44, %38, %26
%47 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %44 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %38 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %26 ]
%48 = call i32 @debug_perror(i8* %47)
%49 = call i32 @close(i32 %16)
br label %50
50: ; preds = %40, %1, %46
%51 = phi i32 [ -1, %46 ], [ %16, %1 ], [ %16, %40 ]
ret i32 %51
}