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

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char **, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int handle_client(struct sockaddr_in client_conn, int client_socketfd,
                  int list_socketfd) {
  char buf[1024];
  if (fork() != 0) {
    close(client_socketfd);
    return 0;
  }
  while (1) {
    int len = recv(client_socketfd, &buf, 1024 - 1, 0);
    if (len == -1) {
      close(client_socketfd);
      printf("There was an error recieving data.");
      return -2;
    }
    if (len == 0) {
      printf("Client disconnected.\n");
      close(client_socketfd);
      close(list_socketfd);
      return 1;
    }
    buf[len] = '\0';
    printf("Client said: %s\n", buf);
    send(client_socketfd, buf, len, 0);
  }
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     1189 <handle_client+0x39>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1250 <handle_client+0x100>
jmp    118e <handle_client+0x3e>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    11d9 <handle_client+0x89>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1250 <handle_client+0x100>
cmpl   $0x0,-0x414(%rbp)
jne    1210 <handle_client+0xc0>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1250 <handle_client+0x100>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
jmp    118e <handle_client+0x3e>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     118e <handle_client+0x2e>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    121c <handle_client+0xbc>
mov    %rsp,%r12
lea    0xe8b(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     11fc <handle_client+0x9c>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11e0 <handle_client+0x80>
movslq %ebx,%rax
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
jmp    11a0 <handle_client+0x40>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xe12(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xfffffffe,%ebx
jmp    121c <handle_client+0xbc>
lea    0xe31(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@.str = [35 x i8] c"There was an error recieving data.\00"
@.str.1 = [22 x i8] c"Client disconnected.\0A\00"
@.str.2 = [17 x i8] c"Client said: %s\0A\00"
define i32 @handle_client(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %0, i32* %10
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = call i64 (...) @fork()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* %6
%15 = call i32 @close(i32 %14)
store i32 0, i32* %4
br label %46
16: ; preds = %3
br label %17
17: ; preds = %16, %36
%18 = load i32, i32* %6
%19 = bitcast [1024 x i8]* %8 to i8**
%20 = call i32 @recv(i32 %18, i8** %19, i32 1023, i32 0)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %4
br label %46
27: ; preds = %17
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
store i32 1, i32* %4
br label %46
36: ; preds = %27
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %6
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* %9
%45 = call i32 @send(i32 %42, i8* %43, i32 %44, i32 0)
br label %17
46: ; preds = %30, %23, %13
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [35 x i8] c"There was an error recieving data.\00"
@.str.2 = [17 x i8] c"Client said: %s\0A\00"
@str = [21 x i8] c"Client disconnected.\00"
define i32 @handle_client(i32 %0, i32 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = bitcast [1024 x i8]* %4 to i8**
br label %12
10: ; preds = %3
%11 = tail call i32 @close(i32 %1)
br label %26
12: ; preds = %8, %21
%13 = call i32 @recv(i32 %1, i8** %9, i32 1023, i32 0)
switch i32 %13, label %21 [
i32 -1, label %14
i32 0, label %17
]
14: ; preds = %12
%15 = call i32 @close(i32 %1)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %26
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%19 = call i32 @close(i32 %1)
%20 = call i32 @close(i32 %2)
br label %26
21: ; preds = %12
%22 = sext i32 %13 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %22
store i8 0, i8* %23
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%25 = call i32 @send(i32 %1, i8* %5, i32 %13, i32 0)
br label %12
26: ; preds = %14, %17, %10
%27 = phi i32 [ 0, %10 ], [ -2, %14 ], [ 1, %17 ]
ret i32 %27
}