#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ listen_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_PKTINFO;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int magic_create_sock4(uint16_t listen_port) {
  static const int on = 1;
  struct sockaddr_in listen_addr = {.sin_family = AF_INET,
                                    .sin_port = htons(listen_port),
                                    .sin_addr = INADDR_ANY};
  int fd, ret;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return fd;
  ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  if (ret < 0)
    goto err;
  ret = setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
  if (ret < 0)
    goto err;
  ret = bind(fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr));
  if (ret < 0)
    goto err;
  return fd;
err:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<magic_create_sock4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11af <magic_create_sock4+0x5f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <magic_create_sock4+0x10d>
mov    -0x1c(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
lea    0xe35(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e8 <magic_create_sock4+0x98>
jmp    124f <magic_create_sock4+0xff>
mov    -0x1c(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
lea    0xdfc(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1221 <magic_create_sock4+0xd1>
jmp    124f <magic_create_sock4+0xff>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1244 <magic_create_sock4+0xf4>
jmp    124f <magic_create_sock4+0xff>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <magic_create_sock4+0x10d>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<magic_create_sock4>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1203 <magic_create_sock4+0xb3>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
lea    0xe58(%rip),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     11f8 <magic_create_sock4+0xa8>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
lea    0xe2e(%rip),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     11f8 <magic_create_sock4+0xa8>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
jns    1203 <magic_create_sock4+0xb3>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@magic_create_sock4.on = internal constant i32 1
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_IP = global i32 0
@IP_PKTINFO = global i32 0
define i32 @magic_create_sock4(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%8 = load i32, i32* @INADDR_ANY
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%10 = load i32, i32* %3
%11 = call i32 @htons(i32 %10)
store i32 %11, i32* %9
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%13 = load i32, i32* @AF_INET
store i32 %13, i32* %12
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = load i32, i32* %5
store i32 %20, i32* %2
br label %50
21: ; preds = %1
%22 = load i32, i32* %5
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i32* @magic_create_sock4.on, i32 4)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %21
br label %46
29: ; preds = %21
%30 = load i32, i32* %5
%31 = load i32, i32* @IPPROTO_IP
%32 = load i32, i32* @IP_PKTINFO
%33 = call i32 @setsockopt(i32 %30, i32 %31, i32 %32, i32* @magic_create_sock4.on, i32 4)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %29
br label %46
37: ; preds = %29
%38 = load i32, i32* %5
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = call i32 @bind(i32 %38, %struct.sockaddr* %39, i32 12)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
br label %46
44: ; preds = %37
%45 = load i32, i32* %5
store i32 %45, i32* %2
br label %50
46: ; preds = %43, %36, %28
%47 = load i32, i32* %5
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %6
store i32 %49, i32* %2
br label %50
50: ; preds = %46, %44, %19
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@magic_create_sock4.on = internal constant i32 1
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_IP = global i32 0
@IP_PKTINFO = global i32 0
define i32 @magic_create_sock4(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%5 = load i32, i32* @INADDR_ANY
store i32 %5, i32* %4
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%7 = tail call i32 @htons(i32 %0)
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
%9 = load i32, i32* @AF_INET
store i32 %9, i32* %8
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %1
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = tail call i32 @setsockopt(i32 %11, i32 %14, i32 %15, i32* @magic_create_sock4.on, i32 4)
%17 = icmp slt i32 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %13
%19 = load i32, i32* @IPPROTO_IP
%20 = load i32, i32* @IP_PKTINFO
%21 = tail call i32 @setsockopt(i32 %11, i32 %19, i32 %20, i32* @magic_create_sock4.on, i32 4)
%22 = icmp slt i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%25 = call i32 @bind(i32 %11, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23, %18, %13
%28 = phi i32 [ %16, %13 ], [ %21, %18 ], [ %25, %23 ]
%29 = call i32 @close(i32 %11)
br label %30
30: ; preds = %23, %1, %27
%31 = phi i32 [ %28, %27 ], [ %11, %1 ], [ %11, %23 ]
ret i32 %31
}