#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;
};

/* Variables and functions */
int /*<<< orphan*/ DoStuff(int, char *, char *);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int Loop(int sockfd, struct sockaddr_in *cli_addr, int clilen, char *buffer,
         char *writeBuf) {
  printf("start a loop!\n");
  int fd = accept(sockfd, (struct sockaddr *)cli_addr, &clilen);
  if (fd < 0) {
    return 0;
  }
  if (fork() == 0) {
    close(sockfd);
    DoStuff(fd, buffer, writeBuf);
    close(fd);
    printf("End a loop in child process\n");
    exit(0);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<Loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11af <Loop+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    11fb <Loop+0x9b>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11f3 <Loop+0x93>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <DoStuff@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
lea    0xe28(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Loop>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %edx,0x4(%rsp)
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <accept@plt>
test   %eax,%eax
js     11aa <Loop+0x4a>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11b7 <Loop+0x57>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <DoStuff@plt>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@str = [14 x i8] c"start a loop!\00"
@str.2 = [28 x i8] c"End a loop in child process\00"
define i32 @Loop(i32 %0, %struct.sockaddr_in* %1, i32 %2, i8* %3, i8* %4) {
%6 = alloca i32
store i32 %2, i32* %6
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%8 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%9 = call i32 @accept(i32 %0, %struct.sockaddr* %8, i32* %6)
%10 = icmp slt i32 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %5
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = call i32 @close(i32 %0)
%16 = call i32 @DoStuff(i32 %9, i8* %3, i8* %4)
%17 = call i32 @close(i32 %9)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %11
%21 = call i32 @close(i32 %9)
br label %22
22: ; preds = %5, %20
ret i32 0
}