#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 /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ close(int);
int createInetStreamSocket();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ listenAtPort(int, int, int);
int /*<<< orphan*/ listeningProcess(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ wait();
int main(int argc, char *argv[]) {
  int socketDescriptor = createInetStreamSocket();
  listenAtPort(socketDescriptor, 9999, 1);
  printf("%sServer listening in port %d.\n%s", "\x1B[37m", 9999, "\x1B[0m");
  printf("%sWrite \"exit\" to quit.%s\n", "\x1B[37m", "\x1B[0m");
  char command[10];
  int pid = fork();
  switch (pid) {
  case -1:
    perror("Error creating child process.\n");
    break;
  case 0:
    listeningProcess(socketDescriptor);
    exit(0);
  default:
    do {
      scanf("%s", command);
      if (strcmp(command, "exit") != 0) {
        printf("%sInvalid command%s\n", "\x1B[31m", "\x1B[0m");
      }
    } while (strcmp(command, "exit") != 0);
    break;
  }
  kill(pid, SIGKILL);
  wait();
  close(socketDescriptor);
  printf("%sSocket closed. Bye!%s\n", "\x1B[31m", "\x1B[0m");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %al,-0x29(%rbp)
call   1070 <createInetStreamSocket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x270f,%esi
mov    %esi,-0x44(%rbp)
mov    $0x1,%edx
call   10c0 <listenAtPort@plt>
mov    -0x44(%rbp),%edx
mov    -0x29(%rbp),%al
lea    0xdfb(%rip),%rdi
lea    0xe16(%rip),%rsi
mov    %rsi,-0x40(%rbp)
lea    0xe11(%rip),%rcx
mov    %rcx,-0x38(%rbp)
call   1030 <printf@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x29(%rbp),%al
lea    0xdfb(%rip),%rdi
call   1030 <printf@plt>
mov    -0x29(%rbp),%al
call   10e0 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0xffffffff,%eax
je     1266 <main+0xa6>
jmp    1256 <main+0x96>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     1277 <main+0xb7>
jmp    1286 <main+0xc6>
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
jmp    12f3 <main+0x133>
mov    -0x14(%rbp),%edi
call   1040 <listeningProcess@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    128b <main+0xcb>
lea    -0x1e(%rbp),%rsi
lea    0xd89(%rip),%rdi
call   1090 <scanf@plt>
lea    -0x1e(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     12d0 <main+0x110>
lea    0xdb1(%rip),%rdi
lea    0xdbf(%rip),%rsi
lea    0xd5f(%rip),%rdx
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d5 <main+0x115>
lea    -0x1e(%rbp),%rdi
lea    0xd87(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    128b <main+0xcb>
jmp    12f3 <main+0x133>
mov    -0x24(%rbp),%edi
mov    0x2ce3(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
mov    $0x0,%al
call   10d0 <wait@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    0xd6d(%rip),%rdi
lea    0xd60(%rip),%rsi
lea    0xd00(%rip),%rdx
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1060 <createInetStreamSocket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x270f,%esi
mov    $0x1,%edx
call   10b0 <listenAtPort@plt>
lea    0xe21(%rip),%rdi
lea    0xe3c(%rip),%r14
lea    0xe3b(%rip),%rbp
mov    %r14,%rsi
mov    $0x270f,%edx
mov    %rbp,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe27(%rip),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
mov    %ebx,0x4(%rsp)
mov    %eax,0x8(%rsp)
je     129d <main+0xed>
test   %eax,%eax
je     12f8 <main+0x148>
lea    0xde9(%rip),%rbx
lea    0xe(%rsp),%rbp
mov    $0x74697865,%r15d
lea    0xe24(%rip),%r12
lea    0xe32(%rip),%r13
lea    0xdd2(%rip),%r14
jmp    1270 <main+0xc0>
nopl   0x0(%rax,%rax,1)
mov    0xe(%rsp),%eax
xor    %r15d,%eax
movzbl 0x12(%rsp),%ecx
or     %eax,%ecx
je     12a9 <main+0xf9>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <scanf@plt>
mov    0xe(%rsp),%eax
xor    %r15d,%eax
movzbl 0x12(%rsp),%ecx
or     %eax,%ecx
je     1260 <main+0xb0>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1260 <main+0xb0>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%edi
call   1070 <kill@plt>
xor    %eax,%eax
call   10c0 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
lea    0xdb5(%rip),%rdi
lea    0xda8(%rip),%rsi
lea    0xd48(%rip),%rdx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%edi
call   1040 <listeningProcess@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"%sServer listening in port %d.\0A%s\00"
@.str.1 = [6 x i8] c"\1B[37m\00"
@.str.2 = [5 x i8] c"\1B[0m\00"
@.str.3 = [27 x i8] c"%sWrite \22exit\22 to quit.%s\0A\00"
@.str.4 = [31 x i8] c"Error creating child process.\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.7 = [21 x i8] c"%sInvalid command%s\0A\00"
@.str.8 = [6 x i8] c"\1B[31m\00"
@SIGKILL = global i32 0
@.str.9 = [25 x i8] c"%sSocket closed. Bye!%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @createInetStreamSocket()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = call i32 @listenAtPort(i32 %10, i32 9999, i32 1)
%12 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 9999, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 (...) @fork()
store i32 %14, i32* %8
%15 = load i32, i32* %8
switch i32 %15, label %22 [
i32 -1, label %16
i32 0, label %18
]
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %37
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 @listeningProcess(i32 %19)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %2
br label %23
23: ; preds = %32, %22
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%27 = call i32 @strcmp(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
br label %32
32: ; preds = %31
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%34 = call i32 @strcmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %23, label %36
36: ; preds = %32
br label %37
37: ; preds = %36, %16
%38 = load i32, i32* %8
%39 = load i32, i32* @SIGKILL
%40 = call i32 @kill(i32 %38, i32 %39)
%41 = call i32 (...) @wait()
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
%44 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [34 x i8] c"%sServer listening in port %d.\0A%s\00"
@.str.1 = [6 x i8] c"\1B[37m\00"
@.str.2 = [5 x i8] c"\1B[0m\00"
@.str.3 = [27 x i8] c"%sWrite \22exit\22 to quit.%s\0A\00"
@.str.4 = [31 x i8] c"Error creating child process.\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.7 = [21 x i8] c"%sInvalid command%s\0A\00"
@.str.8 = [6 x i8] c"\1B[31m\00"
@SIGKILL = global i32 0
@.str.9 = [25 x i8] c"%sSocket closed. Bye!%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i8]
%4 = tail call i32 (...) @createInetStreamSocket()
%5 = tail call i32 @listenAtPort(i32 %4, i32 9999, i32 1)
%6 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 9999, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%9 = tail call i32 (...) @fork()
switch i32 %9, label %15 [
i32 -1, label %10
i32 0, label %12
]
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %24
12: ; preds = %2
%13 = tail call i32 @listeningProcess(i32 %4)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %2, %21
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%17 = call i32 @bcmp(i8* dereferenceable(5) %8, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64 5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %15, %19
%22 = call i32 @bcmp(i8* dereferenceable(5) %8, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64 5)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %15
24: ; preds = %21, %10
%25 = load i32, i32* @SIGKILL
%26 = call i32 @kill(i32 %9, i32 %25)
%27 = call i32 (...) @wait()
%28 = call i32 @close(i32 %4)
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}