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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int fireup(int port) {
  int new_sock;
  struct sockaddr_in name;
  if ((new_sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    printf("Error creating socket\n");
    return -1;
  }
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(new_sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
    printf("Error binding to socket\n");
    return -1;
  }
  if (listen(new_sock, 5) < 0) {
    printf("Error listening to socket\n");
    return -1;
  }
  return (new_sock);
}


x86 assembly (clang, -O0):
<fireup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11a8 <fireup+0x48>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <fireup+0xd8>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1203 <fireup+0xa3>
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <fireup+0xd8>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1232 <fireup+0xd2>
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <fireup+0xd8>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fireup>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11e2 <fireup+0x82>
mov    %eax,%ebx
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e39(%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>
test   %rax,%rax
js     11eb <fireup+0x8b>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    11fc <fireup+0x9c>
lea    0xe20(%rip),%rdi
jmp    11f2 <fireup+0x92>
lea    0xe49(%rip),%rdi
jmp    11f2 <fireup+0x92>
lea    0xe28(%rip),%rdi
call   1030 <puts@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 = [23 x i8] c"Error creating socket\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [25 x i8] c"Error binding to socket\0A\00"
@.str.2 = [27 x i8] c"Error listening to socket\0A\00"
define i32 @fireup(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
12: ; preds = %1
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* %3
%16 = call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %12
%27 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
28: ; preds = %12
%29 = load i32, i32* %4
%30 = call i64 @listen(i32 %29, i32 5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
34: ; preds = %28
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %32, %26, %10
%37 = load i32, i32* %2
ret i32 %37
}

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
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@str = [26 x i8] c"Error listening to socket\00"
@str.3 = [24 x i8] c"Error binding to socket\00"
@str.4 = [22 x i8] c"Error creating socket\00"
define i32 @fireup(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 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.4, i64 0, i64 0))
br label %28
10: ; preds = %1
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = tail call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %6, %struct.sockaddr* %18, i32 12)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.3, i64 0, i64 0))
br label %28
23: ; preds = %10
%24 = call i64 @listen(i32 %6, i32 5)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %23, %26, %21, %8
%29 = phi i32 [ -1, %8 ], [ -1, %21 ], [ -1, %26 ], [ %6, %23 ]
ret i32 %29
}