#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 scalar_t__ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int EINTR;
int EIO;
int /*<<< orphan*/ SEEK_SET;
int errno;
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
scalar_t__ read(int, char *, size_t);
void dread_c(int fd, char *buffer, off_t offset, size_t length, int *iostat) {
  ssize_t nread;
  if ((off_t)lseek(fd, (off_t)(offset), SEEK_SET) < 0) {
    *iostat = errno;
    return;
  }
  while (length) {
    nread = read(fd, buffer, length);
    if (nread < 0) {
      if (errno == EINTR)
        nread = 0;
      else {
        *iostat = errno;
        return;
      }
    } else if (nread == 0) {
      *iostat = EIO;
      return;
    }
    length -= nread;
  }
}


x86 assembly (clang, -O0):
<dread_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    116e <dread_c+0x4e>
mov    0x2e77(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1213 <dread_c+0xf3>
jmp    1173 <dread_c+0x53>
cmpq   $0x0,-0x20(%rbp)
je     1213 <dread_c+0xf3>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11db <dread_c+0xbb>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%eax
jne    11c2 <dread_c+0xa2>
movq   $0x0,-0x30(%rbp)
jmp    11d6 <dread_c+0xb6>
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1213 <dread_c+0xf3>
jmp    11ff <dread_c+0xdf>
cmpq   $0x0,-0x30(%rbp)
jne    11fa <dread_c+0xda>
mov    0x2df3(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1213 <dread_c+0xf3>
jmp    11ff <dread_c+0xdf>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1173 <dread_c+0x53>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dread_c>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %rdx,%rsi
mov    %eax,%edx
call   1030 <lseek@plt>
test   %rax,%rax
js     1197 <dread_c+0x77>
test   %rbx,%rbx
je     11ac <dread_c+0x8c>
mov    0x2e7e(%rip),%r12
mov    0x2e67(%rip),%r13
jmp    1177 <dread_c+0x57>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
je     11a0 <dread_c+0x80>
sub    %rax,%rbx
je     11ac <dread_c+0x8c>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
test   %rax,%rax
jns    1170 <dread_c+0x50>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
jne    11a9 <dread_c+0x89>
xor    %eax,%eax
jmp    1172 <dread_c+0x52>
mov    0x2e3a(%rip),%rax
jmp    11a7 <dread_c+0x87>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EIO = global i32 0
define void @dread_c(i32 %0, i8* %1, i64 %2, i64 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i32* %4, i32** %10
%12 = load i32, i32* %6
%13 = load i64, i64* %8
%14 = load i32, i32* @SEEK_SET
%15 = call i64 @lseek(i32 %12, i64 %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = load i32, i32* @errno
%19 = load i32*, i32** %10
store i32 %18, i32* %19
br label %51
20: ; preds = %5
br label %21
21: ; preds = %47, %20
%22 = load i64, i64* %9
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %51
24: ; preds = %21
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = load i64, i64* %9
%28 = call i64 @read(i32 %25, i8* %26, i64 %27)
store i64 %28, i64* %11
%29 = load i64, i64* %11
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %24
%32 = load i32, i32* @errno
%33 = load i32, i32* @EINTR
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i64 0, i64* %11
br label %39
36: ; preds = %31
%37 = load i32, i32* @errno
%38 = load i32*, i32** %10
store i32 %37, i32* %38
br label %51
39: ; preds = %35
br label %47
40: ; preds = %24
%41 = load i64, i64* %11
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* @EIO
%45 = load i32*, i32** %10
store i32 %44, i32* %45
br label %51
46: ; preds = %40
br label %47
47: ; preds = %46, %39
%48 = load i64, i64* %11
%49 = load i64, i64* %9
%50 = sub i64 %49, %48
store i64 %50, i64* %9
br label %21
51: ; preds = %17, %36, %43, %21
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EIO = global i32 0
define void @dread_c(i32 %0, i8* nocapture %1, i64 %2, i64 %3, i32* nocapture writeonly %4) {
%6 = load i32, i32* @SEEK_SET
%7 = tail call i64 @lseek(i32 %0, i64 %2, i32 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = icmp eq i64 %3, 0
br i1 %10, label %31, label %13
11: ; preds = %5
%12 = load i32, i32* @errno
br label %29
13: ; preds = %9, %25
%14 = phi i64 [ %27, %25 ], [ %3, %9 ]
%15 = tail call i64 @read(i32 %0, i8* %1, i64 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = load i32, i32* @EINTR
%20 = icmp eq i32 %18, %19
br i1 %20, label %25, label %29
21: ; preds = %13
%22 = icmp eq i64 %15, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = load i32, i32* @EIO
br label %29
25: ; preds = %17, %21
%26 = phi i64 [ %15, %21 ], [ 0, %17 ]
%27 = sub i64 %14, %26
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %13
29: ; preds = %17, %11, %23
%30 = phi i32 [ %24, %23 ], [ %12, %11 ], [ %18, %17 ]
store i32 %30, i32* %4
br label %31
31: ; preds = %25, %29, %9
ret void
}