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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
scalar_t__ htons(int);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int launch_gameserver(int port) {
  int ret = 0;
  int gamesocket = 0;
  struct sockaddr_in address;
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  if (port != 0)
    address.sin_port = htons(port);
  else
    address.sin_port = 0;
  gamesocket = socket(AF_INET, SOCK_STREAM, 0);
  if (gamesocket < 0)
    return -1;
  ret = bind(gamesocket, (struct sockaddr *)&address, sizeof(address));
  if (ret < 0)
    return -2;
  ret = listen(gamesocket, 5);
  if (ret < 0)
    return -3;
  return gamesocket;
}


x86 assembly (clang, -O0):
<launch_gameserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     118c <launch_gameserver+0x4c>
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
jmp    1194 <launch_gameserver+0x54>
movq   $0x0,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c6 <launch_gameserver+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <launch_gameserver+0xdc>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1050 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11f0 <launch_gameserver+0xb0>
movl   $0xfffffffe,-0x4(%rbp)
jmp    121c <launch_gameserver+0xdc>
mov    -0x10(%rbp),%edi
mov    $0x5,%esi
call   1040 <listen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1216 <launch_gameserver+0xd6>
movl   $0xfffffffd,-0x4(%rbp)
jmp    121c <launch_gameserver+0xdc>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launch_gameserver>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%eax
mov    0x2e9a(%rip),%rbx
mov    (%rbx),%edi
mov    %edi,0xc(%rsp)
mov    0x2e85(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rsp)
test   %eax,%eax
je     1170 <launch_gameserver+0x30>
mov    %eax,%edi
call   1030 <htons@plt>
mov    (%rbx),%edi
jmp    1172 <launch_gameserver+0x32>
xor    %eax,%eax
mov    %rax,(%rsp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11b7 <launch_gameserver+0x77>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1050 <bind@plt>
test   %eax,%eax
js     11be <launch_gameserver+0x7e>
mov    %ebx,%edi
mov    $0x5,%esi
call   1040 <listen@plt>
test   %eax,%eax
mov    $0xfffffffd,%eax
cmovs  %eax,%ebx
jmp    11c3 <launch_gameserver+0x83>
mov    $0xffffffff,%ebx
jmp    11c3 <launch_gameserver+0x83>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @launch_gameserver(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* %3
%16 = call i64 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i64 %16, i64* %17
br label %20
18: ; preds = %1
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i64 0, i64* %19
br label %20
20: ; preds = %18, %14
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %2
br label %42
27: ; preds = %20
%28 = load i32, i32* %5
%29 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 16)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 -2, i32* %2
br label %42
34: ; preds = %27
%35 = load i32, i32* %5
%36 = call i32 @listen(i32 %35, i32 5)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 -3, i32* %2
br label %42
40: ; preds = %34
%41 = load i32, i32* %5
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %39, %33, %26
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @launch_gameserver(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %4, i32* %5
%6 = load i32, i32* @INADDR_ANY
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %6, i32* %7
%8 = icmp eq i32 %0, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = tail call i64 @htons(i32 %0)
%11 = load i32, i32* @AF_INET
br label %12
12: ; preds = %1, %9
%13 = phi i32 [ %11, %9 ], [ %4, %1 ]
%14 = phi i64 [ %10, %9 ], [ 0, %1 ]
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i64 %14, i64* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %13, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %12
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %17, %struct.sockaddr* %20, i32 16)
%22 = icmp slt i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = call i32 @listen(i32 %17, i32 5)
%25 = icmp slt i32 %24, 0
%26 = select i1 %25, i32 -3, i32 %17
br label %27
27: ; preds = %23, %19, %12
%28 = phi i32 [ -1, %12 ], [ -2, %19 ], [ %26, %23 ]
ret i32 %28
}