#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*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int sprintf(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void *function_thread1(void *arg) {
  int fd = (int)arg;
  char buf[500];
  int len;
  int cnt = 0;
  while (1) {
    len = sprintf(buf, "Hello, main thread, cnt = %d", cnt++);
    write(fd, buf, len);
    len = read(fd, buf, 500);
    buf[len] = '\0';
    printf("%s\n", buf);
    sleep(5);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<function_thread1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x208(%rbp)
lea    -0x200(%rbp),%rdi
mov    -0x208(%rbp),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,-0x20c(%rbp)
mov    %eax,-0x208(%rbp)
lea    0xe6b(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,-0x204(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    -0x204(%rbp),%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    $0x1f4,%edx
call   1050 <read@plt>
mov    %eax,-0x204(%rbp)
movslq -0x204(%rbp),%rax
movb   $0x0,-0x200(%rbp,%rax,1)
lea    -0x200(%rbp),%rsi
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
jmp    1170 <function_thread1+0x20>

x86 assembly (clang, -O2):
<function_thread1>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%r15
xor    %edx,%edx
lea    0xe95(%rip),%r14
mov    %rsp,%rbx
lea    0xea8(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rdx),%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <sprintf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1f4,%edx
call   1050 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    %ebp,%edx
jmp    1180 <function_thread1+0x30>

LLVM IR (-O0):
@.str = [29 x i8] c"Hello, main thread, cnt = %d\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i8* @function_thread1(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [500 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = ptrtoint i8* %7 to i32
store i32 %8, i32* %3
store i32 0, i32* %6
br label %9
9: ; preds = %1, %9
%10 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
store i32 %12, i32* %6
%13 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %13, i32* %5
%14 = load i32, i32* %3
%15 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 500)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @sleep(i32 5)
br label %9
}

LLVM IR (-O2):
@.str = [29 x i8] c"Hello, main thread, cnt = %d\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define noalias i8* @function_thread1(i8* %0) {
%2 = alloca [500 x i8]
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
br label %6
6: ; preds = %6, %1
%7 = phi i32 [ 0, %1 ], [ %8, %6 ]
%8 = add nuw nsw i32 %7, 1
%9 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
%10 = call i32 @write(i32 %4, i8* %5, i32 %9)
%11 = call i32 @read(i32 %4, i8* %5, i32 500)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%15 = call i32 @sleep(i32 5)
br label %6
}