#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 /*<<< orphan*/ socklen_t;
typedef int pid_t;
typedef int /*<<< orphan*/ client;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ handleConnection(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ logData(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
void mainLoop(FILE *logf, int sock) {
  int clientfd = 0;
  struct sockaddr_in client;
  socklen_t clientlen = 0;
  pid_t offspring = 0;
  memset((char *)&client, 0, sizeof(client));
  logData(logf, "entering main loop...");
  while (1) {
    clientfd = accept(sock, (struct sockaddr *)&client, &clientlen);
    if (clientfd == -1) {
      continue;
    }
    offspring = fork();
    if (offspring == -1) {
      continue;
    }
    if (offspring == 0) {
      handleConnection(logf, clientfd);
      close(clientfd);
      exit(0);
    }
    close(clientfd);
  }
}


x86 assembly (clang, -O0):
<mainLoop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1030 <logData@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d6 <mainLoop+0x66>
jmp    11b4 <mainLoop+0x44>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ef <mainLoop+0x7f>
jmp    11b4 <mainLoop+0x44>
cmpl   $0x0,-0x20(%rbp)
jne    1214 <mainLoop+0xa4>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <handleConnection@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    11b4 <mainLoop+0x44>

x86 assembly (clang, -O2):
<mainLoop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0xe5c(%rip),%rsi
mov    %r14,%rdi
call   1030 <logData@plt>
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <accept@plt>
cmp    $0xffffffff,%eax
je     11c0 <mainLoop+0x50>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11c0 <mainLoop+0x50>
test   %eax,%eax
je     11ed <mainLoop+0x7d>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11c0 <mainLoop+0x50>
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <handleConnection@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"entering main loop...\00"
define void @mainLoop(i32* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = call i32 @memset(i8* %5, i32 0, i32 4)
%8 = call i32 @logData(i32* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %10
10: ; preds = %21, %2
%11 = call i32 @accept(i32 %1, %struct.sockaddr* %9, i32* %4)
%12 = icmp eq i32 %11, -1
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = call i32 (...) @fork()
switch i32 %14, label %19 [
i32 -1, label %21
i32 0, label %15
]
15: ; preds = %13
%16 = call i32 @handleConnection(i32* %0, i32 %11)
%17 = call i32 @close(i32 %11)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %13
%20 = call i32 @close(i32 %11)
br label %21
21: ; preds = %19, %10, %13
br label %10
}