#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*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int pthread_self();
int read(int, char *, int);
void *handle_client(void *arg) {
  int clientfd = *((int *)(&arg)), bytes_read = -1;
  char message[256];
  printf("Thread %08x\n returning to memory heaven.\n", pthread_self());
  while (bytes_read) {
    bytes_read = read(clientfd, message, sizeof message);
    if (bytes_read < 0) {
      perror("ERROR reading socket");
      break;
    } else if (bytes_read > 0) {
      printf("[%d] Read: %s\n", pthread_self(), message);
    }
  }
  close(clientfd);
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     1201 <handle_client+0xa1>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cf <handle_client+0x6f>
lea    0xe65(%rip),%rdi
call   1080 <perror@plt>
jmp    1201 <handle_client+0xa1>
cmpl   $0x0,-0x18(%rbp)
jle    11f7 <handle_client+0x97>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    -0x120(%rbp),%rdx
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fc <handle_client+0x9c>
jmp    1193 <handle_client+0x33>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r15
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xe83(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11de <handle_client+0x7e>
lea    0xe9e(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %eax,%eax
je     11ea <handle_client+0x8a>
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11b0 <handle_client+0x50>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1060 <pthread_exit@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Thread %08x\0A returning to memory heaven.\0A\00"
@.str.1 = [21 x i8] c"ERROR reading socket\00"
@.str.2 = [15 x i8] c"[%d] Read: %s\0A\00"
define i8* @handle_client(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
store i8* %0, i8** %3
%7 = bitcast i8** %3 to i32*
%8 = load i32, i32* %7
store i32 %8, i32* %4
store i32 -1, i32* %5
%9 = call i32 (...) @pthread_self()
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %30, %1
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %11
%15 = load i32, i32* %4
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 256)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %31
22: ; preds = %14
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = call i32 (...) @pthread_self()
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %26, i8* %27)
br label %29
29: ; preds = %25, %22
br label %30
30: ; preds = %29
br label %11
31: ; preds = %20, %11
%32 = load i32, i32* %4
%33 = call i32 @close(i32 %32)
%34 = call i32 @pthread_exit(i32* null)
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
@.str = [42 x i8] c"Thread %08x\0A returning to memory heaven.\0A\00"
@.str.1 = [21 x i8] c"ERROR reading socket\00"
@.str.2 = [15 x i8] c"[%d] Read: %s\0A\00"
define noalias i8* @handle_client(i8* %0) {
%2 = alloca [256 x i8]
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = tail call i32 (...) @pthread_self()
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 @read(i32 %4, i8* %5, i32 256)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %15, %1
%11 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %20
12: ; preds = %1, %15
%13 = phi i32 [ %18, %15 ], [ %8, %1 ]
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = call i32 (...) @pthread_self()
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %16, i8* %5)
%18 = call i32 @read(i32 %4, i8* %5, i32 256)
%19 = icmp slt i32 %18, 0
br i1 %19, label %10, label %12
20: ; preds = %12, %10
%21 = call i32 @close(i32 %4)
%22 = call i32 @pthread_exit(i32* null)
ret i8* undef
}