#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;
};

/* Variables and functions */
int EXIT_FAILURE;
scalar_t__ EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(long);
scalar_t__ listen(int, int);
int /*<<< orphan*/ print_err(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_listener(int *socket_invite, struct sockaddr_in *socket_in,
                    long *port) {
  if ((*socket_invite = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    print_err("Unable to create socket");
    return EXIT_FAILURE;
  }
  socket_in->sin_family = PF_INET;
  socket_in->sin_port = htons(*port);
  socket_in->sin_addr.s_addr = INADDR_ANY;
  if (bind(*socket_invite, (struct sockaddr *)socket_in, sizeof(*socket_in)) <
      0) {
    print_err("Unable to bind socket on port nr. '%ld'", *port);
    return EXIT_FAILURE;
  }
  if (listen(*socket_invite, 5) != EXIT_SUCCESS) {
    print_err("Listening on port '%ld' failed", *port);
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<create_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    11ab <create_listener+0x5b>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <print_err@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <create_listener+0x126>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1223 <create_listener+0xd3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <print_err@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <create_listener+0x126>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
je     1269 <create_listener+0x119>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <print_err@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <create_listener+0x126>
mov    0x2d50(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_listener>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x2e68(%rip),%r12
mov    (%r12),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,(%r15)
test   %eax,%eax
js     11da <create_listener+0x8a>
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
mov    (%r14),%rdi
call   1040 <htons@plt>
mov    %eax,0x4(%rbx)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11ea <create_listener+0x9a>
mov    (%r15),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <create_listener+0xb4>
mov    (%r14),%rsi
lea    0xe68(%rip),%rdi
jmp    11f4 <create_listener+0xa4>
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1030 <print_err@plt>
jmp    11fb <create_listener+0xab>
mov    (%r14),%rsi
lea    0xe24(%rip),%rdi
xor    %eax,%eax
call   1030 <print_err@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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"Unable to create socket\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [40 x i8] c"Unable to bind socket on port nr. '%ld'\00"
@EXIT_SUCCESS = global i64 0
@.str.2 = [31 x i8] c"Listening on port '%ld' failed\00"
define i32 @create_listener(i32* %0, %struct.sockaddr_in* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.sockaddr_in*
%7 = alloca i64*
store i32* %0, i32** %5
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %6
store i64* %2, i64** %7
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = load i32*, i32** %5
store i32 %10, i32* %11
%12 = icmp slt i32 %10, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 (i8*, ...) @print_err(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %4
br label %54
16: ; preds = %3
%17 = load i32, i32* @PF_INET
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 2
store i32 %17, i32* %19
%20 = load i64*, i64** %7
%21 = load i64, i64* %20
%22 = call i32 @htons(i64 %21)
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 1
store i32 %22, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %25, i32* %28
%29 = load i32*, i32** %5
%30 = load i32, i32* %29
%31 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%32 = bitcast %struct.sockaddr_in* %31 to %struct.sockaddr*
%33 = call i64 @bind(i32 %30, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %16
%36 = load i64*, i64** %7
%37 = load i64, i64* %36
%38 = call i32 (i8*, ...) @print_err(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i64 %37)
%39 = load i32, i32* @EXIT_FAILURE
store i32 %39, i32* %4
br label %54
40: ; preds = %16
%41 = load i32*, i32** %5
%42 = load i32, i32* %41
%43 = call i64 @listen(i32 %42, i32 5)
%44 = load i64, i64* @EXIT_SUCCESS
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i64*, i64** %7
%48 = load i64, i64* %47
%49 = call i32 (i8*, ...) @print_err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %48)
%50 = load i32, i32* @EXIT_FAILURE
store i32 %50, i32* %4
br label %54
51: ; preds = %40
%52 = load i64, i64* @EXIT_SUCCESS
%53 = trunc i64 %52 to i32
store i32 %53, i32* %4
br label %54
54: ; preds = %51, %46, %35, %13
%55 = load i32, i32* %4
ret i32 %55
}

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"Unable to create socket\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [40 x i8] c"Unable to bind socket on port nr. '%ld'\00"
@EXIT_SUCCESS = global i64 0
@.str.2 = [31 x i8] c"Listening on port '%ld' failed\00"
define i32 @create_listener(i32* nocapture %0, %struct.sockaddr_in* %1, i64* nocapture %2) {
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %0
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 (i8*, ...) @print_err(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
br label %38
11: ; preds = %3
%12 = load i32, i32* @PF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i64, i64* %2
%15 = tail call i32 @htons(i64 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %0
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = tail call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %11
%24 = load i64, i64* %2
%25 = tail call i32 (i8*, ...) @print_err(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i64 %24)
%26 = load i32, i32* @EXIT_FAILURE
br label %38
27: ; preds = %11
%28 = load i32, i32* %0
%29 = tail call i64 @listen(i32 %28, i32 5)
%30 = load i64, i64* @EXIT_SUCCESS
%31 = icmp eq i64 %29, %30
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = load i64, i64* %2
%34 = tail call i32 (i8*, ...) @print_err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %33)
%35 = load i32, i32* @EXIT_FAILURE
br label %38
36: ; preds = %27
%37 = trunc i64 %29 to i32
br label %38
38: ; preds = %36, %32, %23, %8
%39 = phi i32 [ %10, %8 ], [ %26, %23 ], [ %35, %32 ], [ %37, %36 ]
ret i32 %39
}