#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 atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ ftserve_process(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket_accept(int);
int socket_create(int);
int main(int argc, char *argv[]) {
  int sock_listen, sock_controll, port, pid;
  if (argc != 2) {
    printf("usage: ./ftserve port\n");
    exit(0);
  }
  port = atoi(argv[1]);
  if ((sock_listen = socket_create(port)) < 0) {
    perror("Error creating socket");
    exit(1);
  }
  while (1) {
    if ((sock_controll = socket_accept(sock_listen)) < 0) {
      break;
    }
    if ((pid = fork()) < 0) {
      perror("Error forking child process");
    } else if (pid == 0) {
      close(sock_listen);
      ftserve_process(sock_controll);
      close(sock_controll);
      exit(0);
    }
    close(sock_controll);
  }
  close(sock_listen);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c3 <main+0x33>
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <socket_create@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11fd <main+0x6d>
lea    0xe29(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1202 <main+0x72>
mov    -0x14(%rbp),%edi
call   1030 <socket_accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    121b <main+0x8b>
jmp    127a <main+0xea>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    123f <main+0xaf>
lea    0xdf8(%rip),%rdi
call   1060 <perror@plt>
jmp    126d <main+0xdd>
cmpl   $0x0,-0x20(%rbp)
jne    1268 <main+0xd8>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1090 <ftserve_process@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    126d <main+0xdd>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    1202 <main+0x72>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    1227 <main+0x97>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   10a0 <socket_create@plt>
test   %eax,%eax
js     123a <main+0xaa>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <socket_accept@plt>
test   %eax,%eax
js     11fd <main+0x6d>
mov    %eax,%ebp
lea    0xe4b(%rip),%r14
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     11f3 <main+0x63>
je     120b <main+0x7b>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1040 <socket_accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11d0 <main+0x40>
jmp    11fd <main+0x6d>
mov    %r14,%rdi
call   1060 <perror@plt>
jmp    11dd <main+0x4d>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1090 <ftserve_process@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xdbf(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"usage: ./ftserve port\0A\00"
@.str.1 = [22 x i8] c"Error creating socket\00"
@.str.2 = [28 x i8] c"Error forking child process\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 i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = call i32 @socket_create(i32 %20)
store i32 %21, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
br label %27
27: ; preds = %26, %49
%28 = load i32, i32* %6
%29 = call i32 @socket_accept(i32 %28)
store i32 %29, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
br label %52
32: ; preds = %27
%33 = call i32 (...) @fork()
store i32 %33, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %49
37: ; preds = %32
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %7
%44 = call i32 @ftserve_process(i32 %43)
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %37
br label %49
49: ; preds = %48, %35
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
br label %27
52: ; preds = %31
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [22 x i8] c"Error creating socket\00"
@.str.2 = [28 x i8] c"Error forking child process\00"
@str = [22 x i8] c"usage: ./ftserve port\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = tail call i32 @socket_create(i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = tail call i32 @socket_accept(i32 %11)
%15 = icmp slt i32 %14, 0
br i1 %15, label %36, label %19
16: ; preds = %7
%17 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13, %32
%20 = phi i32 [ %34, %32 ], [ %14, %13 ]
%21 = tail call i32 (...) @fork()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %32
25: ; preds = %19
%26 = icmp eq i32 %21, 0
br i1 %26, label %27, label %32
27: ; preds = %25
%28 = tail call i32 @close(i32 %11)
%29 = tail call i32 @ftserve_process(i32 %20)
%30 = tail call i32 @close(i32 %20)
%31 = tail call i32 @exit(i32 0)
unreachable
32: ; preds = %25, %23
%33 = tail call i32 @close(i32 %20)
%34 = tail call i32 @socket_accept(i32 %11)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %19
36: ; preds = %32, %13
%37 = tail call i32 @close(i32 %11)
ret i32 0
}