#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;
};
struct linger {
  int l_onoff;
  int l_linger;
};
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ option;
typedef int /*<<< orphan*/ li;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_LINGER;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socketServerInitNoneBlock(int portnumber) {
  int servfd;
  struct sockaddr_in servaddr;
  if ((servfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Create socket error!\n");
    return -1;
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(portnumber);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  int option = 1;
  setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, (char *)&option, sizeof(option));
  struct linger li;
  li.l_onoff = 1;
  li.l_linger = 1;
  setsockopt(servfd, SOL_SOCKET, SO_LINGER, (char *)&li, sizeof(li));
  if (bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("bind to port 5000 failure");
    return -1;
  }
  if (listen(servfd, 1) < 0) {
    perror("listen error");
    return -1;
  }
  int flags = fcntl(servfd, F_GETFL, 0);
  fcntl(servfd, F_SETFL, flags | O_NONBLOCK);
  return servfd;
}


x86 assembly (clang, -O0):
<socketServerInitNoneBlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11e8 <socketServerInitNoneBlock+0x48>
lea    0xe29(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <socketServerInitNoneBlock+0x176>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x1,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12ae <socketServerInitNoneBlock+0x10e>
lea    0xd79(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <socketServerInitNoneBlock+0x176>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12dd <socketServerInitNoneBlock+0x13d>
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <socketServerInitNoneBlock+0x176>
mov    -0xc(%rbp),%edi
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2ccf(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketServerInitNoneBlock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebp
mov    0x2e2d(%rip),%r15
mov    (%r15),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12b4 <socketServerInitNoneBlock+0x114>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2dd3(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
movabs $0x100000001,%rax
mov    %rax,0x20(%rsp)
mov    0x0(%rbp),%esi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12d2 <socketServerInitNoneBlock+0x132>
mov    %ebx,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     12db <socketServerInitNoneBlock+0x13b>
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    0x2d1a(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d31(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <fcntl@plt>
jmp    12c5 <socketServerInitNoneBlock+0x125>
lea    0xd6c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd27(%rip),%rdi
jmp    12e2 <socketServerInitNoneBlock+0x142>
lea    0xd38(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c0 <socketServerInitNoneBlock+0x120>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.linger = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Create socket error!\0A\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_LINGER = global i32 0
@.str.1 = [26 x i8] c"bind to port 5000 failure\00"
@.str.2 = [13 x i8] c"listen error\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @socketServerInitNoneBlock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca %struct.linger
%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 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %61
15: ; preds = %1
%16 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %3
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
store i32 1, i32* %6
%26 = load i32, i32* %4
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = bitcast i32* %6 to i8*
%30 = call i32 @setsockopt(i32 %26, i32 %27, i32 %28, i8* %29, i32 4)
%31 = getelementptr inbounds %struct.linger, %struct.linger* %7, i32 0, i32 0
store i32 1, i32* %31
%32 = getelementptr inbounds %struct.linger, %struct.linger* %7, i32 0, i32 1
store i32 1, i32* %32
%33 = load i32, i32* %4
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_LINGER
%36 = bitcast %struct.linger* %7 to i8*
%37 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i8* %36, i32 8)
%38 = load i32, i32* %4
%39 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%40 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %15
%43 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %61
44: ; preds = %15
%45 = load i32, i32* %4
%46 = call i64 @listen(i32 %45, i32 1)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %61
50: ; preds = %44
%51 = load i32, i32* %4
%52 = load i32, i32* @F_GETFL
%53 = call i32 @fcntl(i32 %51, i32 %52, i32 0)
store i32 %53, i32* %8
%54 = load i32, i32* %4
%55 = load i32, i32* @F_SETFL
%56 = load i32, i32* %8
%57 = load i32, i32* @O_NONBLOCK
%58 = or i32 %56, %57
%59 = call i32 @fcntl(i32 %54, i32 %55, i32 %58)
%60 = load i32, i32* %4
store i32 %60, i32* %2
br label %61
61: ; preds = %50, %48, %42, %13
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.linger = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_LINGER = global i32 0
@.str.1 = [26 x i8] c"bind to port 5000 failure\00"
@.str.2 = [13 x i8] c"listen error\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@str = [21 x i8] c"Create socket error!\00"
define i32 @socketServerInitNoneBlock(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca %struct.linger
%5 = bitcast %struct.sockaddr_in* %2 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 %10, label %12
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %50
12: ; preds = %1
%13 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %8, i32 %22, i32 %23, i8* %21, i32 4)
%25 = bitcast %struct.linger* %4 to i8*
%26 = getelementptr inbounds %struct.linger, %struct.linger* %4, i64 0, i32 0
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.linger, %struct.linger* %4, i64 0, i32 1
store i32 1, i32* %27
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_LINGER
%30 = call i32 @setsockopt(i32 %8, i32 %28, i32 %29, i8* %25, i32 8)
%31 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%32 = call i64 @bind(i32 %8, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %12
%35 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %48
36: ; preds = %12
%37 = call i64 @listen(i32 %8, i32 1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %48
41: ; preds = %36
%42 = load i32, i32* @F_GETFL
%43 = call i32 @fcntl(i32 %8, i32 %42, i32 0)
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* @O_NONBLOCK
%46 = or i32 %45, %43
%47 = call i32 @fcntl(i32 %8, i32 %44, i32 %46)
br label %48
48: ; preds = %41, %39, %34
%49 = phi i32 [ -1, %34 ], [ -1, %39 ], [ %8, %41 ]
br label %50
50: ; preds = %48, %10
%51 = phi i32 [ -1, %10 ], [ %49, %48 ]
ret i32 %51
}