#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*/ errno;
int pread(int, char **, size_t, int);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
void read_file(int fd, int index, int size) {
  char buff[size];
  size_t nbytes = size;
  off_t offset = index * nbytes;
  int error;
  error = pread(fd, &buff, nbytes, offset);
  if (error < 0)
    printf("ERROR: %s\n", strerror(errno));
  else {
    printf("%s", buff);
  }
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
movslq -0xc(%rbp),%rax
mov    %rax,-0x28(%rbp)
movslq -0x8(%rbp),%rax
imul   -0x28(%rbp),%rax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1040 <pread@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11b9 <read_file+0x89>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
jmp    11c9 <read_file+0x99>
mov    -0x38(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    %esi,%ecx
mov    %edx,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movslq %edx,%rdx
imul   %edx,%ecx
mov    %rbx,%rsi
call   1040 <pread@plt>
test   %eax,%eax
js     1173 <read_file+0x43>
lea    0xea7(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe78(%rip),%rdi
mov    %rax,%rsi
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"ERROR: %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define void @read_file(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %6
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
store i64 %17, i64* %9
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = load i64, i64* %9
%21 = mul i64 %19, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %10
%23 = load i32, i32* %4
%24 = bitcast i8* %15 to i8**
%25 = load i64, i64* %9
%26 = load i32, i32* %10
%27 = call i32 @pread(i32 %23, i8** %24, i64 %25, i32 %26)
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %3
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %32)
br label %36
34: ; preds = %3
%35 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %36
36: ; preds = %34, %30
%37 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"ERROR: %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define void @read_file(i32 %0, i32 %1, i32 %2) {
%4 = zext i32 %2 to i64
%5 = alloca i8, i64 %4
%6 = sext i32 %2 to i64
%7 = mul i32 %2, %1
%8 = bitcast i8* %5 to i8**
%9 = call i32 @pread(i32 %0, i8** %8, i64 %6, i32 %7)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %3
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %17
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %17
17: ; preds = %15, %11
ret void
}