#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;
  char message[256];
  printf("Thread %08x\n returning to memory heaven.\n", pthread_self());
  while (1) {
    bytes_read = read(clientfd, message, sizeof message);
    if (bytes_read < 0) {
      perror("ERROR reading socket");
      break;
    }
    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)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11be <handle_client+0x5e>
lea    0xe76(%rip),%rdi
call   1080 <perror@plt>
jmp    11e1 <handle_client+0x81>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    -0x120(%rbp),%rdx
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    118c <handle_client+0x2c>
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     11da <handle_client+0x7a>
lea    0xe9e(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
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    0xe49(%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
%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 = %1, %19
%12 = load i32, i32* %4
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 256)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %23
19: ; preds = %11
%20 = call i32 (...) @pthread_self()
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %20, i8* %21)
br label %11
23: ; preds = %17
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
%26 = call i32 @pthread_exit(i32* null)
%27 = load i8*, i8** %2
ret i8* %27
}

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 %14
10: ; preds = %14, %1
%11 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @close(i32 %4)
%13 = call i32 @pthread_exit(i32* null)
ret i8* undef
14: ; preds = %1, %14
%15 = call i32 (...) @pthread_self()
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %15, i8* %5)
%17 = call i32 @read(i32 %4, i8* %5, i32 256)
%18 = icmp slt i32 %17, 0
br i1 %18, label %10, label %14
}