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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ ax_web_socket_set_nonblock(int);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ax_web_socket_server(char *ipaddr, int port) {
  int socketfd = socket(AF_INET, SOCK_STREAM, 0);
  if (socketfd <= 0) {
    return -1;
  }
  struct sockaddr_in serveraddr;
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_port = htons(port);
  serveraddr.sin_addr.s_addr = inet_addr(ipaddr);
  if (bind(socketfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) != 0) {
    return -2;
  }
  if (ax_web_socket_set_nonblock(socketfd) != 0) {
    return -3;
  }
  if (listen(socketfd, 1024) != 0) {
    return -4;
  }
  return socketfd;
}


x86 assembly (clang, -O0):
<ax_web_socket_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11a1 <ax_web_socket_server+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <ax_web_socket_server+0xd2>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     11eb <ax_web_socket_server+0x8b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1232 <ax_web_socket_server+0xd2>
mov    -0x18(%rbp),%edi
call   1030 <ax_web_socket_set_nonblock@plt>
cmp    $0x0,%rax
je     1209 <ax_web_socket_server+0xa9>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1232 <ax_web_socket_server+0xd2>
mov    -0x18(%rbp),%edi
mov    $0x400,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     122c <ax_web_socket_server+0xcc>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1232 <ax_web_socket_server+0xd2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ax_web_socket_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e72(%rip),%r15
mov    (%r15),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
jle    11f8 <ax_web_socket_server+0x98>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %rax,%rcx
mov    $0xfffffffe,%eax
test   %rcx,%rcx
jne    11fd <ax_web_socket_server+0x9d>
mov    %ebx,%edi
call   1030 <ax_web_socket_set_nonblock@plt>
mov    %rax,%rcx
mov    $0xfffffffd,%eax
test   %rcx,%rcx
jne    11fd <ax_web_socket_server+0x9d>
mov    %ebx,%edi
mov    $0x400,%esi
call   1060 <listen@plt>
test   %rax,%rax
mov    $0xfffffffc,%eax
cmove  %ebx,%eax
jmp    11fd <ax_web_socket_server+0x9d>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
@SOCK_STREAM = global i32 0
define i32 @ax_web_socket_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %41
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* %5
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i8*, i8** %4
%21 = call i32 @inet_addr(i8* %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %14
store i32 -2, i32* %3
br label %41
29: ; preds = %14
%30 = load i32, i32* %6
%31 = call i64 @ax_web_socket_set_nonblock(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -3, i32* %3
br label %41
34: ; preds = %29
%35 = load i32, i32* %6
%36 = call i64 @listen(i32 %35, i32 1024)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 -4, i32* %3
br label %41
39: ; preds = %34
%40 = load i32, i32* %6
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %38, %33, %28, %13
%42 = load i32, i32* %3
ret i32 %42
}

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