#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*/ FlipLong(unsigned char *);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, ...);
int /*<<< orphan*/ stderr;
void read_mnist_char(char *file_path, int num_data, int len_info, int arr_n,
                     unsigned char data_char[][arr_n], int info_arr[]) {
  int i, fd;
  unsigned char *ptr;
  if ((fd = open(file_path, O_RDONLY)) == -1) {
    fprintf(stderr, "couldn't open image file");
    exit(-1);
  }
  read(fd, info_arr, len_info * sizeof(int));
  for (i = 0; i < len_info; i++) {
    ptr = (unsigned char *)(info_arr + i);
    FlipLong(ptr);
    ptr = ptr + sizeof(int);
  }
  for (i = 0; i < num_data; i++) {
    read(fd, data_char[i], arr_n * sizeof(unsigned char));
  }
  close(fd);
}


x86 assembly (clang, -O0):
<read_mnist_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    11c1 <read_mnist_char+0x61>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x10(%rbp),%rdx
shl    $0x2,%rdx
mov    $0x0,%al
call   1040 <read@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1220 <read_mnist_char+0xc0>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <FlipLong@plt>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11de <read_mnist_char+0x7e>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1266 <read_mnist_char+0x106>
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x2c(%rbp),%rax
imul   %rcx,%rax
add    %rax,%rsi
movslq -0x14(%rbp),%rdx
shl    $0x0,%rdx
mov    $0x0,%al
call   1040 <read@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1227 <read_mnist_char+0xc7>
mov    -0x30(%rbp),%edi
call   1030 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mnist_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%rbp
mov    %r8,%r15
mov    %ecx,%r14d
mov    %edx,%r13d
mov    %esi,0x4(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     121f <read_mnist_char+0xbf>
mov    %eax,%r12d
movslq %r13d,%rbx
lea    0x0(,%rbx,4),%rdx
mov    %eax,%edi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <read@plt>
test   %ebx,%ebx
jle    11d2 <read_mnist_char+0x72>
mov    %r13d,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1060 <FlipLong@plt>
add    $0x4,%rbp
add    $0xffffffffffffffff,%rbx
jne    11c0 <read_mnist_char+0x60>
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    1209 <read_mnist_char+0xa9>
mov    %r14d,%r13d
movslq %r14d,%r14
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <read@plt>
add    %r13,%r15
add    $0xffffffffffffffff,%rbp
jne    11f0 <read_mnist_char+0x90>
mov    %r12d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"couldn't open image file\00"
define void @read_mnist_char(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32* %5) {
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i32* %5, i32** %12
%16 = load i32, i32* %10
%17 = zext i32 %16 to i64
%18 = load i8*, i8** %7
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %14
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %6
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %6
%27 = load i32, i32* %14
%28 = load i32*, i32** %12
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = mul i64 %30, 4
%32 = call i32 (i32, ...) @read(i32 %27, i32* %28, i64 %31)
store i32 0, i32* %13
br label %33
33: ; preds = %47, %26
%34 = load i32, i32* %13
%35 = load i32, i32* %9
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %50
37: ; preds = %33
%38 = load i32*, i32** %12
%39 = load i32, i32* %13
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = bitcast i32* %41 to i8*
store i8* %42, i8** %15
%43 = load i8*, i8** %15
%44 = call i32 @FlipLong(i8* %43)
%45 = load i8*, i8** %15
%46 = getelementptr inbounds i8, i8* %45, i64 4
store i8* %46, i8** %15
br label %47
47: ; preds = %37
%48 = load i32, i32* %13
%49 = add nsw i32 %48, 1
store i32 %49, i32* %13
br label %33
50: ; preds = %33
store i32 0, i32* %13
br label %51
51: ; preds = %66, %50
%52 = load i32, i32* %13
%53 = load i32, i32* %8
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %69
55: ; preds = %51
%56 = load i32, i32* %14
%57 = load i8*, i8** %11
%58 = load i32, i32* %13
%59 = sext i32 %58 to i64
%60 = mul nsw i64 %59, %17
%61 = getelementptr inbounds i8, i8* %57, i64 %60
%62 = load i32, i32* %10
%63 = sext i32 %62 to i64
%64 = mul i64 %63, 1
%65 = call i32 (i32, ...) @read(i32 %56, i8* %61, i64 %64)
br label %66
66: ; preds = %55
%67 = load i32, i32* %13
%68 = add nsw i32 %67, 1
store i32 %68, i32* %13
br label %51
69: ; preds = %51
%70 = load i32, i32* %14
%71 = call i32 @close(i32 %70)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"couldn't open image file\00"
define void @read_mnist_char(i8* nocapture %0, i32 %1, i32 %2, i32 %3, i8* %4, i32* %5) {
%7 = zext i32 %3 to i64
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %6
%16 = sext i32 %2 to i64
%17 = shl nsw i64 %16, 2
%18 = tail call i32 (i32, ...) @read(i32 %9, i32* %5, i64 %17)
%19 = icmp sgt i32 %2, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = zext i32 %2 to i64
br label %27
22: ; preds = %27, %15
%23 = icmp sgt i32 %1, 0
br i1 %23, label %24, label %41
24: ; preds = %22
%25 = sext i32 %3 to i64
%26 = zext i32 %1 to i64
br label %34
27: ; preds = %20, %27
%28 = phi i64 [ 0, %20 ], [ %32, %27 ]
%29 = getelementptr inbounds i32, i32* %5, i64 %28
%30 = bitcast i32* %29 to i8*
%31 = tail call i32 @FlipLong(i8* %30)
%32 = add nuw nsw i64 %28, 1
%33 = icmp eq i64 %32, %21
br i1 %33, label %22, label %27
34: ; preds = %24, %34
%35 = phi i64 [ 0, %24 ], [ %39, %34 ]
%36 = mul nuw nsw i64 %35, %7
%37 = getelementptr inbounds i8, i8* %4, i64 %36
%38 = tail call i32 (i32, ...) @read(i32 %9, i8* %37, i64 %25)
%39 = add nuw nsw i64 %35, 1
%40 = icmp eq i64 %39, %26
br i1 %40, label %41, label %34
41: ; preds = %34, %22
%42 = tail call i32 @close(i32 %9)
ret void
}