#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 errno;
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
void *thr_func(void *arg) {
  int ret = 0, n = 0;
  int fd = (int)arg;
  char ibuf[4096] = {0};
  while (n++ < 10) {
    ret = read(fd, ibuf, sizeof ibuf);
    printf("read %d from fifo\n", ret);
    if (ret < 0) {
      break;
    } else if (ret == 0) {
      break;
    }
  }
  printf("read over, ret = %d, errno %d\n", ret, errno);
}


x86 assembly (clang, -O0):
<thr_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x1c(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0xa,%eax
jge    11cb <thr_func+0x9b>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jge    11b2 <thr_func+0x82>
jmp    11cb <thr_func+0x9b>
cmpl   $0x0,-0x14(%rbp)
jne    11c1 <thr_func+0x91>
jmp    11cb <thr_func+0x9b>
jmp    11c6 <thr_func+0x96>
jmp    1167 <thr_func+0x37>
mov    -0x14(%rbp),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thr_func>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
mov    %rsp,%rbp
mov    $0x1000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xe9a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xe48(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xdf6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xdcd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xda4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xd7f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xd5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    12d4 <thr_func+0x1a4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xd35(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
lea    0xd2f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"read %d from fifo\0A\00"
@.str.1 = [31 x i8] c"read over, ret = %d, errno %d\0A\00"
@errno = global i32 0
define i8* @thr_func(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i32
store i32 %9, i32* %6
%10 = bitcast [4096 x i8]* %7 to i8*
br label %11
11: ; preds = %29, %1
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
store i32 %13, i32* %5
%14 = icmp slt i32 %12, 10
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = load i32, i32* %6
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 4096)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %15
br label %30
24: ; preds = %15
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
br label %30
28: ; preds = %24
br label %29
29: ; preds = %28
br label %11
30: ; preds = %27, %23, %11
%31 = load i32, i32* %4
%32 = load i32, i32* @errno
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %32)
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@.str = [19 x i8] c"read %d from fifo\0A\00"
@.str.1 = [31 x i8] c"read over, ret = %d, errno %d\0A\00"
@errno = global i32 0
define noalias i8* @thr_func(i8* %0) {
%2 = alloca [4096 x i8]
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%6 = call i32 @read(i32 %4, i8* %5, i32 4096)
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %6, 1
br i1 %8, label %44, label %9
9: ; preds = %1
%10 = call i32 @read(i32 %4, i8* %5, i32 4096)
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %10, 1
br i1 %12, label %44, label %13
13: ; preds = %9
%14 = call i32 @read(i32 %4, i8* %5, i32 4096)
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %14, 1
br i1 %16, label %44, label %17
17: ; preds = %13
%18 = call i32 @read(i32 %4, i8* %5, i32 4096)
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %18, 1
br i1 %20, label %44, label %21
21: ; preds = %17
%22 = call i32 @read(i32 %4, i8* %5, i32 4096)
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp slt i32 %22, 1
br i1 %24, label %44, label %25
25: ; preds = %21
%26 = call i32 @read(i32 %4, i8* %5, i32 4096)
%27 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = icmp slt i32 %26, 1
br i1 %28, label %44, label %29
29: ; preds = %25
%30 = call i32 @read(i32 %4, i8* %5, i32 4096)
%31 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = icmp slt i32 %30, 1
br i1 %32, label %44, label %33
33: ; preds = %29
%34 = call i32 @read(i32 %4, i8* %5, i32 4096)
%35 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = icmp slt i32 %34, 1
br i1 %36, label %44, label %37
37: ; preds = %33
%38 = call i32 @read(i32 %4, i8* %5, i32 4096)
%39 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = icmp slt i32 %38, 1
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = call i32 @read(i32 %4, i8* %5, i32 4096)
%43 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %37, %33, %29, %25, %21, %17, %13, %9, %1
%45 = phi i32 [ %6, %1 ], [ %10, %9 ], [ %14, %13 ], [ %18, %17 ], [ %22, %21 ], [ %26, %25 ], [ %30, %29 ], [ %34, %33 ], [ %38, %37 ], [ %42, %41 ]
%46 = load i32, i32* @errno
%47 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %46)
ret i8* undef
}