#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 uint64_t;
struct file_stats {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ check_sector(char *, int, struct file_stats *);
uint64_t check_buffer(char *buf, size_t size, uint64_t expected_offset,
                      struct file_stats *stats) {
  char *beyond_buf = buf + size;
  assert(size % (512) == 0);
  while (buf < beyond_buf) {
    check_sector(buf, expected_offset, stats);
    buf += (512);
    expected_offset += (512);
  }
  return expected_offset;
}


x86 assembly (clang, -O0):
<check_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
and    $0x1ff,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    119a <check_buffer+0x7a>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <check_sector@plt>
mov    -0x8(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x200,%eax
mov    %eax,-0x14(%rbp)
jmp    115e <check_buffer+0x3e>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
test   $0x1ff,%r15d
sete   %dil
call   1030 <assert@plt>
test   %r15,%r15
jle    116f <check_buffer+0x4f>
add    %rbx,%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1040 <check_sector@plt>
add    $0x200,%rbx
add    $0x200,%ebp
cmp    %r15,%rbx
jb     1150 <check_buffer+0x30>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.file_stats = type { i32 }
define i32 @check_buffer(i8* %0, i64 %1, i32 %2, %struct.file_stats* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.file_stats*
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store %struct.file_stats* %3, %struct.file_stats** %8
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = getelementptr inbounds i8, i8* %10, i64 %11
store i8* %12, i8** %9
%13 = load i64, i64* %6
%14 = urem i64 %13, 512
%15 = icmp eq i64 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
br label %18
18: ; preds = %22, %4
%19 = load i8*, i8** %5
%20 = load i8*, i8** %9
%21 = icmp ult i8* %19, %20
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = load %struct.file_stats*, %struct.file_stats** %8
%26 = call i32 @check_sector(i8* %23, i32 %24, %struct.file_stats* %25)
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 512
store i8* %28, i8** %5
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 512
store i32 %30, i32* %7
br label %18
31: ; preds = %18
%32 = load i32, i32* %7
ret i32 %32
}

LLVM IR (-O2):
%struct.file_stats = type { i32 }
define i32 @check_buffer(i8* %0, i64 %1, i32 %2, %struct.file_stats* %3) {
%5 = getelementptr inbounds i8, i8* %0, i64 %1
%6 = and i64 %1, 511
%7 = icmp eq i64 %6, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp sgt i64 %1, 0
br i1 %10, label %11, label %18
11: ; preds = %4, %11
%12 = phi i8* [ %15, %11 ], [ %0, %4 ]
%13 = phi i32 [ %16, %11 ], [ %2, %4 ]
%14 = tail call i32 @check_sector(i8* %12, i32 %13, %struct.file_stats* %3)
%15 = getelementptr inbounds i8, i8* %12, i64 512
%16 = add nsw i32 %13, 512
%17 = icmp ult i8* %15, %5
br i1 %17, label %11, label %18
18: ; preds = %11, %4
%19 = phi i32 [ %2, %4 ], [ %16, %11 ]
ret i32 %19
}