#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 */
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int sin_family;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
scalar_t__ listen(int, int);
scalar_t__ setnonblock(int);
int socket(int, int, int /*<<< orphan*/);
int create_socket(int domain, int type, uint16_t port) {
  int sock;
  struct sockaddr_in addr;
  sock = socket(domain, type, 0);
  if (sock < 0) {
    return -1;
  }
  addr.sin_family = domain;
  addr.sin_port = port;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    return -1;
  }
  if (listen(sock, 10) < 0) {
    return -1;
  }
  if (setnonblock(sock) < 0) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1187 <create_socket+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <create_socket+0xc2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11cb <create_socket+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <create_socket+0xc2>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    11ee <create_socket+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <create_socket+0xc2>
mov    -0x14(%rbp),%edi
call   1040 <setnonblock@plt>
cmp    $0x0,%rax
jge    120c <create_socket+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <create_socket+0xc2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %edi,%ebp
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11c9 <create_socket+0x79>
mov    %eax,%ebx
mov    %ebp,0x8(%rsp)
mov    %r15d,0x10(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11c9 <create_socket+0x79>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
test   %rax,%rax
js     11c9 <create_socket+0x79>
mov    %ebx,%edi
call   1040 <setnonblock@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
mov    %ebx,%r14d
mov    %r14d,%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 }
@INADDR_ANY = global i32 0
define i32 @create_socket(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %42
16: ; preds = %3
%17 = load i32, i32* %5
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %7
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %8
%26 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%27 = call i64 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %16
store i32 -1, i32* %4
br label %42
30: ; preds = %16
%31 = load i32, i32* %8
%32 = call i64 @listen(i32 %31, i32 10)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %4
br label %42
35: ; preds = %30
%36 = load i32, i32* %8
%37 = call i64 @setnonblock(i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %4
br label %42
40: ; preds = %35
%41 = load i32, i32* %8
store i32 %41, i32* %4
br label %42
42: ; preds = %40, %39, %34, %29, %15
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_ANY = global i32 0
define i32 @create_socket(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = tail call i32 @socket(i32 %0, i32 %1, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %3
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %0, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %2, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = tail call i32 @htonl(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%15 = call i64 @bind(i32 %6, %struct.sockaddr* %14, i32 12)
%16 = icmp slt i64 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %8
%18 = call i64 @listen(i32 %6, i32 10)
%19 = icmp slt i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = call i64 @setnonblock(i32 %6)
%22 = icmp slt i64 %21, 0
%23 = select i1 %22, i32 -1, i32 %6
br label %24
24: ; preds = %20, %17, %8, %3
%25 = phi i32 [ -1, %3 ], [ -1, %8 ], [ -1, %17 ], [ %23, %20 ]
ret i32 %25
}