#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*/ uint8_t;
typedef scalar_t__ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ flashfsFlushSync();
scalar_t__ flashfsGetSize();
int m25p16_readBytes(scalar_t__, int /*<<< orphan*/ *, unsigned int);
int flashfsReadAbs(uint32_t address, uint8_t *buffer, unsigned int len) {
  int bytesRead;
  if (address + len > flashfsGetSize()) {
    len = flashfsGetSize() - address;
  }
  flashfsFlushSync();
  bytesRead = m25p16_readBytes(address, buffer, len);
  return bytesRead;
}


x86 assembly (clang, -O0):
<flashfsReadAbs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <flashfsGetSize@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jle    1176 <flashfsReadAbs+0x46>
mov    $0x0,%al
call   1040 <flashfsGetSize@plt>
sub    -0x8(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <flashfsFlushSync@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <m25p16_readBytes@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flashfsReadAbs>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%ebx
add    %rdi,%rbx
xor    %eax,%eax
call   1040 <flashfsGetSize@plt>
cmp    %rax,%rbx
jle    115f <flashfsReadAbs+0x2f>
xor    %eax,%eax
call   1040 <flashfsGetSize@plt>
mov    %rax,%r12
sub    %r15d,%r12d
xor    %eax,%eax
call   1030 <flashfsFlushSync@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <m25p16_readBytes@plt>

LLVM IR (-O0):
define i32 @flashfsReadAbs(i64 %0, i32* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i64 %0, i64* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%8 = load i64, i64* %4
%9 = load i32, i32* %6
%10 = zext i32 %9 to i64
%11 = add nsw i64 %8, %10
%12 = call i64 (...) @flashfsGetSize()
%13 = icmp sgt i64 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = call i64 (...) @flashfsGetSize()
%16 = load i64, i64* %4
%17 = sub nsw i64 %15, %16
%18 = trunc i64 %17 to i32
store i32 %18, i32* %6
br label %19
19: ; preds = %14, %3
%20 = call i32 (...) @flashfsFlushSync()
%21 = load i64, i64* %4
%22 = load i32*, i32** %5
%23 = load i32, i32* %6
%24 = call i32 @m25p16_readBytes(i64 %21, i32* %22, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
ret i32 %25
}

LLVM IR (-O2):
define i32 @flashfsReadAbs(i64 %0, i32* %1, i32 %2) {
%4 = zext i32 %2 to i64
%5 = add nsw i64 %4, %0
%6 = tail call i64 (...) @flashfsGetSize()
%7 = icmp sgt i64 %5, %6
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = tail call i64 (...) @flashfsGetSize()
%10 = sub nsw i64 %9, %0
%11 = trunc i64 %10 to i32
br label %12
12: ; preds = %8, %3
%13 = phi i32 [ %11, %8 ], [ %2, %3 ]
%14 = tail call i32 (...) @flashfsFlushSync()
%15 = tail call i32 @m25p16_readBytes(i64 %0, i32* %1, i32 %13)
ret i32 %15
}