#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 BUFSIZ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
unsigned int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, unsigned int, int /*<<< orphan*/);
void *handle_thread(void *client_socket) {
  char buf[BUFSIZ];
  unsigned int len;
  int client_sockfd = *(int *)client_socket;
  while ((len = recv(client_sockfd, buf, BUFSIZ, 0)) > 0) {
    buf[len] = '\0';
    printf("%s\n", buf);
    if (send(client_sockfd, buf, len, 0) < 0) {
      perror("write");
      exit(1);
    }
  }
  close(client_sockfd);
  return NULL;
}


x86 assembly (clang, -O0):
<handle_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jbe    1211 <handle_thread+0xb1>
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%eax
movb   $0x0,(%rsi,%rax,1)
lea    0xe2a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0x0,%rax
jge    120c <handle_thread+0xac>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11a1 <handle_thread+0x41>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_thread>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6b(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%r12
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    (%rdi),%r14d
mov    %r14d,%edi
mov    %r12,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     11ed <handle_thread+0x8d>
mov    %eax,%ebx
lea    0xe56(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebx,%eax
movb   $0x0,(%r12,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
test   %rax,%rax
js     1206 <handle_thread+0xa6>
mov    0x0(%r13),%edx
mov    %r14d,%edi
mov    %r12,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11b0 <handle_thread+0x50>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [6 x i8] c"write\00"
define i8* @handle_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = load i8*, i8** %2
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
store i32 %13, i32* %6
br label %14
14: ; preds = %31, %1
%15 = load i32, i32* %6
%16 = load i32, i32* @BUFSIZ
%17 = call i32 @recv(i32 %15, i8* %10, i32 %16, i32 0)
store i32 %17, i32* %5
%18 = icmp ugt i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %14
%20 = load i32, i32* %5
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %10, i64 %21
store i8 0, i8* %22
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %10)
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = call i64 @send(i32 %24, i8* %10, i32 %25, i32 0)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
br label %14
32: ; preds = %14
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %3
ret i8* null
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [6 x i8] c"write\00"
define noalias i8* @handle_thread(i8* nocapture %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = call i32 @recv(i32 %6, i8* %4, i32 %2, i32 0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %1, %16
%10 = phi i32 [ %18, %16 ], [ %7, %1 ]
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %4, i64 %11
store i8 0, i8* %12
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %4)
%14 = call i64 @send(i32 %6, i8* %4, i32 %10, i32 0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %9
%17 = load i32, i32* @BUFSIZ
%18 = call i32 @recv(i32 %6, i8* %4, i32 %17, i32 0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %9
20: ; preds = %9
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16, %1
%24 = call i32 @close(i32 %6)
ret i8* null
}