#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__ EINTR;
scalar_t__ errno;
int read(int, char *, int);
ssize_t my_read(int fd, char *ptr) {
  static int read_cnt;
  static char *read_ptr;
  static char read_buf[100];
  if (read_cnt <= 0) {
  again:
    if ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
      if (errno == EINTR)
        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     11ab <my_read+0x9b>
jmp    1131 <my_read+0x21>
mov    -0x8(%rbp),%edi
lea    0x2f15(%rip),%rsi
mov    $0x64,%edx
call   1030 <read@plt>
mov    %eax,0x2ef5(%rip)
cmp    $0x0,%eax
jge    117f <my_read+0x6f>
mov    0x2e85(%rip),%rax
mov    (%rax),%rax
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
jne    1173 <my_read+0x63>
jmp    1131 <my_read+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <my_read+0xce>
cmpl   $0x0,0x2eba(%rip)
jne    1198 <my_read+0x88>
movl   $0x0,-0x4(%rbp)
jmp    11de <my_read+0xce>
jmp    119d <my_read+0x8d>
lea    0x2eac(%rip),%rax
mov    %rax,0x2e9d(%rip)
mov    0x2e8f(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2e86(%rip)
mov    0x2e87(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x2e79(%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   %r12
push   %rbx
mov    %rsi,%r14
mov    0x2f1f(%rip),%eax
test   %eax,%eax
jle    1153 <my_read+0x43>
mov    0x2f1c(%rip),%rbx
add    $0xffffffff,%eax
mov    %eax,0x2f0b(%rip)
lea    0x1(%rbx),%rax
mov    %rax,0x2f08(%rip)
mov    (%rbx),%al
mov    %al,(%r14)
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %edi,%ebp
lea    0x2ef4(%rip),%rbx
mov    0x2e7d(%rip),%r15
mov    0x2e6e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1030 <read@plt>
mov    %eax,0x2ebb(%rip)
test   %eax,%eax
jns    1199 <my_read+0x89>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1170 <my_read+0x60>
mov    $0xffffffff,%eax
jmp    114a <my_read+0x3a>
jne    112c <my_read+0x1c>
xor    %eax,%eax
jmp    114a <my_read+0x3a>

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 [100 x i8] zeroinitializer
@errno = global i64 0
@EINTR = 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 %25
8: ; preds = %2
br label %9
9: ; preds = %17, %8
%10 = load i32, i32* %4
%11 = call i32 @read(i32 %10, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @my_read.read_buf, i64 0, i64 0), i32 100)
store i32 %11, i32* @my_read.read_cnt
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
br label %9
18: ; preds = %13
store i32 -1, i32* %3
br label %32
19: ; preds = %9
%20 = load i32, i32* @my_read.read_cnt
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 0, i32* %3
br label %32
23: ; preds = %19
br label %24
24: ; preds = %23
store i8* getelementptr inbounds ([100 x i8], [100 x i8]* @my_read.read_buf, i64 0, i64 0), i8** @my_read.read_ptr
br label %25
25: ; preds = %24, %2
%26 = load i32, i32* @my_read.read_cnt
%27 = add nsw i32 %26, -1
store i32 %27, i32* @my_read.read_cnt
%28 = load i8*, i8** @my_read.read_ptr
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** @my_read.read_ptr
%30 = load i8, i8* %28
%31 = load i8*, i8** %5
store i8 %30, i8* %31
store i32 1, i32* %3
br label %32
32: ; preds = %25, %22, %18
%33 = load i32, i32* %3
ret i32 %33
}

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 [100 x i8] zeroinitializer
@errno = global i64 0
@EINTR = 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 %16
7: ; preds = %2, %10
%8 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @my_read.read_buf, i64 0, i64 0), i32 100)
store i32 %8, i32* @my_read.read_cnt
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %7, label %22
14: ; preds = %7
%15 = icmp eq i32 %8, 0
br i1 %15, label %22, label %16
16: ; preds = %14, %5
%17 = phi i8* [ %6, %5 ], [ getelementptr inbounds ([100 x i8], [100 x i8]* @my_read.read_buf, i64 0, i64 0), %14 ]
%18 = phi i32 [ %3, %5 ], [ %8, %14 ]
%19 = add nsw i32 %18, -1
store i32 %19, i32* @my_read.read_cnt
%20 = getelementptr inbounds i8, i8* %17, i64 1
store i8* %20, i8** @my_read.read_ptr
%21 = load i8, i8* %17
store i8 %21, i8* %1
br label %22
22: ; preds = %10, %14, %16
%23 = phi i32 [ 1, %16 ], [ 0, %14 ], [ -1, %10 ]
ret i32 %23
}