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

/* Variables and functions */
scalar_t__ ECHILD;
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, char *, int);
ssize_t my_read(int fd, char *ptr) {
  static int read_cnt = 0;
  static char *read_ptr;
  static char read_buf[1024];
  if (read_cnt <= 0) {
  again:
    if ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
      if ((errno == EINTR || errno == ECHILD))
        goto again;
      return -1;
    } else if (read_cnt == 0)
      return 0;
    read_ptr = read_buf;
  }
  read_cnt--;
  *ptr = *read_ptr++;
  return 1;
}


x86 assembly (clang, -O0):
<my_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,0x2f1a(%rip)
jg     11c5 <my_read+0xb5>
jmp    1131 <my_read+0x21>
mov    -0x8(%rbp),%edi
lea    0x2f15(%rip),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,0x2ef5(%rip)
cmp    $0x0,%eax
jge    1199 <my_read+0x89>
mov    0x2e85(%rip),%rax
mov    (%rax),%rax
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
je     1188 <my_read+0x78>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
jne    118d <my_read+0x7d>
jmp    1131 <my_read+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <my_read+0xe8>
cmpl   $0x0,0x2ea0(%rip)
jne    11b2 <my_read+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <my_read+0xe8>
jmp    11b7 <my_read+0xa7>
lea    0x2e92(%rip),%rax
mov    %rax,0x2e83(%rip)
mov    0x2e75(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2e6c(%rip)
mov    0x2e6d(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x2e5f(%rip)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2f1c(%rip),%eax
test   %eax,%eax
jle    115c <my_read+0x4c>
mov    0x2f19(%rip),%rbx
add    $0xffffffff,%eax
mov    %eax,0x2f08(%rip)
lea    0x1(%rbx),%rax
mov    %rax,0x2f05(%rip)
mov    (%rbx),%al
mov    %al,(%r14)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %edi,%ebp
lea    0x2eeb(%rip),%rbx
mov    0x2e74(%rip),%r15
mov    0x2e65(%rip),%r12
mov    0x2e56(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,0x2eab(%rip)
test   %eax,%eax
jns    11af <my_read+0x9f>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1180 <my_read+0x70>
cmp    0x0(%r13),%rax
je     1180 <my_read+0x70>
mov    $0xffffffff,%eax
jmp    114d <my_read+0x3d>
jne    112f <my_read+0x1f>
xor    %eax,%eax
jmp    114d <my_read+0x3d>

LLVM IR (-O0):
@my_read.read_cnt = internal global i32 0
@my_read.read_ptr = internal global i8* null
@my_read.read_buf = internal global [1024 x i8] zeroinitializer
@errno = global i64 0
@EINTR = global i64 0
@ECHILD = global i64 0
define i32 @my_read(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%6 = load i32, i32* @my_read.read_cnt
%7 = icmp sle i32 %6, 0
br i1 %7, label %8, label %29
8: ; preds = %2
br label %9
9: ; preds = %21, %8
%10 = load i32, i32* %4
%11 = call i32 @read(i32 %10, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @my_read.read_buf, i64 0, i64 0), i32 1024)
store i32 %11, i32* @my_read.read_cnt
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @ECHILD
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17, %13
br label %9
22: ; preds = %17
store i32 -1, i32* %3
br label %36
23: ; preds = %9
%24 = load i32, i32* @my_read.read_cnt
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 0, i32* %3
br label %36
27: ; preds = %23
br label %28
28: ; preds = %27
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @my_read.read_buf, i64 0, i64 0), i8** @my_read.read_ptr
br label %29
29: ; preds = %28, %2
%30 = load i32, i32* @my_read.read_cnt
%31 = add nsw i32 %30, -1
store i32 %31, i32* @my_read.read_cnt
%32 = load i8*, i8** @my_read.read_ptr
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** @my_read.read_ptr
%34 = load i8, i8* %32
%35 = load i8*, i8** %5
store i8 %34, i8* %35
store i32 1, i32* %3
br label %36
36: ; preds = %29, %26, %22
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@my_read.read_cnt = internal unnamed_addr global i32 0
@my_read.read_ptr = internal unnamed_addr global i8* null
@my_read.read_buf = internal global [1024 x i8] zeroinitializer
@errno = global i64 0
@EINTR = global i64 0
@ECHILD = global i64 0
define i32 @my_read(i32 %0, i8* nocapture writeonly %1) {
%3 = load i32, i32* @my_read.read_cnt
%4 = icmp slt i32 %3, 1
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = load i8*, i8** @my_read.read_ptr
br label %19
7: ; preds = %2, %10
%8 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @my_read.read_buf, i64 0, i64 0), i32 1024)
store i32 %8, i32* @my_read.read_cnt
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = load i64, i64* @ECHILD
%15 = icmp eq i64 %11, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %7, label %25
17: ; preds = %7
%18 = icmp eq i32 %8, 0
br i1 %18, label %25, label %19
19: ; preds = %17, %5
%20 = phi i8* [ %6, %5 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @my_read.read_buf, i64 0, i64 0), %17 ]
%21 = phi i32 [ %3, %5 ], [ %8, %17 ]
%22 = add nsw i32 %21, -1
store i32 %22, i32* @my_read.read_cnt
%23 = getelementptr inbounds i8, i8* %20, i64 1
store i8* %23, i8** @my_read.read_ptr
%24 = load i8, i8* %20
store i8 %24, i8* %1
br label %25
25: ; preds = %10, %17, %19
%26 = phi i32 [ 1, %19 ], [ 0, %17 ], [ -1, %10 ]
ret i32 %26
}