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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getpid();
int /*<<< orphan*/ htons(int);
void *inet_addr(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int net_init(int *sock, int port, int flag) {
  int sfd;
  struct sockaddr_in srv_addr;
  if (sock == NULL)
    return -1;
  sfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&srv_addr, 0, sizeof(srv_addr));
  srv_addr.sin_family = AF_INET;
  srv_addr.sin_port = htons(port);
  if (flag == 1) {
    srv_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
  } else {
    srv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  }
  if (bind(sfd, (const struct sockaddr *)&srv_addr, sizeof srv_addr) == 0) {
    printf("Binding socket is OK\n");
  } else {
    fprintf(stderr, "Fail to bind the socket: %s\n", strerror(errno));
    return -1;
  }
  if (listen(sfd, 20) == 0) {
    printf("[%d] Start listening\n", getpid());
  } else {
    fprintf(stderr, "Fail to start listening: %s\n", strerror(errno));
    return -1;
  }
  *sock = sfd;
  return 0;
}


x86 assembly (clang, -O0):
<net_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c9 <net_init+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <net_init+0x17c>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    122b <net_init+0x8b>
lea    0xde3(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x30(%rbp)
jmp    123b <net_init+0x9b>
lea    0xdd6(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jne    1269 <net_init+0xc9>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a1 <net_init+0x101>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xd98(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <net_init+0x17c>
mov    -0x1c(%rbp),%edi
mov    $0x14,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jne    12d4 <net_init+0x134>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    130c <net_init+0x16c>
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd60(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <net_init+0x17c>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     12b4 <net_init+0x104>
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r12
mov    0x2e0d(%rip),%r14
mov    (%r14),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
cmp    $0x1,%r15d
lea    0xddf(%rip),%rax
lea    0xde0(%rip),%rdi
cmove  %rax,%rdi
call   1080 <inet_addr@plt>
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %rax,%rax
je     126a <net_init+0xba>
mov    0x2d87(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdaa(%rip),%rsi
jmp    12a5 <net_init+0xf5>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x14,%esi
call   10a0 <listen@plt>
test   %rax,%rax
je     12c5 <net_init+0x115>
mov    0x2d4a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xda0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd58(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,(%r12)
jmp    12b4 <net_init+0x104>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [22 x i8] c"Binding socket is OK\0A\00"
@stderr = global i32 0
@.str.3 = [29 x i8] c"Fail to bind the socket: %s\0A\00"
@errno = global i32 0
@.str.4 = [22 x i8] c"[%d] Start listening\0A\00"
@.str.5 = [29 x i8] c"Fail to start listening: %s\0A\00"
define i32 @net_init(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 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %60
13: ; preds = %3
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %8
%17 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %6
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %13
%26 = call i8* @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i8* %26, i8** %28
br label %33
29: ; preds = %13
%30 = call i8* @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i8* %30, i8** %32
br label %33
33: ; preds = %29, %25
%34 = load i32, i32* %8
%35 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%36 = call i64 @bind(i32 %34, %struct.sockaddr* %35, i32 16)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %45
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %43)
store i32 -1, i32* %4
br label %60
45: ; preds = %38
%46 = load i32, i32* %8
%47 = call i64 @listen(i32 %46, i32 20)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = call i32 (...) @getpid()
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %50)
br label %57
52: ; preds = %45
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %55)
store i32 -1, i32* %4
br label %60
57: ; preds = %49
%58 = load i32, i32* %8
%59 = load i32*, i32** %5
store i32 %58, i32* %59
store i32 0, i32* %4
br label %60
60: ; preds = %57, %52, %40, %12
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.3 = [29 x i8] c"Fail to bind the socket: %s\0A\00"
@errno = global i32 0
@.str.4 = [22 x i8] c"[%d] Start listening\0A\00"
@.str.5 = [29 x i8] c"Fail to start listening: %s\0A\00"
@str = [21 x i8] c"Binding socket is OK\00"
define i32 @net_init(i32* writeonly %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = icmp eq i32* %0, null
br i1 %6, label %40, label %7
7: ; preds = %3
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 16)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %14, i32* %15
%16 = icmp eq i32 %2, 1
%17 = select i1 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0)
%18 = call i8* @inet_addr(i8* %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i8* %18, i8** %19
%20 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%21 = call i64 @bind(i32 %10, %struct.sockaddr* %20, i32 16)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %7
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%25 = call i64 @listen(i32 %10, i32 20)
%26 = icmp eq i64 %25, 0
br i1 %26, label %32, label %35
27: ; preds = %7
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %40
32: ; preds = %23
%33 = call i32 (...) @getpid()
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %33)
store i32 %10, i32* %0
br label %40
35: ; preds = %23
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %3, %32, %35, %27
%41 = phi i32 [ 0, %32 ], [ -1, %35 ], [ -1, %27 ], [ -1, %3 ]
ret i32 %41
}