#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ newConnection(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ wait();
int acceptLoop(int fd) {
  int err;
  while (1) {
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    int csock;
    csock = accept(fd, (struct sockaddr *)&addr, &addrlen);
    if (csock < 0) {
      perror("Error accepting connection");
      return -1;
    }
    switch (fork()) {
    case -1:
      perror("Unable to fork");
      return -1;
    case 0:
      switch (fork()) {
      case -1:
        perror("Unable to fork");
        return -1;
      case 0:
        close(fd);
        newConnection(csock);
        exit(1);
      default:
        exit(0);
      }
    default:
      wait();
      close(csock);
      break;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<acceptLoop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b7 <acceptLoop+0x47>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <acceptLoop+0x100>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11e1 <acceptLoop+0x71>
jmp    11d1 <acceptLoop+0x61>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11f9 <acceptLoop+0x89>
jmp    125c <acceptLoop+0xec>
lea    0xe33(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <acceptLoop+0x100>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     1223 <acceptLoop+0xb3>
jmp    1213 <acceptLoop+0xa3>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     123b <acceptLoop+0xcb>
jmp    1255 <acceptLoop+0xe5>
lea    0xdf1(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <acceptLoop+0x100>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <newConnection@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <wait@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
jmp    117b <acceptLoop+0xb>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<acceptLoop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1060 <accept@plt>
lea    0xe62(%rip),%r13
test   %eax,%eax
js     11f9 <acceptLoop+0x89>
mov    %eax,%ebp
lea    0xe70(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     120a <acceptLoop+0x9a>
test   %eax,%eax
je     11fe <acceptLoop+0x8e>
xor    %eax,%eax
call   1080 <wait@plt>
mov    %ebp,%edi
call   1030 <close@plt>
movl   $0x4,0xc(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c0 <acceptLoop+0x50>
mov    %r13,%r14
jmp    120a <acceptLoop+0x9a>
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
jne    1226 <acceptLoop+0xb6>
mov    %r14,%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %eax,%eax
jne    1242 <acceptLoop+0xd2>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
call   1040 <newConnection@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"Error accepting connection\00"
@.str.1 = [15 x i8] c"Unable to fork\00"
define i32 @acceptLoop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
br label %8
8: ; preds = %1, %36
store i32 4, i32* %6
%9 = load i32, i32* %3
%10 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %6)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
16: ; preds = %8
%17 = call i32 (...) @fork()
switch i32 %17, label %32 [
i32 -1, label %18
i32 0, label %20
]
18: ; preds = %16
%19 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
20: ; preds = %16
%21 = call i32 (...) @fork()
switch i32 %21, label %30 [
i32 -1, label %22
i32 0, label %24
]
22: ; preds = %20
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
24: ; preds = %20
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %7
%28 = call i32 @newConnection(i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %16
%33 = call i32 (...) @wait()
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %32
br label %8
37: ; preds = %22, %18, %14
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"Error accepting connection\00"
@.str.1 = [15 x i8] c"Unable to fork\00"
define i32 @acceptLoop(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
store i32 4, i32* %3
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %1, %20
%10 = phi i32 [ %23, %20 ], [ %7, %1 ]
%11 = call i32 (...) @fork()
switch i32 %11, label %20 [
i32 -1, label %25
i32 0, label %12
]
12: ; preds = %9
%13 = call i32 (...) @fork()
switch i32 %13, label %18 [
i32 -1, label %25
i32 0, label %14
]
14: ; preds = %12
%15 = call i32 @close(i32 %0)
%16 = call i32 @newConnection(i32 %10)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %9
%21 = call i32 (...) @wait()
%22 = call i32 @close(i32 %10)
store i32 4, i32* %3
%23 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %3)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %9
25: ; preds = %9, %20, %12, %1
%26 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %20 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %9 ]
%27 = call i32 @perror(i8* %26)
ret i32 -1
}