#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*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, void *, int);
int msx_readdata(char *fn, void *into, int max, int size) {
  int fd = open(fn, 0);
  int r;
  if (fd == -1)
    return (0);
  r = read(fd, into, max * size);
  close(fd);
  if (r == -1 || (r % size))
    return (0);
  return (r / size);
}


x86 assembly (clang, -O0):
<msx_readdata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    116a <msx_readdata+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    11b8 <msx_readdata+0x88>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
imul   -0x20(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
cmpl   $0xffffffff,-0x28(%rbp)
je     11a2 <msx_readdata+0x72>
mov    -0x28(%rbp),%eax
cltd
idivl  -0x20(%rbp)
cmp    $0x0,%edx
je     11ae <msx_readdata+0x7e>
movl   $0x0,-0x4(%rbp)
jmp    11b8 <msx_readdata+0x88>
mov    -0x28(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msx_readdata>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%r15
xor    %r12d,%r12d
xor    %esi,%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     117e <msx_readdata+0x4e>
mov    %eax,%ebx
imul   %r14d,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%ebp
je     117e <msx_readdata+0x4e>
mov    %ebp,%eax
cltd
idiv   %r14d
xor    %r12d,%r12d
test   %edx,%edx
cmove  %eax,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @msx_readdata(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = call i32 @open(i8* %12, i32 0)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 0, i32* %5
br label %38
17: ; preds = %4
%18 = load i32, i32* %10
%19 = load i8*, i8** %7
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = mul nsw i32 %20, %21
%23 = call i32 @read(i32 %18, i8* %19, i32 %22)
store i32 %23, i32* %11
%24 = load i32, i32* %10
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %11
%27 = icmp eq i32 %26, -1
br i1 %27, label %33, label %28
28: ; preds = %17
%29 = load i32, i32* %11
%30 = load i32, i32* %9
%31 = srem i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28, %17
store i32 0, i32* %5
br label %38
34: ; preds = %28
%35 = load i32, i32* %11
%36 = load i32, i32* %9
%37 = sdiv i32 %35, %36
store i32 %37, i32* %5
br label %38
38: ; preds = %34, %33, %16
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
define i32 @msx_readdata(i8* nocapture %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32 @open(i8* %0, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = mul nsw i32 %3, %2
%9 = tail call i32 @read(i32 %5, i8* %1, i32 %8)
%10 = tail call i32 @close(i32 %5)
%11 = icmp eq i32 %9, -1
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = srem i32 %9, %3
%14 = sdiv i32 %9, %3
%15 = icmp eq i32 %13, 0
%16 = select i1 %15, i32 %14, i32 0
br label %17
17: ; preds = %12, %7, %4
%18 = phi i32 [ 0, %4 ], [ 0, %7 ], [ %16, %12 ]
ret i32 %18
}