#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;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ __CONST_SOCKADDR_ARG;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void const *,
                      int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_listenfd(int port) {
  int listenfd;
  int optval = 1;
  struct sockaddr_in serveraddr;
  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return -1;
  if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval,
                 sizeof(int)) < 0)
    return -1;
  bzero((char *)&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serveraddr.sin_port = htons((unsigned short)port);
  if (bind(listenfd, (__CONST_SOCKADDR_ARG)&serveraddr, sizeof(serveraddr)) < 0)
    return -1;
  if (listen(listenfd, 5) < 0)
    return -1;
  return listenfd;
}


x86 assembly (clang, -O0):
<open_listenfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11b3 <open_listenfd+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <open_listenfd+0x108>
mov    -0xc(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11ed <open_listenfd+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <open_listenfd+0x108>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rax
mov    %eax,%esi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    124f <open_listenfd+0xdf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <open_listenfd+0x108>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1272 <open_listenfd+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <open_listenfd+0x108>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_listenfd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
movl   $0x1,0xc(%rsp)
mov    0x2e52(%rip),%r12
mov    (%r12),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1240 <open_listenfd+0xd0>
mov    %eax,%ebx
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1240 <open_listenfd+0xd0>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
movzwl %r14w,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15d,%esi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1240 <open_listenfd+0xd0>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
define i32 @open_listenfd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 1, i32* %5
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %45
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = bitcast i32* %5 to i8*
%17 = call i64 @setsockopt(i32 %13, i32 %14, i32 %15, i8* %16, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %12
store i32 -1, i32* %2
br label %45
20: ; preds = %12
%21 = bitcast %struct.sockaddr_in* %6 to i8*
%22 = call i32 @bzero(i8* %21, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %3
%30 = trunc i32 %29 to i16
%31 = call i32 @htons(i16 zeroext %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %4
%34 = ptrtoint %struct.sockaddr_in* %6 to i32
%35 = call i64 @bind(i32 %33, i32 %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %20
store i32 -1, i32* %2
br label %45
38: ; preds = %20
%39 = load i32, i32* %4
%40 = call i64 @listen(i32 %39, i32 5)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 -1, i32* %2
br label %45
43: ; preds = %38
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %42, %37, %19, %11
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
define i32 @open_listenfd(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %33, label %10
10: ; preds = %1
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = call i64 @setsockopt(i32 %8, i32 %11, i32 %12, i8* %4, i32 4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %10
%16 = call i32 @bzero(i8* %5, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = trunc i32 %0 to i16
%23 = call i32 @htons(i16 zeroext %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = ptrtoint %struct.sockaddr_in* %3 to i64
%26 = trunc i64 %25 to i32
%27 = call i64 @bind(i32 %8, i32 %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %15
%30 = call i64 @listen(i32 %8, i32 5)
%31 = icmp slt i64 %30, 0
%32 = select i1 %31, i32 -1, i32 %8
br label %33
33: ; preds = %29, %15, %10, %1
%34 = phi i32 [ -1, %1 ], [ -1, %10 ], [ -1, %15 ], [ %32, %29 ]
ret i32 %34
}