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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ INADDR_ANY;
int O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ printError(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createListenSocket(uint16_t port) {
  int sock, flags;
  struct sockaddr_in name;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    printError("Could not create socket");
    return -1;
  }
  flags = fcntl(sock, F_GETFD);
  if (flags == -1) {
    printError("Could not get flags on socket");
    return -1;
  }
  if (fcntl(sock, F_SETFD, flags | O_NONBLOCK) == -1) {
    printError("Could not set flags on socket");
    return -1;
  }
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&name, sizeof(name)) == -1) {
    printError("Could not bind socket to address");
    return -1;
  }
  if (listen(sock, 8) == -1) {
    printError("Could not make the damn socket listen");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<createListenSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11b9 <createListenSocket+0x49>
lea    0xe58(%rip),%rdi
call   1080 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <createListenSocket+0x14f>
mov    -0xc(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f1 <createListenSocket+0x81>
lea    0xe38(%rip),%rdi
call   1080 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <createListenSocket+0x14f>
mov    -0xc(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    0x2dd9(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1231 <createListenSocket+0xc1>
lea    0xe16(%rip),%rdi
call   1080 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <createListenSocket+0x14f>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    128b <createListenSocket+0x11b>
lea    0xdda(%rip),%rdi
call   1080 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <createListenSocket+0x14f>
mov    -0xc(%rbp),%edi
mov    $0x8,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12b9 <createListenSocket+0x149>
lea    0xdcd(%rip),%rdi
call   1080 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <createListenSocket+0x14f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createListenSocket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1233 <createListenSocket+0xc3>
mov    %eax,%ebx
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     123c <createListenSocket+0xcc>
mov    0x2e0a(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1245 <createListenSocket+0xd5>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     124e <createListenSocket+0xde>
mov    %ebx,%edi
mov    $0x8,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    125f <createListenSocket+0xef>
lea    0xe44(%rip),%rdi
jmp    1255 <createListenSocket+0xe5>
lea    0xdc6(%rip),%rdi
jmp    1255 <createListenSocket+0xe5>
lea    0xdd5(%rip),%rdi
jmp    1255 <createListenSocket+0xe5>
lea    0xdea(%rip),%rdi
jmp    1255 <createListenSocket+0xe5>
lea    0xdff(%rip),%rdi
call   1080 <printError@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@F_GETFD = global i32 0
@.str.1 = [30 x i8] c"Could not get flags on socket\00"
@F_SETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [30 x i8] c"Could not set flags on socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [33 x i8] c"Could not bind socket to address\00"
@.str.4 = [38 x i8] c"Could not make the damn socket listen\00"
define i32 @createListenSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @printError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i32, i32* @F_GETFD
%17 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @printError(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
22: ; preds = %14
%23 = load i32, i32* %4
%24 = load i32, i32* @F_SETFD
%25 = load i32, i32* %5
%26 = load i32, i32* @O_NONBLOCK
%27 = or i32 %25, %26
%28 = call i32 (i32, i32, ...) @fcntl(i32 %23, i32 %24, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @printError(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
32: ; preds = %22
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* %3
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* @INADDR_ANY
%39 = call i32 @htonl(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i32, i32* %4
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = call i32 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %32
%47 = call i32 @printError(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
48: ; preds = %32
%49 = load i32, i32* %4
%50 = call i32 @listen(i32 %49, i32 8)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @printError(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
54: ; preds = %48
%55 = load i32, i32* %4
store i32 %55, i32* %2
br label %56
56: ; preds = %54, %52, %46, %30, %20, %12
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@F_GETFD = global i32 0
@.str.1 = [30 x i8] c"Could not get flags on socket\00"
@F_SETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [30 x i8] c"Could not set flags on socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [33 x i8] c"Could not bind socket to address\00"
@.str.4 = [38 x i8] c"Could not make the damn socket listen\00"
define i32 @createListenSocket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @printError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %42
10: ; preds = %1
%11 = load i32, i32* @F_GETFD
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @printError(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %42
16: ; preds = %10
%17 = load i32, i32* @F_SETFD
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %12
%20 = tail call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %17, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 @printError(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %42
24: ; preds = %16
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %25, i32* %26
%27 = tail call i32 @htons(i32 %0)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = tail call i32 @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i32 @bind(i32 %6, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %24
%36 = call i32 @printError(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %42
37: ; preds = %24
%38 = call i32 @listen(i32 %6, i32 8)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @printError(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %37, %40, %35, %22, %14, %8
%43 = phi i32 [ -1, %8 ], [ -1, %14 ], [ -1, %22 ], [ -1, %35 ], [ -1, %40 ], [ %6, %37 ]
ret i32 %43
}