#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, size_t);
int /*<<< orphan*/ stderr;
int doread(int fd, off_t offset, size_t count, char *buf, int max_file_size) {
  int ret = 0;
  int was_read = 0;
  if (lseek(fd, offset, SEEK_SET) == -1) {
    perror("lseek failed");
    return -1;
  }
  while (count) {
    ret = read(fd, buf + offset + was_read, count);
    if (ret < 0)
      return -1;
    if (ret == 0)
      break;
    if (ret > count) {
      fprintf(stderr, "READ: read more than asked\n");
      return -1;
    }
    count -= ret;
    was_read += ret;
  }
  return ret;
}


x86 assembly (clang, -O0):
<doread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
jne    119d <doread+0x5d>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <doread+0x105>
jmp    11a2 <doread+0x62>
cmpq   $0x0,-0x18(%rbp)
je     123f <doread+0xff>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11e4 <doread+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <doread+0x105>
cmpl   $0x0,-0x28(%rbp)
jne    11f3 <doread+0xb3>
jmp    123f <doread+0xff>
movslq -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1222 <doread+0xe2>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <doread+0x105>
movslq -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11a2 <doread+0x62>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r15d
mov    0x2e7e(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
je     11c6 <doread+0x86>
test   %rbx,%rbx
je     11aa <doread+0x6a>
movslq %ebp,%rax
add    %rax,%r14
xor    %ebp,%ebp
mov    $0xffffffff,%r12d
nopl   0x0(%rax)
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
call   1040 <read@plt>
test   %eax,%eax
js     11d8 <doread+0x98>
je     11aa <doread+0x6a>
mov    %eax,%ecx
cmp    %rcx,%rbx
jb     11af <doread+0x6f>
add    %eax,%ebp
sub    %rcx,%rbx
jne    1180 <doread+0x40>
mov    %eax,%r12d
jmp    11d8 <doread+0x98>
xor    %r12d,%r12d
jmp    11d8 <doread+0x98>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11d8 <doread+0x98>
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [13 x i8] c"lseek failed\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"READ: read more than asked\0A\00"
define i32 @doread(i32 %0, i32 %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @lseek(i32 %14, i32 %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %5
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %61
21: ; preds = %5
br label %22
22: ; preds = %51, %21
%23 = load i64, i64* %9
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %59
25: ; preds = %22
%26 = load i32, i32* %7
%27 = load i8*, i8** %10
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %13
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i64, i64* %9
%35 = call i32 @read(i32 %26, i8* %33, i64 %34)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %25
store i32 -1, i32* %6
br label %61
39: ; preds = %25
%40 = load i32, i32* %12
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
br label %59
43: ; preds = %39
%44 = load i32, i32* %12
%45 = sext i32 %44 to i64
%46 = load i64, i64* %9
%47 = icmp ugt i64 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %61
51: ; preds = %43
%52 = load i32, i32* %12
%53 = sext i32 %52 to i64
%54 = load i64, i64* %9
%55 = sub i64 %54, %53
store i64 %55, i64* %9
%56 = load i32, i32* %12
%57 = load i32, i32* %13
%58 = add nsw i32 %57, %56
store i32 %58, i32* %13
br label %22
59: ; preds = %42, %22
%60 = load i32, i32* %12
store i32 %60, i32* %6
br label %61
61: ; preds = %59, %48, %38, %19
%62 = load i32, i32* %6
ret i32 %62
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [13 x i8] c"lseek failed\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"READ: read more than asked\0A\00"
define i32 @doread(i32 %0, i32 %1, i64 %2, i8* nocapture %3, i32 %4) {
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %0, i32 %1, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = icmp eq i64 %2, 0
br i1 %10, label %35, label %11
11: ; preds = %9
%12 = sext i32 %1 to i64
%13 = getelementptr inbounds i8, i8* %3, i64 %12
br label %16
14: ; preds = %5
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %35
16: ; preds = %11, %31
%17 = phi i32 [ 0, %11 ], [ %33, %31 ]
%18 = phi i64 [ %2, %11 ], [ %32, %31 ]
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8, i8* %13, i64 %19
%21 = tail call i32 @read(i32 %0, i8* %20, i64 %18)
%22 = icmp slt i32 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %16
%24 = icmp eq i32 %21, 0
br i1 %24, label %35, label %25
25: ; preds = %23
%26 = zext i32 %21 to i64
%27 = icmp ult i64 %18, %26
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %35
31: ; preds = %25
%32 = sub i64 %18, %26
%33 = add nsw i32 %21, %17
%34 = icmp eq i64 %32, 0
br i1 %34, label %35, label %16
35: ; preds = %16, %23, %31, %9, %28, %14
%36 = phi i32 [ -1, %14 ], [ -1, %28 ], [ 0, %9 ], [ -1, %16 ], [ 0, %23 ], [ %21, %31 ]
ret i32 %36
}