#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 */
int /*<<< orphan*/ SHUT_RDWR;
int accept(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int read(int, char *, int);
int /*<<< orphan*/ server_pkg_handler(int, char *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
void server_loop(int sockfd) {
  while (true) {
    int cfd = accept(sockfd, 0, 0);
    char buffer[1024];
    int bufferbytes = read(cfd, buffer, 1024);
    if (fork() == 0 && bufferbytes > 0) {
      server_pkg_handler(cfd, buffer);
      shutdown(cfd, SHUT_RDWR);
      close(cfd);
    }
  }
}


x86 assembly (clang, -O0):
<server_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <accept@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11dd <server_loop+0x7d>
cmpl   $0x0,-0x414(%rbp)
jle    11dd <server_loop+0x7d>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1060 <server_pkg_handler@plt>
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    116e <server_loop+0xe>

x86 assembly (clang, -O2):
<server_loop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %edi,%r15d
mov    %rsp,%r14
mov    0x2e74(%rip),%r12
nopl   0x0(%rax)
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %ebx,%ebx
jle    1180 <server_loop+0x20>
test   %rax,%rax
jne    1180 <server_loop+0x20>
mov    %ebp,%edi
mov    %r14,%rsi
call   1060 <server_pkg_handler@plt>
mov    (%r12),%esi
mov    %ebp,%edi
call   1030 <shutdown@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1180 <server_loop+0x20>

LLVM IR (-O0):
@SHUT_RDWR = global i32 0
define void @server_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %26
%7 = load i32, i32* %2
%8 = call i32 @accept(i32 %7, i32 0, i32 0)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 1024)
store i32 %11, i32* %5
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %6
%15 = load i32, i32* %5
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i32, i32* %3
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 @server_pkg_handler(i32 %18, i8* %19)
%21 = load i32, i32* %3
%22 = load i32, i32* @SHUT_RDWR
%23 = call i32 @shutdown(i32 %21, i32 %22)
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %17, %14, %6
br label %6
}

LLVM IR (-O2):
@SHUT_RDWR = global i32 0
define void @server_loop(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %1, %16
%5 = call i32 @accept(i32 %0, i32 0, i32 0)
%6 = call i32 @read(i32 %5, i8* %3, i32 1024)
%7 = call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
%9 = icmp sgt i32 %6, 0
%10 = and i1 %9, %8
br i1 %10, label %11, label %16
11: ; preds = %4
%12 = call i32 @server_pkg_handler(i32 %5, i8* %3)
%13 = load i32, i32* @SHUT_RDWR
%14 = call i32 @shutdown(i32 %5, i32 %13)
%15 = call i32 @close(i32 %5)
br label %16
16: ; preds = %11, %4
br label %4
}