#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 /*<<< orphan*/ n;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int socket_inaddr_any_server(int port, int type) {
  struct sockaddr_in addr;
  size_t alen;
  int s, n;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  s = socket(AF_INET, type, 0);
  if (s < 0)
    return -1;
  n = 1;
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(s);
    return -1;
  }
  if (type == SOCK_STREAM) {
    int ret;
    ret = listen(s, 4);
    if (ret < 0) {
      close(s);
      return -1;
    }
  }
  return s;
}


x86 assembly (clang, -O0):
<socket_inaddr_any_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f2 <socket_inaddr_any_server+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <socket_inaddr_any_server+0x117>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    124c <socket_inaddr_any_server+0xcc>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <socket_inaddr_any_server+0x117>
mov    -0xc(%rbp),%eax
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%eax
jne    1291 <socket_inaddr_any_server+0x111>
mov    -0x24(%rbp),%edi
mov    $0x4,%esi
call   1080 <listen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    128c <socket_inaddr_any_server+0x10c>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <socket_inaddr_any_server+0x117>
jmp    1291 <socket_inaddr_any_server+0x111>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_inaddr_any_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e3b(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    0x0(%rbp),%edi
mov    %r14d,%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     124a <socket_inaddr_any_server+0xca>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1243 <socket_inaddr_any_server+0xc3>
mov    0x2d9e(%rip),%rax
cmp    %r14d,(%rax)
jne    123f <socket_inaddr_any_server+0xbf>
mov    %ebp,%edi
mov    $0x4,%esi
call   1080 <listen@plt>
test   %eax,%eax
js     1243 <socket_inaddr_any_server+0xc3>
mov    %ebp,%ebx
jmp    124a <socket_inaddr_any_server+0xca>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
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
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOCK_STREAM = global i32 0
define i32 @socket_inaddr_any_server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* %4
%15 = call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* %5
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %2
store i32 -1, i32* %3
br label %54
27: ; preds = %2
store i32 1, i32* %9
%28 = load i32, i32* %8
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %9, i32 4)
%32 = load i32, i32* %8
%33 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %3
br label %54
39: ; preds = %27
%40 = load i32, i32* %5
%41 = load i32, i32* @SOCK_STREAM
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %52
43: ; preds = %39
%44 = load i32, i32* %8
%45 = call i32 @listen(i32 %44, i32 4)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %3
br label %54
51: ; preds = %43
br label %52
52: ; preds = %51, %39
%53 = load i32, i32* %8
store i32 %53, i32* %3
br label %54
54: ; preds = %52, %48, %36, %26
%55 = load i32, i32* %3
ret i32 %55
}

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
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOCK_STREAM = global i32 0
define i32 @socket_inaddr_any_server(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @htons(i32 %0)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = call i32 @socket(i32 %15, i32 %1, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %33, label %18
18: ; preds = %2
store i32 1, i32* %4
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %16, i32 %19, i32 %20, i32* %4, i32 4)
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %16, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %18
%26 = load i32, i32* @SOCK_STREAM
%27 = icmp eq i32 %26, %1
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = call i32 @listen(i32 %16, i32 4)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28, %18
%32 = call i32 @close(i32 %16)
br label %33
33: ; preds = %31, %25, %28, %2
%34 = phi i32 [ -1, %2 ], [ %16, %28 ], [ %16, %25 ], [ -1, %31 ]
ret i32 %34
}