#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int *malloc(int);
int *readvalues(int count, int indices[], char *filename) {
  FILE *fp = fopen(filename, "r");
  int *results = malloc(sizeof(int) * count);
  int buffer[] = {0};
  int i;
  int readcount = 1;
  for (i = 0; i < count; i++) {
    int ok = fseek(fp, (long)indices[i] * 4, SEEK_SET);
    int c = fread(buffer, sizeof(int), readcount, fp);
    if (ok == 0 && c == readcount) {
      int value = buffer[0];
      if (value == -2147483648l)
        value = 99999;
      results[i] = value;
    }
  }
  fclose(fp);
  return results;
}


x86 assembly (clang, -O0):
<readvalues>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x1,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1255 <readvalues+0xf5>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
movslq (%rax,%rcx,4),%rsi
shl    $0x2,%rsi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    %eax,-0x38(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x4,%esi
call   1030 <fread@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    1242 <readvalues+0xe2>
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    1242 <readvalues+0xe2>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
movslq -0x40(%rbp),%rax
cmp    $0xffffffff80000000,%rax
jne    1234 <readvalues+0xd4>
movl   $0x1869f,-0x40(%rbp)
mov    -0x40(%rbp),%edx
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1247 <readvalues+0xe7>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11b8 <readvalues+0x58>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readvalues>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xe95(%rip),%rsi
mov    %rdx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0x0(,%r12,4),%edi
call   1050 <malloc@plt>
mov    %rax,0x10(%rsp)
movl   $0x0,0xc(%rsp)
test   %r12d,%r12d
jle    120e <readvalues+0xbe>
mov    %r12d,%r13d
xor    %r15d,%r15d
lea    0xc(%rsp),%r12
jmp    11b9 <readvalues+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r15
cmp    %r15,%r13
je     120e <readvalues+0xbe>
movslq (%r14,%r15,4),%rsi
shl    $0x2,%rsi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1060 <fseek@plt>
mov    %eax,%ebp
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %ebp,%ebp
jne    11b0 <readvalues+0x60>
cmp    $0x1,%eax
jne    11b0 <readvalues+0x60>
mov    0xc(%rsp),%eax
cmp    $0x80000000,%eax
mov    $0x1869f,%ecx
cmove  %ecx,%eax
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx,%r15,4)
jmp    11b0 <readvalues+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define i32* @readvalues(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [1 x i32]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%15 = load i8*, i8** %6
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %7
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = mul i64 4, %18
%20 = trunc i64 %19 to i32
%21 = call i32* @malloc(i32 %20)
store i32* %21, i32** %8
%22 = bitcast [1 x i32]* %9 to i8*
store i32 1, i32* %11
store i32 0, i32* %10
br label %23
23: ; preds = %62, %3
%24 = load i32, i32* %10
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %65
27: ; preds = %23
%28 = load i32*, i32** %7
%29 = load i32*, i32** %5
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = mul nsw i64 %34, 4
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %28, i64 %35, i32 %36)
store i32 %37, i32* %12
%38 = getelementptr inbounds [1 x i32], [1 x i32]* %9, i64 0, i64 0
%39 = load i32, i32* %11
%40 = load i32*, i32** %7
%41 = call i32 @fread(i32* %38, i32 4, i32 %39, i32* %40)
store i32 %41, i32* %13
%42 = load i32, i32* %12
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %61
44: ; preds = %27
%45 = load i32, i32* %13
%46 = load i32, i32* %11
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %61
48: ; preds = %44
%49 = getelementptr inbounds [1 x i32], [1 x i32]* %9, i64 0, i64 0
%50 = load i32, i32* %49
store i32 %50, i32* %14
%51 = load i32, i32* %14
%52 = sext i32 %51 to i64
%53 = icmp eq i64 %52, -2147483648
br i1 %53, label %54, label %55
54: ; preds = %48
store i32 99999, i32* %14
br label %55
55: ; preds = %54, %48
%56 = load i32, i32* %14
%57 = load i32*, i32** %8
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
store i32 %56, i32* %60
br label %61
61: ; preds = %55, %44, %27
br label %62
62: ; preds = %61
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %23
65: ; preds = %23
%66 = load i32*, i32** %7
%67 = call i32 @fclose(i32* %66)
%68 = load i32*, i32** %8
ret i32* %68
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define noalias i32* @readvalues(i32 %0, i32* nocapture %1, i8* nocapture %2) {
%4 = alloca [1 x i32]
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = shl i32 %0, 2
%7 = tail call i32* @malloc(i32 %6)
%8 = bitcast [1 x i32]* %4 to i8*
%9 = getelementptr inbounds [1 x i32], [1 x i32]* %4, i64 0, i64 0
store i32 0, i32* %9
%10 = icmp sgt i32 %0, 0
br i1 %10, label %11, label %33
11: ; preds = %3
%12 = zext i32 %0 to i64
br label %13
13: ; preds = %11, %30
%14 = phi i64 [ 0, %11 ], [ %31, %30 ]
%15 = getelementptr inbounds i32, i32* %1, i64 %14
%16 = load i32, i32* %15
%17 = sext i32 %16 to i64
%18 = shl nsw i64 %17, 2
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %5, i64 %18, i32 %19)
%21 = call i32 @fread(i32* %9, i32 4, i32 1, i32* %5)
%22 = icmp eq i32 %20, 0
%23 = icmp eq i32 %21, 1
%24 = and i1 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %13
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, -2147483648
%28 = select i1 %27, i32 99999, i32 %26
%29 = getelementptr inbounds i32, i32* %7, i64 %14
store i32 %28, i32* %29
br label %30
30: ; preds = %25, %13
%31 = add nuw nsw i64 %14, 1
%32 = icmp eq i64 %31, %12
br i1 %32, label %33, label %13
33: ; preds = %30, %3
%34 = tail call i32 @fclose(i32* %5)
ret i32* %7
}