#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*/ serveraddr;

/* 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, struct sockaddr *, 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) {
  if (port <= 0) {
    port = 3000;
  }
  int listenfd, 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, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
    return -1;
  if (listen(listenfd, 1024) < 0)
    return -1;
  return listenfd;
}


x86 assembly (clang, -O0):
<open_listenfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     118c <open_listenfd+0x1c>
movl   $0xbb8,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c4 <open_listenfd+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <open_listenfd+0x117>
mov    -0xc(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11fe <open_listenfd+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <open_listenfd+0x117>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2db9(%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),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    125e <open_listenfd+0xee>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <open_listenfd+0x117>
mov    -0xc(%rbp),%edi
mov    $0x400,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1281 <open_listenfd+0x111>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <open_listenfd+0x117>
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     124b <open_listenfd+0xdb>
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     124b <open_listenfd+0xdb>
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>
test   %r14d,%r14d
mov    %eax,0x14(%rsp)
mov    $0xbb8,%eax
cmovg  %r14d,%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     124b <open_listenfd+0xdb>
mov    %ebx,%edi
mov    $0x400,%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 }
%struct.sockaddr = 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
%7 = load i32, i32* %3
%8 = icmp sle i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 3000, i32* %3
br label %10
10: ; preds = %9, %1
store i32 1, i32* %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10
store i32 -1, i32* %2
br label %49
16: ; preds = %10
%17 = load i32, i32* %4
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = bitcast i32* %5 to i8*
%21 = call i64 @setsockopt(i32 %17, i32 %18, i32 %19, i8* %20, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %2
br label %49
24: ; preds = %16
%25 = bitcast %struct.sockaddr_in* %6 to i8*
%26 = call i32 @bzero(i8* %25, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = call i32 @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load i32, i32* %3
%34 = trunc i32 %33 to i16
%35 = call i32 @htons(i16 zeroext %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %4
%38 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %24
store i32 -1, i32* %2
br label %49
42: ; preds = %24
%43 = load i32, i32* %4
%44 = call i64 @listen(i32 %43, i32 1024)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %2
br label %49
47: ; preds = %42
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %46, %41, %23, %15
%50 = load i32, i32* %2
ret i32 %50
}

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