#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 */
typedef int /*<<< orphan*/ r_buf;

/* Variables and functions */
int EAGAIN;
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ sleep(int);
main(int argc, char **argv) {
  char r_buf[4096 * 2];
  int fd;
  int r_size;
  int ret_size;
  if (argc < 2) {
    printf("Usage:%s size", argv[0]);
    exit(1);
  }
  r_size = atoi(argv[1]);
  printf("required real read bytes %d\n", r_size);
  memset(r_buf, 0, sizeof(r_buf));
  fd = open("/tmp/fifoserver", O_RDONLY, 0);
  if (fd == -1) {
    printf("open %s for read error\n", "/tmp/fifoserver");
    exit(1);
  }
  while (1) {
    memset(r_buf, 0, sizeof(r_buf));
    ret_size = (int)read(fd, r_buf, (size_t)r_size);
    if (ret_size == -1) {
      if (errno == EAGAIN) {
        printf("no data avlaible\n");
      } else {
        printf("read error:%d\n", errno);
      }
    }
    printf("real read bytes %d\n", ret_size);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x2018(%rbp)
mov    -0x2018(%rbp),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1040 <memset@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x2014(%rbp)
cmpl   $0xffffffff,-0x2014(%rbp)
jne    1235 <main+0xc5>
lea    0xe1e(%rip),%rdi
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    123a <main+0xca>
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1040 <memset@plt>
mov    -0x2014(%rbp),%edi
lea    -0x2010(%rbp),%rsi
movslq -0x2018(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x201c(%rbp)
cmpl   $0xffffffff,-0x201c(%rbp)
jne    12c0 <main+0x150>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    0x2d3f(%rip),%rcx
cmp    (%rcx),%eax
jne    12a4 <main+0x134>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12bb <main+0x14b>
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c0 <main+0x150>
mov    -0x201c(%rbp),%esi
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    123a <main+0xca>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
cmp    $0x1,%edi
jle    126a <main+0xea>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
lea    0xe62(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1050 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xe57(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1276 <main+0xf6>
mov    %eax,%r14d
movslq %ebx,%rbx
mov    %rsp,%rbp
mov    0x2dd4(%rip),%r12
lea    0xe1c(%rip),%r13
jmp    1227 <main+0xa7>
lea    0xe4f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
mov    %rax,%r15
cmp    $0xffffffff,%r15d
jne    1210 <main+0x90>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
cmp    (%r12),%esi
jne    11fd <main+0x7d>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
jmp    1210 <main+0x90>
mov    (%rsi),%rsi
lea    0xd8c(%rip),%rdi
jmp    1284 <main+0x104>
lea    0xdbe(%rip),%rdi
lea    0xda7(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Usage:%s size\00"
@.str.1 = [29 x i8] c"required real read bytes %d\0A\00"
@.str.2 = [16 x i8] c"/tmp/fifoserver\00"
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"open %s for read error\0A\00"
@errno = global i32 0
@EAGAIN = global i32 0
@.str.4 = [18 x i8] c"no data avlaible\0A\00"
@.str.5 = [15 x i8] c"read error:%d\0A\00"
@.str.6 = [20 x i8] c"real read bytes %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [8192 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 8192)
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 0)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %18
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %18
br label %35
35: ; preds = %34, %56
%36 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 8192)
%38 = load i32, i32* %7
%39 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = call i64 @read(i32 %38, i8* %39, i64 %41)
%43 = trunc i64 %42 to i32
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %56
46: ; preds = %35
%47 = load i32, i32* @errno
%48 = load i32, i32* @EAGAIN
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %55
52: ; preds = %46
%53 = load i32, i32* @errno
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %52, %50
br label %56
56: ; preds = %55, %35
%57 = load i32, i32* %9
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %57)
%59 = call i32 @sleep(i32 1)
br label %35
}

LLVM IR (-O2):
@.str = [14 x i8] c"Usage:%s size\00"
@.str.1 = [29 x i8] c"required real read bytes %d\0A\00"
@.str.2 = [16 x i8] c"/tmp/fifoserver\00"
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"open %s for read error\0A\00"
@errno = global i32 0
@EAGAIN = global i32 0
@.str.5 = [15 x i8] c"read error:%d\0A\00"
@.str.6 = [20 x i8] c"real read bytes %d\0A\00"
@str = [17 x i8] c"no data avlaible\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 @memset(i8* %4, i32 0, i32 8192)
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %21, label %19
19: ; preds = %10
%20 = sext i32 %13 to i64
br label %24
21: ; preds = %10
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %19, %37
%25 = call i32 @memset(i8* %4, i32 0, i32 8192)
%26 = call i64 @read(i32 %17, i8* %4, i64 %20)
%27 = trunc i64 %26 to i32
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = load i32, i32* @EAGAIN
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %37
35: ; preds = %29
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %30)
br label %37
37: ; preds = %33, %35, %24
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %27)
%39 = call i32 @sleep(i32 1)
br label %24
}