#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 */
scalar_t__ bind_socket(int, int, int);
int create_socket();
int /*<<< orphan*/ destroy_socket(int);
int /*<<< orphan*/ disconnect_socket(int);
int /*<<< orphan*/ do_processing(int);
int fork();
int /*<<< orphan*/ perror(char *);
int wait_connection(int);
int main(int argc, char *argv[]) {
  int sockfd = create_socket();
  if (sockfd != -1) {
    if (bind_socket(sockfd, 9090, 10)) {
      while (1) {
        int new_sockfd = wait_connection(sockfd);
        if (new_sockfd != -1) {
          int pid = fork();
          if (pid >= 0) {
            if (pid == 0) {
              disconnect_socket(sockfd);
              do_processing(new_sockfd);
              disconnect_socket(new_sockfd);
            }
          } else {
            perror("ERROR forking");
          }
        } else {
          perror("ERROR waiting");
        }
      }
    } else {
      perror("ERROR binding");
    }
    disconnect_socket(sockfd);
  } else {
    perror("ERROR opening socket");
  }
  destroy_socket(sockfd);
  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)
mov    $0x0,%al
call   1080 <create_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1256 <main+0xd6>
mov    -0x14(%rbp),%edi
mov    $0x2382,%esi
mov    $0xa,%edx
call   1030 <bind_socket@plt>
cmp    $0x0,%rax
je     123d <main+0xbd>
jmp    11cb <main+0x4b>
mov    -0x14(%rbp),%edi
call   1060 <wait_connection@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     122c <main+0xac>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     121b <main+0x9b>
cmpl   $0x0,-0x1c(%rbp)
jne    1216 <main+0x96>
mov    -0x14(%rbp),%edi
call   1040 <disconnect_socket@plt>
mov    -0x18(%rbp),%edi
call   1070 <do_processing@plt>
mov    -0x18(%rbp),%edi
call   1040 <disconnect_socket@plt>
jmp    1227 <main+0xa7>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
jmp    1238 <main+0xb8>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
jmp    11cb <main+0x4b>
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <disconnect_socket@plt>
jmp    1262 <main+0xe2>
lea    0xdcd(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <destroy_socket@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <create_socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     120c <main+0x8c>
mov    %ebx,%edi
mov    $0x2382,%esi
mov    $0xa,%edx
call   1030 <bind_socket@plt>
test   %rax,%rax
je     121a <main+0x9a>
lea    0xe5c(%rip),%r14
lea    0xe47(%rip),%r15
jmp    11d0 <main+0x50>
mov    %r14,%rdi
call   1090 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1060 <wait_connection@plt>
cmp    $0xffffffff,%eax
je     11bb <main+0x3b>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     1202 <main+0x82>
jne    11d0 <main+0x50>
mov    %ebx,%edi
call   1040 <disconnect_socket@plt>
mov    %ebp,%edi
call   1070 <do_processing@plt>
mov    %ebp,%edi
call   1040 <disconnect_socket@plt>
jmp    11d0 <main+0x50>
mov    %r15,%rdi
call   1090 <perror@plt>
jmp    11d0 <main+0x50>
lea    0xe17(%rip),%rdi
call   1090 <perror@plt>
jmp    122d <main+0xad>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1040 <disconnect_socket@plt>
mov    %ebx,%edi
call   1050 <destroy_socket@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"ERROR forking\00"
@.str.1 = [14 x i8] c"ERROR waiting\00"
@.str.2 = [14 x i8] c"ERROR binding\00"
@.str.3 = [21 x i8] c"ERROR opening socket\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @create_socket()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %48
12: ; preds = %2
%13 = load i32, i32* %6
%14 = call i64 @bind_socket(i32 %13, i32 9090, i32 10)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %12
br label %17
17: ; preds = %16, %42
%18 = load i32, i32* %6
%19 = call i32 @wait_connection(i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %40
22: ; preds = %17
%23 = call i32 (...) @fork()
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %22
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = load i32, i32* %6
%31 = call i32 @disconnect_socket(i32 %30)
%32 = load i32, i32* %7
%33 = call i32 @do_processing(i32 %32)
%34 = load i32, i32* %7
%35 = call i32 @disconnect_socket(i32 %34)
br label %36
36: ; preds = %29, %26
br label %39
37: ; preds = %22
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %36
br label %42
40: ; preds = %17
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %39
br label %17
43: ; preds = %12
%44 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43
%46 = load i32, i32* %6
%47 = call i32 @disconnect_socket(i32 %46)
br label %50
48: ; preds = %2
%49 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %45
%51 = load i32, i32* %6
%52 = call i32 @destroy_socket(i32 %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"ERROR forking\00"
@.str.1 = [14 x i8] c"ERROR waiting\00"
@.str.2 = [14 x i8] c"ERROR binding\00"
@.str.3 = [21 x i8] c"ERROR opening socket\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @create_socket()
%4 = icmp eq i32 %3, -1
br i1 %4, label %28, label %5
5: ; preds = %2
%6 = tail call i64 @bind_socket(i32 %3, i32 9090, i32 10)
%7 = icmp eq i64 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %5, %22
%9 = tail call i32 @wait_connection(i32 %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %23, label %11
11: ; preds = %8
%12 = tail call i32 (...) @fork()
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = icmp eq i32 %12, 0
br i1 %15, label %16, label %22
16: ; preds = %14
%17 = tail call i32 @disconnect_socket(i32 %3)
%18 = tail call i32 @do_processing(i32 %9)
%19 = tail call i32 @disconnect_socket(i32 %9)
br label %22
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %16, %14, %23
br label %8
23: ; preds = %8
%24 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %22
25: ; preds = %5
%26 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @disconnect_socket(i32 %3)
br label %30
28: ; preds = %2
%29 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = tail call i32 @destroy_socket(i32 %3)
ret i32 0
}