#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*/ hdd_image_read(int /*<<< orphan*/, scalar_t__, scalar_t__,
                                  int /*<<< orphan*/ *);
scalar_t__ hdd_sectors(int /*<<< orphan*/);
int hdd_image_read_ex(uint8_t id, uint32_t sector, uint32_t count,
                      uint8_t *buffer) {
  uint32_t transfer_sectors = count;
  uint32_t sectors = hdd_sectors(id);
  if ((sectors - sector) < transfer_sectors)
    transfer_sectors = sectors - sector;
  hdd_image_read(id, sector, transfer_sectors, buffer);
  if (count != transfer_sectors)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<hdd_image_read_ex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <hdd_sectors@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    1169 <hdd_image_read_ex+0x49>
mov    -0x30(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <hdd_image_read@plt>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1197 <hdd_image_read_ex+0x77>
movl   $0x1,-0x4(%rbp)
jmp    119e <hdd_image_read_ex+0x7e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hdd_image_read_ex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
call   1040 <hdd_sectors@plt>
sub    %r15,%rax
xor    %ebp,%ebp
cmp    %rbx,%rax
setl   %bpl
cmovl  %rax,%rbx
mov    %r12d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <hdd_image_read@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hdd_image_read_ex(i32 %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%12 = load i64, i64* %8
store i64 %12, i64* %10
%13 = load i32, i32* %6
%14 = call i64 @hdd_sectors(i32 %13)
store i64 %14, i64* %11
%15 = load i64, i64* %11
%16 = load i64, i64* %7
%17 = sub nsw i64 %15, %16
%18 = load i64, i64* %10
%19 = icmp slt i64 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = load i64, i64* %11
%22 = load i64, i64* %7
%23 = sub nsw i64 %21, %22
store i64 %23, i64* %10
br label %24
24: ; preds = %20, %4
%25 = load i32, i32* %6
%26 = load i64, i64* %7
%27 = load i64, i64* %10
%28 = load i32*, i32** %9
%29 = call i32 @hdd_image_read(i32 %25, i64 %26, i64 %27, i32* %28)
%30 = load i64, i64* %8
%31 = load i64, i64* %10
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %24
store i32 1, i32* %5
br label %35
34: ; preds = %24
store i32 0, i32* %5
br label %35
35: ; preds = %34, %33
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
define i32 @hdd_image_read_ex(i32 %0, i64 %1, i64 %2, i32* %3) {
%5 = tail call i64 @hdd_sectors(i32 %0)
%6 = sub nsw i64 %5, %1
%7 = icmp slt i64 %6, %2
%8 = select i1 %7, i64 %6, i64 %2
%9 = tail call i32 @hdd_image_read(i32 %0, i64 %1, i64 %8, i32* %3)
%10 = zext i1 %7 to i32
ret i32 %10
}