#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, size_t, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, size_t, size_t);
size_t read_all(int, void *, size_t);
int read_log(const char *fname, void *buf, size_t off, size_t size) {
  int f = open(fname, O_RDONLY);
  lseek(f, off, SEEK_SET);
  size_t r = read_all(f, buf, size);
  close(f);
  if (r != size) {
    printf("Error: from %s read %zd bytes out of %zd\n", fname, r, size);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <read_all@plt>
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     11e0 <read_log+0x90>
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <read_log+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_log>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <lseek@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1070 <read_all@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
cmp    %r15,%rbx
je     11bf <read_log+0x6f>
lea    0xe54(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [42 x i8] c"Error: from %s read %zd bytes out of %zd\0A\00"
define i32 @read_log(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%12 = load i8*, i8** %6
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = load i64, i64* %8
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek(i32 %15, i64 %16, i32 %17)
%19 = load i32, i32* %10
%20 = load i8*, i8** %7
%21 = load i64, i64* %9
%22 = call i64 @read_all(i32 %19, i8* %20, i64 %21)
store i64 %22, i64* %11
%23 = load i32, i32* %10
%24 = call i32 @close(i32 %23)
%25 = load i64, i64* %11
%26 = load i64, i64* %9
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %4
%29 = load i8*, i8** %6
%30 = load i64, i64* %11
%31 = load i64, i64* %9
%32 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %29, i64 %30, i64 %31)
store i32 -1, i32* %5
br label %34
33: ; preds = %4
store i32 0, i32* %5
br label %34
34: ; preds = %33, %28
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [42 x i8] c"Error: from %s read %zd bytes out of %zd\0A\00"
define i32 @read_log(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @lseek(i32 %6, i64 %2, i32 %7)
%9 = tail call i64 @read_all(i32 %6, i8* %1, i64 %3)
%10 = tail call i32 @close(i32 %6)
%11 = icmp eq i64 %9, %3
br i1 %11, label %14, label %12
12: ; preds = %4
%13 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %9, i64 %3)
br label %14
14: ; preds = %4, %12
%15 = phi i32 [ -1, %12 ], [ 0, %4 ]
ret i32 %15
}