#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 */
struct device {
  int dummy;
};

/* Variables and functions */
int bcache_read_block(struct device *, char *, int);
int device_block_size(struct device *);
int bcache_read(struct device *device, char *data, int blocks, int offset) {
  int i, r;
  int count = 0;
  int bs = device_block_size(device);
  for (i = 0; i < blocks; i++) {
    r = bcache_read_block(device, &data[i * bs], offset + i);
    if (r < 1)
      break;
    count++;
  }
  if (count > 0) {
    return count;
  } else {
    return r;
  }
}


x86 assembly (clang, -O0):
<bcache_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <device_block_size@plt>
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11a4 <bcache_read+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%eax
imul   -0x30(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x20(%rbp),%edx
add    -0x24(%rbp),%edx
call   1040 <bcache_read_block@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jge    118d <bcache_read+0x6d>
jmp    11a4 <bcache_read+0x84>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1150 <bcache_read+0x30>
cmpl   $0x0,-0x2c(%rbp)
jle    11b9 <bcache_read+0x99>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <bcache_read+0x9f>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcache_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <device_block_size@plt>
test   %ebp,%ebp
jle    1179 <bcache_read+0x59>
mov    %ebp,%r12d
movslq %eax,%r13
mov    %ebx,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%rbp,1),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <bcache_read_block@plt>
test   %eax,%eax
jle    1171 <bcache_read+0x51>
add    $0x1,%rbx
add    %r13,%r14
cmp    %rbx,%r12
jne    1150 <bcache_read+0x30>
mov    %r12d,%ebx
test   %ebx,%ebx
jne    1179 <bcache_read+0x59>
mov    %eax,%ebx
jmp    1179 <bcache_read+0x59>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.device = type { i32 }
define i32 @bcache_read(%struct.device* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.device*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store %struct.device* %0, %struct.device** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %12
%14 = load %struct.device*, %struct.device** %6
%15 = call i32 @device_block_size(%struct.device* %14)
store i32 %15, i32* %13
store i32 0, i32* %10
br label %16
16: ; preds = %38, %4
%17 = load i32, i32* %10
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %41
20: ; preds = %16
%21 = load %struct.device*, %struct.device** %6
%22 = load i8*, i8** %7
%23 = load i32, i32* %10
%24 = load i32, i32* %13
%25 = mul nsw i32 %23, %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %22, i64 %26
%28 = load i32, i32* %9
%29 = load i32, i32* %10
%30 = add nsw i32 %28, %29
%31 = call i32 @bcache_read_block(%struct.device* %21, i8* %27, i32 %30)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %20
br label %41
35: ; preds = %20
%36 = load i32, i32* %12
%37 = add nsw i32 %36, 1
store i32 %37, i32* %12
br label %38
38: ; preds = %35
%39 = load i32, i32* %10
%40 = add nsw i32 %39, 1
store i32 %40, i32* %10
br label %16
41: ; preds = %34, %16
%42 = load i32, i32* %12
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = load i32, i32* %12
store i32 %45, i32* %5
br label %48
46: ; preds = %41
%47 = load i32, i32* %11
store i32 %47, i32* %5
br label %48
48: ; preds = %46, %44
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.device = type { i32 }
define i32 @bcache_read(%struct.device* %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @device_block_size(%struct.device* %0)
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %26
7: ; preds = %4
%8 = sext i32 %5 to i64
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %7, %18
%11 = phi i64 [ 0, %7 ], [ %19, %18 ]
%12 = mul nsw i64 %11, %8
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = trunc i64 %11 to i32
%15 = add i32 %14, %3
%16 = tail call i32 @bcache_read_block(%struct.device* %0, i8* %13, i32 %15)
%17 = icmp slt i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %10
%19 = add nuw nsw i64 %11, 1
%20 = icmp eq i64 %19, %9
br i1 %20, label %23, label %10
21: ; preds = %10
%22 = trunc i64 %11 to i32
br label %23
23: ; preds = %18, %21
%24 = phi i32 [ %22, %21 ], [ %2, %18 ]
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %4, %23
%27 = phi i32 [ %16, %23 ], [ undef, %4 ]
br label %28
28: ; preds = %23, %26
%29 = phi i32 [ %27, %26 ], [ %24, %23 ]
ret i32 %29
}