#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int InitSocket(int nPort, int nBackLog, int *sockfd,
               struct sockaddr_in *serv_addr) {
  *sockfd = socket(AF_INET, SOCK_STREAM, 0);
  serv_addr->sin_family = AF_INET;
  serv_addr->sin_addr.s_addr = INADDR_ANY;
  serv_addr->sin_port = htons(nPort);
  int nOne = 1;
  if (setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&nOne,
                 sizeof(int)) < 0) {
    error("setsockopt");
  }
  if (bind(*sockfd, (struct sockaddr *)serv_addr, sizeof(struct sockaddr_in)) <
      0) {
    error("bind");
  }
  listen(*sockfd, nBackLog);
  signal(SIGCHLD, SIG_IGN);
  return 1;
}


x86 assembly (clang, -O0):
<InitSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    121b <InitSocket+0xab>
lea    0xdea(%rip),%rdi
call   1080 <error@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1245 <InitSocket+0xd5>
lea    0xdcb(%rip),%rdi
call   1080 <error@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
call   1060 <listen@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InitSocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e51(%rip),%r12
mov    (%r12),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,(%rbx)
mov    (%r12),%eax
mov    %eax,0x8(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x0(%rbp)
movl   $0x1,0xc(%rsp)
mov    (%rbx),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jns    1200 <InitSocket+0x90>
lea    0xe05(%rip),%rdi
call   1080 <error@plt>
mov    (%rbx),%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1220 <InitSocket+0xb0>
lea    0xdf0(%rip),%rdi
call   1080 <error@plt>
mov    (%rbx),%edi
mov    %r14d,%esi
call   1060 <listen@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [11 x i8] c"setsockopt\00"
@.str.1 = [5 x i8] c"bind\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
define i32 @InitSocket(i32 %0, i32 %1, i32* %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in*
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %8
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = load i32*, i32** %7
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
store i32 %14, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %17, i32* %20
%21 = load i32, i32* %5
%22 = call i32 @htons(i32 %21)
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
store i32 %22, i32* %24
store i32 1, i32* %9
%25 = load i32*, i32** %7
%26 = load i32, i32* %25
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = bitcast i32* %9 to i8*
%30 = call i64 @setsockopt(i32 %26, i32 %27, i32 %28, i8* %29, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %4
%33 = call i32 @error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %4
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%38 = bitcast %struct.sockaddr_in* %37 to %struct.sockaddr*
%39 = call i64 @bind(i32 %36, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %34
%44 = load i32*, i32** %7
%45 = load i32, i32* %44
%46 = load i32, i32* %6
%47 = call i32 @listen(i32 %45, i32 %46)
%48 = load i32, i32* @SIGCHLD
%49 = load i32, i32* @SIG_IGN
%50 = call i32 @signal(i32 %48, i32 %49)
ret i32 1
}

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
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [11 x i8] c"setsockopt\00"
@.str.1 = [5 x i8] c"bind\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
define i32 @InitSocket(i32 %0, i32 %1, i32* nocapture %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %13, i32* %14
%15 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%16 = load i32, i32* %2
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i64 @setsockopt(i32 %16, i32 %17, i32 %18, i8* %15, i32 4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = call i32 @error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %4
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = load i32, i32* %2
%32 = call i32 @listen(i32 %31, i32 %1)
%33 = load i32, i32* @SIGCHLD
%34 = load i32, i32* @SIG_IGN
%35 = call i32 @signal(i32 %33, i32 %34)
ret i32 1
}