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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int network_setup(int portnum) {
  int sockid, status;
  struct sockaddr_in addrport;
  sockid = socket(PF_INET, SOCK_STREAM, 0);
  if (sockid == -1) {
    fprintf(stderr, "Error: cannot open a new TCP stream socket.\n");
    return -1;
  }
  addrport.sin_family = AF_INET;
  addrport.sin_port = htons(portnum);
  addrport.sin_addr.s_addr = htonl(INADDR_ANY);
  status = bind(sockid, (struct sockaddr *)&addrport, sizeof(addrport));
  if (status == -1) {
    fprintf(stderr, "Error: cannot bind the socket to port %d\n", portnum);
    close(sockid);
    return -1;
  }
  status = listen(sockid, SOMAXCONN);
  if (status == -1) {
    fprintf(stderr, "Error: cannot start listening to the port.\n");
    close(sockid);
    return -1;
  }
  return sockid;
}


x86 assembly (clang, -O0):
<network_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e36(%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    11c4 <network_setup+0x54>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <network_setup+0x117>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1238 <network_setup+0xc8>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <network_setup+0x117>
mov    -0xc(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1281 <network_setup+0x111>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <network_setup+0x117>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<network_setup>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1206 <network_setup+0x96>
mov    %eax,%ebx
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     121f <network_setup+0xaf>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1244 <network_setup+0xd4>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1238 <network_setup+0xc8>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    123f <network_setup+0xcf>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@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
@stderr = global i32 0
@.str = [45 x i8] c"Error: cannot open a new TCP stream socket.\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [42 x i8] c"Error: cannot bind the socket to port %d\0A\00"
@SOMAXCONN = global i32 0
@.str.2 = [44 x i8] c"Error: cannot start listening to the port.\0A\00"
define i32 @network_setup(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 %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %49
15: ; preds = %1
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* %3
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
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* %6, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %4
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %15
%31 = load i32, i32* @stderr
%32 = load i32, i32* %3
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %49
36: ; preds = %15
%37 = load i32, i32* %4
%38 = load i32, i32* @SOMAXCONN
%39 = call i32 @listen(i32 %37, i32 %38)
store i32 %39, i32* %5
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %4
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %2
br label %49
47: ; preds = %36
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %42, %30, %12
%50 = load i32, i32* %2
ret i32 %50
}

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
@stderr = global i32 0
@.str = [45 x i8] c"Error: cannot open a new TCP stream socket.\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [42 x i8] c"Error: cannot bind the socket to port %d\0A\00"
@SOMAXCONN = global i32 0
@.str.2 = [44 x i8] c"Error: cannot start listening to the port.\0A\00"
define i32 @network_setup(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 %11
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %34
11: ; preds = %1
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %0)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%20 = call i32 @bind(i32 %6, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %11
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%25 = call i32 @close(i32 %6)
br label %34
26: ; preds = %11
%27 = load i32, i32* @SOMAXCONN
%28 = call i32 @listen(i32 %6, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @close(i32 %6)
br label %34
34: ; preds = %26, %30, %22, %8
%35 = phi i32 [ -1, %8 ], [ -1, %22 ], [ -1, %30 ], [ %6, %26 ]
ret i32 %35
}