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

/* Variables and functions */
int acceptNewConnections(int);
int atoi(char *);
int createServerSocket(int);
int /*<<< orphan*/ deleteSocket(int);
int /*<<< orphan*/ doServiceFork(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
main(int argc, char *argv[]) {
  int socketFD;
  int connectionFD;
  char buffer[80];
  int ret;
  int port;
  if (argc != 2) {
    strcpy(buffer, "Usage: ServerSocket PortNumber\n");
    write(2, buffer, strlen(buffer));
    exit(1);
  }
  port = atoi(argv[1]);
  socketFD = createServerSocket(port);
  if (socketFD < 0) {
    perror("Error creating socket\n");
    exit(1);
  }
  while (1) {
    connectionFD = acceptNewConnections(socketFD);
    if (connectionFD < 0) {
      perror("Error establishing connection \n");
      deleteSocket(socketFD);
      exit(1);
    }
    doServiceFork(connectionFD);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fe <main+0x5e>
lea    -0x70(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1050 <write@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%edi
call   1030 <createServerSocket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1239 <main+0x99>
lea    0xdf6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    123e <main+0x9e>
mov    -0x14(%rbp),%edi
call   1080 <acceptNewConnections@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1271 <main+0xd1>
lea    0xddd(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1090 <deleteSocket@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <doServiceFork@plt>
jmp    123e <main+0x9e>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
cmp    $0x2,%edi
jne    11d4 <main+0x34>
mov    0x8(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1030 <createServerSocket@plt>
test   %eax,%eax
jns    1207 <main+0x67>
lea    0xe5b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xe25(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %eax,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1080 <acceptNewConnections@plt>
test   %eax,%eax
js     1224 <main+0x84>
mov    %eax,%edi
call   1070 <doServiceFork@plt>
jmp    1210 <main+0x70>
lea    0xe0c(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1090 <deleteSocket@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: ServerSocket PortNumber\0A\00"
@.str.1 = [23 x i8] c"Error creating socket\0A\00"
@.str.2 = [32 x i8] c"Error establishing connection \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [80 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%17 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @write(i32 2, i8* %16, i32 %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = call i32 @createServerSocket(i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %21
br label %34
34: ; preds = %33, %44
%35 = load i32, i32* %6
%36 = call i32 @acceptNewConnections(i32 %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %6
%42 = call i32 @deleteSocket(i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = load i32, i32* %7
%46 = call i32 @doServiceFork(i32 %45)
br label %34
}

LLVM IR (-O2):
@.str = [32 x i8] c"Usage: ServerSocket PortNumber\0A\00"
@.str.1 = [23 x i8] c"Error creating socket\0A\00"
@.str.2 = [32 x i8] c"Error establishing connection \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strlen(i8* %4)
%9 = call i32 @write(i32 2, i8* %4, i32 %8)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i32 @createServerSocket(i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = tail call i32 @acceptNewConnections(i32 %15)
%19 = icmp slt i32 %18, 0
br i1 %19, label %23, label %27
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %27, %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @deleteSocket(i32 %15)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %17, %27
%28 = phi i32 [ %30, %27 ], [ %18, %17 ]
%29 = tail call i32 @doServiceFork(i32 %28)
%30 = tail call i32 @acceptNewConnections(i32 %15)
%31 = icmp slt i32 %30, 0
br i1 %31, label %23, label %27
}