#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*/ ra;
typedef int /*<<< orphan*/ local;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FIONBIO;
int /*<<< orphan*/ F_GETFL;
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*/ close(int);
int fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
scalar_t__ listen(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sm_listen(int port) {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    return -1;
  }
  int opts = fcntl(fd, F_GETFL);
  struct sockaddr_in local;
  local.sin_family = AF_INET;
  local.sin_addr.s_addr = INADDR_ANY;
  local.sin_port = htons(port);
  int ra = 1;
  int nb = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&ra, sizeof(ra)) < 0 ||
      opts < 0 || ioctl(fd, FIONBIO, (char *)&nb) < 0 ||
      bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0 || listen(fd, 5)) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<sm_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11bd <sm_listen+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <sm_listen+0x125>
mov    -0xc(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1040 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jl     128b <sm_listen+0x10b>
cmpl   $0x0,-0x10(%rbp)
jl     128b <sm_listen+0x10b>
mov    -0xc(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jl     128b <sm_listen+0x10b>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jl     128b <sm_listen+0x10b>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     129f <sm_listen+0x11f>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <sm_listen+0x125>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sm_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e4d(%rip),%r15
mov    (%r15),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1265 <sm_listen+0xe5>
mov    %eax,%ebx
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <fcntl@plt>
mov    %eax,%r14d
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
movl   $0x1,0x4(%rsp)
movl   $0x1,(%rsp)
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     125e <sm_listen+0xde>
test   %r14d,%r14d
js     125e <sm_listen+0xde>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     125e <sm_listen+0xde>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     125e <sm_listen+0xde>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     126a <sm_listen+0xea>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
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
@F_GETFL = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@FIONBIO = global i32 0
define i32 @sm_listen(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %56
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i32, i32* @F_GETFL
%18 = call i32 @fcntl(i32 %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %25, i32* %26
store i32 1, i32* %7
store i32 1, i32* %8
%27 = load i32, i32* %4
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = bitcast i32* %7 to i8*
%31 = call i64 @setsockopt(i32 %27, i32 %28, i32 %29, i8* %30, i32 4)
%32 = icmp slt i64 %31, 0
br i1 %32, label %51, label %33
33: ; preds = %15
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 0
br i1 %35, label %51, label %36
36: ; preds = %33
%37 = load i32, i32* %4
%38 = load i32, i32* @FIONBIO
%39 = bitcast i32* %8 to i8*
%40 = call i64 @ioctl(i32 %37, i32 %38, i8* %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %51, label %42
42: ; preds = %36
%43 = load i32, i32* %4
%44 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%45 = call i64 @bind(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %42
%48 = load i32, i32* %4
%49 = call i64 @listen(i32 %48, i32 5)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47, %42, %36, %33, %15
%52 = load i32, i32* %4
%53 = call i32 @close(i32 %52)
store i32 -1, i32* %2
br label %56
54: ; preds = %47
%55 = load i32, i32* %4
store i32 %55, i32* %2
br label %56
56: ; preds = %54, %51, %14
%57 = load i32, i32* %2
ret i32 %57
}

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