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

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ saServer;
typedef int /*<<< orphan*/ saClient;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int OpenSocket(int nPort) {
  int nSocket;
  struct sockaddr_in saServer;
  struct sockaddr_in saClient;
  int one = 1;
  int nSize;
  int nFD;
  int nLen;
  nSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (nSocket < 0) {
    perror("socket");
    exit(1);
  }
  if (setsockopt(nSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)) <
      0) {
    perror("setsockopt");
    exit(2);
  }
  memset(&saServer, 0, sizeof(saServer));
  saServer.sin_family = AF_INET;
  saServer.sin_port = htons(nPort);
  nSize = sizeof(saServer);
  if (bind(nSocket, (struct sockaddr *)&saServer, nSize) < 0) {
    perror("bind");
    exit(3);
  }
  if (listen(nSocket, 512) < 0) {
    perror("listen");
    exit(4);
  }
  nLen = sizeof(saClient);
  nFD = accept(nSocket, (struct sockaddr *)&saClient, &nLen);
  if (nFD < 0) {
    perror("accept");
    exit(5);
  }
  fprintf(stderr, "Incoming accepted on port %d\n", nPort);
  return nFD;
}


x86 assembly (clang, -O0):
<OpenSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f5 <OpenSocket+0x55>
lea    0xe1a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1239 <OpenSocket+0x99>
lea    0xddd(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
movl   $0x8,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1296 <OpenSocket+0xf6>
lea    0xd8b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x200,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12c3 <OpenSocket+0x123>
lea    0xd63(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
movl   $0x8,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12fd <OpenSocket+0x15d>
lea    0xd30(%rip),%rdi
call   1090 <perror@plt>
mov    $0x5,%edi
call   10b0 <exit@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xd15(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
movl   $0x1,0x4(%rsp)
mov    0x2e24(%rip),%r15
mov    (%r15),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12a2 <OpenSocket+0x102>
mov    %eax,%ebp
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     12b8 <OpenSocket+0x118>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     12ce <OpenSocket+0x12e>
mov    %ebp,%edi
mov    $0x200,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     12e4 <OpenSocket+0x144>
movl   $0x8,(%rsp)
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
call   10a0 <accept@plt>
test   %eax,%eax
js     12fa <OpenSocket+0x15a>
mov    %eax,%ebp
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
mov    %r14d,%edx
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd57(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd48(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd3d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
lea    0xd2c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
lea    0xd1d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x5,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.5 = [30 x i8] c"Incoming accepted on port %d\0A\00"
define i32 @OpenSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %6
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = load i32, i32* %3
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = bitcast i32* %6 to i8*
%24 = call i64 @setsockopt(i32 %20, i32 %21, i32 %22, i8* %23, i32 4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %19
%30 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 8)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* %2
%34 = call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %34, i32* %35
store i32 8, i32* %7
%36 = load i32, i32* %3
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = load i32, i32* %7
%39 = call i64 @bind(i32 %36, %struct.sockaddr* %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %29
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 3)
unreachable
44: ; preds = %29
%45 = load i32, i32* %3
%46 = call i64 @listen(i32 %45, i32 512)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 4)
unreachable
51: ; preds = %44
store i32 8, i32* %9
%52 = load i32, i32* %3
%53 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%54 = call i32 @accept(i32 %52, %struct.sockaddr* %53, i32* %9)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 5)
unreachable
60: ; preds = %51
%61 = load i32, i32* @stderr
%62 = load i32, i32* %2
%63 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %8
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.5 = [30 x i8] c"Incoming accepted on port %d\0A\00"
define i32 @OpenSocket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%9 = bitcast i32* %5 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = tail call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i64 @setsockopt(i32 %13, i32 %19, i32 %20, i8* %8, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %18
%27 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %0)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i64 @bind(i32 %13, %struct.sockaddr* %32, i32 8)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 3)
unreachable
38: ; preds = %26
%39 = call i64 @listen(i32 %13, i32 512)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 4)
unreachable
44: ; preds = %38
store i32 8, i32* %5
%45 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%46 = call i32 @accept(i32 %13, %struct.sockaddr* %45, i32* %5)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @exit(i32 5)
unreachable
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %0)
ret i32 %46
}