#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;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ in_port_t;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ evutil_make_socket_nonblocking(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int net_socket(in_addr_t iaIP, in_port_t ipPort, struct sockaddr_in *srv) {
  int on = 1;
  memset(srv, 0, sizeof(*srv));
  srv->sin_family = AF_INET;
  srv->sin_port = htons(ipPort);
  srv->sin_addr.s_addr = htonl(iaIP);
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock >= 0) {
    evutil_make_socket_nonblocking(sock);
  }
  int setsockopt(int sockfd, int level, int optname, const void *optval,
                 socklen_t optlen);
  return sock;
}


x86 assembly (clang, -O0):
<net_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x4(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     11d8 <net_socket+0x88>
mov    -0x18(%rbp),%edi
call   1060 <evutil_make_socket_nonblocking@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r14d
mov    %rdx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e73(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rbx)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    %r14d,%edi
call   1040 <htonl@plt>
mov    %eax,(%rbx)
mov    (%r15),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11af <net_socket+0x5f>
mov    %ebx,%edi
call   1060 <evutil_make_socket_nonblocking@plt>
mov    %ebx,%eax
add    $0x8,%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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @net_socket(i32 %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %6
store i32 1, i32* %7
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%10 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %11, i32* %13
%14 = load i32, i32* %5
%15 = call i32 @htons(i32 %14)
%16 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load i32, i32* %4
%19 = call i32 @htonl(i32 %18)
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %19, i32* %22
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp sge i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %3
%29 = load i32, i32* %8
%30 = call i32 @evutil_make_socket_nonblocking(i32 %29)
br label %31
31: ; preds = %28, %3
%32 = load i32, i32* %8
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @net_socket(i32 %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = tail call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @htons(i32 %1)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %7, i32* %8
%9 = tail call i32 @htonl(i32 %0)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = tail call i32 @evutil_make_socket_nonblocking(i32 %13)
br label %17
17: ; preds = %15, %3
ret i32 %13
}