#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
void *thread_main(void *arg) {
  int cd = *((int *)arg), read_n;
  char *buffer = malloc(100);
  if ((read_n = read(cd, buffer, 100)) < 0) {
    perror("could not read");
  } else {
    buffer[read_n] = 0;
    printf("Received: %s\n", buffer);
  }
  free(arg);
  free(buffer);
  return NULL;
}


x86 assembly (clang, -O0):
<thread_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11a1 <thread_main+0x51>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
jmp    11bd <thread_main+0x6d>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rsi
lea    0xe57(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_main>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    (%rdi),%ebp
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x64,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11a5 <thread_main+0x55>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
lea    0xe89(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
jmp    118e <thread_main+0x3e>

LLVM IR (-O0):
@.str = [15 x i8] c"could not read\00"
@.str.1 = [14 x i8] c"Received: %s\0A\00"
define i8* @thread_main(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
store i32 %8, i32* %3
%9 = call i8* @malloc(i32 100)
store i8* %9, i8** %5
%10 = load i32, i32* %3
%11 = load i8*, i8** %5
%12 = call i32 @read(i32 %10, i8* %11, i32 100)
store i32 %12, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %23
16: ; preds = %1
%17 = load i8*, i8** %5
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 0, i8* %20
%21 = load i8*, i8** %5
%22 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %16, %14
%24 = load i8*, i8** %2
%25 = call i32 @free(i8* %24)
%26 = load i8*, i8** %5
%27 = call i32 @free(i8* %26)
ret i8* null
}

LLVM IR (-O2):
@.str = [15 x i8] c"could not read\00"
@.str.1 = [14 x i8] c"Received: %s\0A\00"
define noalias i8* @thread_main(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%5 = tail call i32 @read(i32 %3, i8* %4, i32 100)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %13
9: ; preds = %1
%10 = zext i32 %5 to i64
%11 = getelementptr inbounds i8, i8* %4, i64 %10
store i8 0, i8* %11
%12 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %13
13: ; preds = %9, %7
%14 = tail call i32 @free(i8* %0)
%15 = tail call i32 @free(i8* %4)
ret i8* null
}