#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 read_wrapper(int fd, char *buf) {
  static int count = 0;
  static char *p;
  static char read_buf[256];
  if (count <= 0) {
    int keep_reading = 0;
    do {
      count = read(fd, read_buf, sizeof(read_buf));
      if (count < 0) {
        if (errno == EINTR)
          keep_reading = 1;
        else
          return -1;
      } else if (count == 0) {
        return 0;
      }
      p = read_buf;
    } while (keep_reading);
  }
  count--;
  *buf = *p++;
  return 1;
}


x86 assembly (clang, -O0):
<read_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,0x2f1a(%rip)
jg     11cc <read_wrapper+0xbc>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    0x2f13(%rip),%rsi
mov    $0x100,%edx
call   1030 <read@plt>
mov    %eax,0x2ef3(%rip)
cmpl   $0x0,0x2eec(%rip)
jge    1191 <read_wrapper+0x81>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <read_wrapper+0x70>
movl   $0x1,-0x14(%rbp)
jmp    118c <read_wrapper+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <read_wrapper+0xef>
jmp    11af <read_wrapper+0x9f>
cmpl   $0x0,0x2ea8(%rip)
jne    11aa <read_wrapper+0x9a>
movl   $0x0,-0x4(%rbp)
jmp    11ff <read_wrapper+0xef>
jmp    11af <read_wrapper+0x9f>
lea    0x2e9a(%rip),%rax
mov    %rax,0x2e8b(%rip)
cmpl   $0x0,-0x14(%rbp)
jne    1133 <read_wrapper+0x23>
jmp    11cc <read_wrapper+0xbc>
mov    0x2e6e(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2e65(%rip)
mov    0x2e66(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x2e58(%rip)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_wrapper>:
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 <read_wrapper+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,%r12d
xor    %ebp,%ebp
lea    0x2ee8(%rip),%rbx
mov    0x2e71(%rip),%r15
mov    0x2e62(%rip),%r13
jmp    119e <read_wrapper+0x8e>
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rcx
mov    $0x1,%ebp
cmp    0x0(%r13),%rcx
jne    11be <read_wrapper+0xae>
mov    %rbx,0x2eb3(%rip)
test   %ebp,%ebp
mov    $0x1,%ebp
je     112f <read_wrapper+0x1f>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1030 <read@plt>
mov    %eax,0x2e8c(%rip)
test   %eax,%eax
js     1180 <read_wrapper+0x70>
jne    118e <read_wrapper+0x7e>
xor    %eax,%eax
jmp    114d <read_wrapper+0x3d>
mov    $0xffffffff,%eax
jmp    114d <read_wrapper+0x3d>

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

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