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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ GetPort();
int /*<<< orphan*/ GuiSetStat(char *, int /*<<< orphan*/);
int /*<<< orphan*/ INADDR_ANY;
int SOCK_NONBLOCK;
int SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int NetSock_GenMaster() {
  struct sockaddr_in server;
  int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  if (fd != -1) {
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(GetPort());
    if (bind(fd, (struct sockaddr *)&server, sizeof(server)) >= 0) {
      listen(fd, 100);
    } else {
      fd = -2;
    }
  }
  if (fd == -1) {
    GuiSetStat("Master Socket: Failed to generate socket", 0);
  } else if (fd == -2) {
    GuiSetStat("Master Socket: Failed to bind", 0);
  } else {
    GuiSetStat("Master Socket: Generated", 0);
  }
  return fd;
}


x86 assembly (clang, -O0):
<NetSock_GenMaster>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11f9 <NetSock_GenMaster+0x99>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <GetPort@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jl     11ed <NetSock_GenMaster+0x8d>
mov    -0x14(%rbp),%edi
mov    $0x64,%esi
call   1050 <listen@plt>
jmp    11f4 <NetSock_GenMaster+0x94>
movl   $0xfffffffe,-0x14(%rbp)
jmp    11f9 <NetSock_GenMaster+0x99>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1216 <NetSock_GenMaster+0xb6>
lea    0xdf6(%rip),%rdi
xor    %esi,%esi
call   1070 <GuiSetStat@plt>
jmp    1246 <NetSock_GenMaster+0xe6>
cmpl   $0xfffffffe,-0x14(%rbp)
jne    1233 <NetSock_GenMaster+0xd3>
lea    0xe02(%rip),%rdi
xor    %esi,%esi
call   1070 <GuiSetStat@plt>
jmp    1241 <NetSock_GenMaster+0xe1>
lea    0xe0d(%rip),%rdi
xor    %esi,%esi
call   1070 <GuiSetStat@plt>
jmp    1246 <NetSock_GenMaster+0xe6>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NetSock_GenMaster>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7a(%rip),%r14
mov    (%r14),%edi
mov    0x2e50(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f3 <NetSock_GenMaster+0x93>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   1030 <GetPort@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11de <NetSock_GenMaster+0x7e>
mov    %ebx,%edi
mov    $0x64,%esi
call   1050 <listen@plt>
cmp    $0xfffffffe,%ebx
jne    1208 <NetSock_GenMaster+0xa8>
lea    0xe44(%rip),%rdi
xor    %esi,%esi
call   1070 <GuiSetStat@plt>
mov    $0xfffffffe,%ebx
jmp    1216 <NetSock_GenMaster+0xb6>
lea    0xe06(%rip),%rdi
xor    %esi,%esi
call   1070 <GuiSetStat@plt>
mov    $0xffffffff,%ebx
jmp    1216 <NetSock_GenMaster+0xb6>
lea    0xe38(%rip),%rdi
xor    %esi,%esi
call   1070 <GuiSetStat@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@INADDR_ANY = global i32 0
@.str = [41 x i8] c"Master Socket: Failed to generate socket\00"
@.str.1 = [30 x i8] c"Master Socket: Failed to bind\00"
@.str.2 = [25 x i8] c"Master Socket: Generated\00"
define i32 @NetSock_GenMaster() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @SOCK_NONBLOCK
%6 = or i32 %4, %5
%7 = call i32 @socket(i32 %3, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %28
10: ; preds = %0
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = call i32 (...) @GetPort()
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %2
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp sge i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %10
%24 = load i32, i32* %2
%25 = call i32 @listen(i32 %24, i32 100)
br label %27
26: ; preds = %10
store i32 -2, i32* %2
br label %27
27: ; preds = %26, %23
br label %28
28: ; preds = %27, %0
%29 = load i32, i32* %2
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @GuiSetStat(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 0)
br label %41
33: ; preds = %28
%34 = load i32, i32* %2
%35 = icmp eq i32 %34, -2
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @GuiSetStat(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %40
38: ; preds = %33
%39 = call i32 @GuiSetStat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %40
40: ; preds = %38, %36
br label %41
41: ; preds = %40, %31
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@INADDR_ANY = global i32 0
@.str = [41 x i8] c"Master Socket: Failed to generate socket\00"
@.str.1 = [30 x i8] c"Master Socket: Failed to bind\00"
@.str.2 = [25 x i8] c"Master Socket: Generated\00"
define i32 @NetSock_GenMaster() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @SOCK_NONBLOCK
%6 = or i32 %5, %4
%7 = tail call i32 @socket(i32 %3, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %23, label %9
9: ; preds = %0
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = tail call i32 (...) @GetPort()
%15 = tail call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i64 @bind(i32 %7, %struct.sockaddr* %17, i32 12)
%19 = icmp sgt i64 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %9
%21 = call i32 @listen(i32 %7, i32 100)
%22 = icmp eq i32 %7, -2
br i1 %22, label %25, label %27
23: ; preds = %0
%24 = tail call i32 @GuiSetStat(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 0)
br label %29
25: ; preds = %20, %9
%26 = call i32 @GuiSetStat(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %29
27: ; preds = %20
%28 = call i32 @GuiSetStat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %29
29: ; preds = %25, %27, %23
%30 = phi i32 [ -2, %25 ], [ %7, %27 ], [ -1, %23 ]
ret i32 %30
}