#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SOMAXCONN;
int create_and_bind(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int listen(int, int /*<<< orphan*/);
int make_socket_non_binding(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ process_work_process(int);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sfd, res, i;
  pid_t pid;
  if (argc != 2) {
    printf("usage : <port>\n");
    exit(0);
  }
  sfd = create_and_bind(argv[1]);
  if (sfd == -1) {
    perror("ERROR : create_and_bind\n");
    exit(1);
  }
  res = make_socket_non_binding(sfd);
  if (res == -1) {
    perror("ERROR : make_sokcet_non_binding\n");
    exit(1);
  }
  res = listen(sfd, SOMAXCONN);
  if (res == -1) {
    perror("ERROR : listen\n");
    exit(1);
  }
  for (i = 0; i < 4; i++) {
    if ((pid = fork()) < 0) {
      perror("ERROR : fork\n");
      exit(1);
    } else if (pid == 0) {
      process_work_process(sfd);
    } else {
      printf("worker process started, pid = %d\n", pid);
    }
  }
  while (waitpid(-1, NULL, 0))
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c5 <main+0x35>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <create_and_bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f5 <main+0x65>
lea    0xe2a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <make_socket_non_binding@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1220 <main+0x90>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1060 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1254 <main+0xc4>
lea    0xe05(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    12d2 <main+0x142>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    1290 <main+0x100>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12a8 <main+0x118>
mov    -0x14(%rbp),%edi
call   1040 <process_work_process@plt>
jmp    12ba <main+0x12a>
mov    -0x28(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12bf <main+0x12f>
jmp    12c4 <main+0x134>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    125b <main+0xcb>
jmp    12d7 <main+0x147>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0x0,%rax
je     12f6 <main+0x166>
jmp    12d7 <main+0x147>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    12b0 <main+0x110>
mov    0x8(%rsi),%rdi
call   1090 <create_and_bind@plt>
cmp    $0xffffffff,%eax
je     12c3 <main+0x123>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <make_socket_non_binding@plt>
cmp    $0xffffffff,%eax
je     12cc <main+0x12c>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     12d5 <main+0x135>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12a7 <main+0x107>
je     120c <main+0x6c>
lea    0xe58(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1213 <main+0x73>
mov    %ebx,%edi
call   1050 <process_work_process@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12a7 <main+0x107>
je     1238 <main+0x98>
lea    0xe2c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    123f <main+0x9f>
mov    %ebx,%edi
call   1050 <process_work_process@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12a7 <main+0x107>
je     1260 <main+0xc0>
lea    0xe04(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1267 <main+0xc7>
mov    %ebx,%edi
call   1050 <process_work_process@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12a7 <main+0x107>
je     1288 <main+0xe8>
lea    0xddc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1290 <main+0xf0>
mov    %ebx,%edi
call   1050 <process_work_process@plt>
nop
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %rax,%rax
jne    1290 <main+0xf0>
xor    %eax,%eax
pop    %rbx
ret
lea    0xd9c(%rip),%rdi
jmp    12dc <main+0x13c>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd36(%rip),%rdi
jmp    12dc <main+0x13c>
lea    0xd46(%rip),%rdi
jmp    12dc <main+0x13c>
lea    0xd5e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"usage : <port>\0A\00"
@.str.1 = [25 x i8] c"ERROR : create_and_bind\0A\00"
@.str.2 = [33 x i8] c"ERROR : make_sokcet_non_binding\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [16 x i8] c"ERROR : listen\0A\00"
@.str.4 = [14 x i8] c"ERROR : fork\0A\00"
@.str.5 = [34 x i8] c"worker process started, pid = %d\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 i32
%9 = alloca i64
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 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 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 @create_and_bind(i8* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = load i32, i32* %6
%27 = call i32 @make_socket_non_binding(i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = load i32, i32* %6
%35 = load i32, i32* @SOMAXCONN
%36 = call i32 @listen(i32 %34, i32 %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
store i32 0, i32* %8
br label %43
43: ; preds = %63, %42
%44 = load i32, i32* %8
%45 = icmp slt i32 %44, 4
br i1 %45, label %46, label %66
46: ; preds = %43
%47 = call i64 (...) @fork()
store i64 %47, i64* %9
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %46
%53 = load i64, i64* %9
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %6
%57 = call i32 @process_work_process(i32 %56)
br label %61
58: ; preds = %52
%59 = load i64, i64* %9
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i64 %59)
br label %61
61: ; preds = %58, %55
br label %62
62: ; preds = %61
br label %63
63: ; preds = %62
%64 = load i32, i32* %8
%65 = add nsw i32 %64, 1
store i32 %65, i32* %8
br label %43
66: ; preds = %43
br label %67
67: ; preds = %70, %66
%68 = call i64 @waitpid(i32 -1, i32* null, i32 0)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %67
br label %67
71: ; preds = %67
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c"ERROR : create_and_bind\0A\00"
@.str.2 = [33 x i8] c"ERROR : make_sokcet_non_binding\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [16 x i8] c"ERROR : listen\0A\00"
@.str.4 = [14 x i8] c"ERROR : fork\0A\00"
@.str.5 = [34 x i8] c"worker process started, pid = %d\0A\00"
@str = [15 x i8] c"usage : <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 ([15 x i8], [15 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 @create_and_bind(i8* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = tail call i32 @make_socket_non_binding(i32 %10)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = load i32, i32* @SOMAXCONN
%23 = tail call i32 @listen(i32 %10, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = tail call i64 (...) @fork()
%27 = icmp slt i64 %26, 0
br i1 %27, label %31, label %34
28: ; preds = %21
%29 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %58, %49, %40, %25
%32 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = icmp eq i64 %26, 0
br i1 %35, label %36, label %38
36: ; preds = %34
%37 = tail call i32 @process_work_process(i32 %10)
br label %40
38: ; preds = %34
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i64 %26)
br label %40
40: ; preds = %38, %36
%41 = tail call i64 (...) @fork()
%42 = icmp slt i64 %41, 0
br i1 %42, label %31, label %43
43: ; preds = %40
%44 = icmp eq i64 %41, 0
br i1 %44, label %47, label %45
45: ; preds = %43
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i64 %41)
br label %49
47: ; preds = %43
%48 = tail call i32 @process_work_process(i32 %10)
br label %49
49: ; preds = %47, %45
%50 = tail call i64 (...) @fork()
%51 = icmp slt i64 %50, 0
br i1 %51, label %31, label %52
52: ; preds = %49
%53 = icmp eq i64 %50, 0
br i1 %53, label %56, label %54
54: ; preds = %52
%55 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i64 %50)
br label %58
56: ; preds = %52
%57 = tail call i32 @process_work_process(i32 %10)
br label %58
58: ; preds = %56, %54
%59 = tail call i64 (...) @fork()
%60 = icmp slt i64 %59, 0
br i1 %60, label %31, label %61
61: ; preds = %58
%62 = icmp eq i64 %59, 0
br i1 %62, label %65, label %63
63: ; preds = %61
%64 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i64 %59)
br label %67
65: ; preds = %61
%66 = tail call i32 @process_work_process(i32 %10)
br label %67
67: ; preds = %65, %63
br label %68
68: ; preds = %67, %68
%69 = tail call i64 @waitpid(i32 -1, i32* null, i32 0)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %68
71: ; preds = %68
ret i32 0
}