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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ err_printf(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
unsigned char *malloc(size_t);
int read(int, unsigned char *, size_t);
int /*<<< orphan*/ strerror(scalar_t__);
int read_file_block(int fd, off_t offset, size_t len, void **dest) {
  unsigned char *buffer;
  size_t sofar;
  int r;
  if ((buffer = malloc(len)) == NULL) {
    err_printf("read_file_block: malloc failed\n");
    return 1;
  }
  if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
    err_printf("read_file_block: %s\n", strerror(errno));
    free(buffer);
    return 1;
  }
  sofar = 0;
  while (sofar != len) {
    r = read(fd, buffer + sofar, len - sofar);
    if (r < 0) {
      if (errno == EINTR)
        continue;
      err_printf("read_file_block: %s\n", strerror(errno));
      free(buffer);
      return 1;
    } else if (r == 0) {
      err_printf("read_file_block: unexpected EOF\n");
      free(buffer);
      return 1;
    }
    sofar += r;
  }
  *dest = buffer;
  return 0;
}


x86 assembly (clang, -O0):
<read_file_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a8 <read_file_block+0x48>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1080 <err_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <read_file_block+0x17e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11fb <read_file_block+0x9b>
mov    0x2e12(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1080 <err_printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <read_file_block+0x17e>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     12cc <read_file_block+0x16c>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1289 <read_file_block+0x129>
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
jne    1255 <read_file_block+0xf5>
jmp    1203 <read_file_block+0xa3>
mov    0x2d84(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1080 <err_printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <read_file_block+0x17e>
cmpl   $0x0,-0x34(%rbp)
jne    12b6 <read_file_block+0x156>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1080 <err_printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <read_file_block+0x17e>
jmp    12bb <read_file_block+0x15b>
movslq -0x34(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1203 <read_file_block+0xa3>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r13d
mov    %rdx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     122a <read_file_block+0xca>
mov    %rax,%rbp
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %r15,%rsi
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     123a <read_file_block+0xda>
mov    %r14,0x10(%rsp)
mov    %rbp,(%rsp)
test   %rbx,%rbx
je     121a <read_file_block+0xba>
mov    %rbx,%rax
xor    %r14d,%r14d
mov    0x2e19(%rip),%r15
mov    0x2e02(%rip),%r12
mov    (%rsp),%rbp
mov    %rbx,0x8(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
test   %eax,%eax
jns    11fd <read_file_block+0x9d>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     11e0 <read_file_block+0x80>
jmp    125e <read_file_block+0xfe>
je     1290 <read_file_block+0x130>
mov    %eax,%eax
add    %rax,%r14
mov    (%rsp),%rax
lea    (%rax,%r14,1),%rbp
mov    0x8(%rsp),%rbx
sub    %r14,%rbx
jne    11e0 <read_file_block+0x80>
mov    (%rsp),%rax
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
xor    %eax,%eax
jmp    1281 <read_file_block+0x121>
lea    0xdcf(%rip),%rdi
xor    %eax,%eax
call   1080 <err_printf@plt>
jmp    127c <read_file_block+0x11c>
mov    0x2d9f(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xdd0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <err_printf@plt>
mov    %rbp,%rdi
jmp    1277 <read_file_block+0x117>
call   1070 <strerror@plt>
lea    0xdb6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <err_printf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
xor    %eax,%eax
call   1080 <err_printf@plt>
jmp    1273 <read_file_block+0x113>

LLVM IR (-O0):
@.str = [32 x i8] c"read_file_block: malloc failed\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [21 x i8] c"read_file_block: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [33 x i8] c"read_file_block: unexpected EOF\0A\00"
define i32 @read_file_block(i32 %0, i64 %1, i64 %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8** %3, i8*** %9
%13 = load i64, i64* %8
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %10
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %73
18: ; preds = %4
%19 = load i32, i32* %6
%20 = load i64, i64* %7
%21 = load i32, i32* @SEEK_SET
%22 = call i64 @lseek(i32 %19, i64 %20, i32 %21)
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i64, i64* @errno
%26 = call i32 @strerror(i64 %25)
%27 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %10
%29 = call i32 @free(i8* %28)
store i32 1, i32* %5
br label %73
30: ; preds = %18
store i64 0, i64* %11
br label %31
31: ; preds = %65, %50, %30
%32 = load i64, i64* %11
%33 = load i64, i64* %8
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %70
35: ; preds = %31
%36 = load i32, i32* %6
%37 = load i8*, i8** %10
%38 = load i64, i64* %11
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = load i64, i64* %8
%41 = load i64, i64* %11
%42 = sub i64 %40, %41
%43 = call i32 @read(i32 %36, i8* %39, i64 %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %35
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %31
51: ; preds = %46
%52 = load i64, i64* @errno
%53 = call i32 @strerror(i64 %52)
%54 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %53)
%55 = load i8*, i8** %10
%56 = call i32 @free(i8* %55)
store i32 1, i32* %5
br label %73
57: ; preds = %35
%58 = load i32, i32* %12
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%62 = load i8*, i8** %10
%63 = call i32 @free(i8* %62)
store i32 1, i32* %5
br label %73
64: ; preds = %57
br label %65
65: ; preds = %64
%66 = load i32, i32* %12
%67 = sext i32 %66 to i64
%68 = load i64, i64* %11
%69 = add i64 %68, %67
store i64 %69, i64* %11
br label %31
70: ; preds = %31
%71 = load i8*, i8** %10
%72 = load i8**, i8*** %9
store i8* %71, i8** %72
store i32 0, i32* %5
br label %73
73: ; preds = %70, %60, %51, %24, %16
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
@.str = [32 x i8] c"read_file_block: malloc failed\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [21 x i8] c"read_file_block: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [33 x i8] c"read_file_block: unexpected EOF\0A\00"
define i32 @read_file_block(i32 %0, i64 %1, i64 %2, i8** nocapture writeonly %3) {
%5 = tail call i8* @malloc(i64 %2)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %47
9: ; preds = %4
%10 = load i32, i32* @SEEK_SET
%11 = tail call i64 @lseek(i32 %0, i64 %1, i32 %10)
%12 = icmp eq i64 %11, -1
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = icmp eq i64 %2, 0
br i1 %14, label %46, label %42
15: ; preds = %9
%16 = load i64, i64* @errno
%17 = tail call i32 @strerror(i64 %16)
%18 = tail call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = tail call i32 @free(i8* %5)
br label %47
20: ; preds = %42, %23
%21 = tail call i32 @read(i32 %0, i8* %44, i64 %43)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %20, label %27
27: ; preds = %23
%28 = tail call i32 @strerror(i64 %24)
%29 = tail call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = tail call i32 @free(i8* %5)
br label %47
31: ; preds = %20
%32 = icmp eq i32 %21, 0
%33 = zext i32 %21 to i64
%34 = add i64 %45, %33
br i1 %32, label %35, label %38
35: ; preds = %31
%36 = tail call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%37 = tail call i32 @free(i8* %5)
br label %47
38: ; preds = %31
%39 = icmp eq i64 %34, %2
%40 = getelementptr inbounds i8, i8* %5, i64 %34
%41 = sub i64 %2, %34
br i1 %39, label %46, label %42
42: ; preds = %13, %38
%43 = phi i64 [ %41, %38 ], [ %2, %13 ]
%44 = phi i8* [ %40, %38 ], [ %5, %13 ]
%45 = phi i64 [ %34, %38 ], [ 0, %13 ]
br label %20
46: ; preds = %38, %13
store i8* %5, i8** %3
br label %47
47: ; preds = %46, %35, %27, %15, %7
%48 = phi i32 [ 1, %7 ], [ 1, %15 ], [ 1, %27 ], [ 1, %35 ], [ 0, %46 ]
ret i32 %48
}