#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*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, size_t);
unsigned long proc_read_word(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int proc_read_bytes(pid_t pid, char *address, void *buf, size_t num_bytes) {
  char *bufp = buf;
  size_t lim = num_bytes / sizeof(long);
  if (num_bytes % sizeof(long))
    ++lim;
  fprintf(stderr, "num_bytes / lim: %zu / %zu\n", num_bytes, lim);
  for (size_t i = 0; i < lim; i++) {
    *(unsigned long *)(bufp + i * sizeof(long)) =
        proc_read_word(pid, address + i * sizeof(long));
  }
  return 0;
}


x86 assembly (clang, -O0):
<proc_read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
je     1169 <proc_read_bytes+0x49>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    0xe7f(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    11d7 <proc_read_bytes+0xb7>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   1040 <proc_read_word@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x3,%rcx
mov    %rdx,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    118e <proc_read_bytes+0x6e>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_read_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rcx,%r12
shr    $0x3,%r12
xor    %ebp,%ebp
test   $0x7,%cl
setne  %bpl
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
lea    0xeaf(%rip),%rsi
lea    (%r12,%rbp,1),%rax
mov    %rcx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
add    %rbp,%r12
je     118c <proc_read_bytes+0x6c>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <proc_read_word@plt>
mov    %rax,(%r14,%rbp,8)
add    $0x1,%rbp
add    $0x8,%rbx
cmp    %rbp,%r12
jne    1170 <proc_read_bytes+0x50>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"num_bytes / lim: %zu / %zu\0A\00"
define i32 @proc_read_bytes(i32 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%12 = load i8*, i8** %7
store i8* %12, i8** %9
%13 = load i64, i64* %8
%14 = udiv i64 %13, 8
store i64 %14, i64* %10
%15 = load i64, i64* %8
%16 = urem i64 %15, 8
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = load i64, i64* %10
%20 = add i64 %19, 1
store i64 %20, i64* %10
br label %21
21: ; preds = %18, %4
%22 = load i32, i32* @stderr
%23 = load i64, i64* %8
%24 = load i64, i64* %10
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %23, i64 %24)
store i64 0, i64* %11
br label %26
26: ; preds = %42, %21
%27 = load i64, i64* %11
%28 = load i64, i64* %10
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i32, i32* %5
%32 = load i8*, i8** %6
%33 = load i64, i64* %11
%34 = mul i64 %33, 8
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = call i64 @proc_read_word(i32 %31, i8* %35)
%37 = load i8*, i8** %9
%38 = load i64, i64* %11
%39 = mul i64 %38, 8
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = bitcast i8* %40 to i64*
store i64 %36, i64* %41
br label %42
42: ; preds = %30
%43 = load i64, i64* %11
%44 = add i64 %43, 1
store i64 %44, i64* %11
br label %26
45: ; preds = %26
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"num_bytes / lim: %zu / %zu\0A\00"
define i32 @proc_read_bytes(i32 %0, i8* %1, i8* nocapture writeonly %2, i64 %3) {
%5 = lshr i64 %3, 3
%6 = and i64 %3, 7
%7 = icmp ne i64 %6, 0
%8 = zext i1 %7 to i64
%9 = add nuw nsw i64 %5, %8
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %3, i64 %9)
%12 = icmp eq i64 %9, 0
br i1 %12, label %13, label %14
13: ; preds = %14, %4
ret i32 0
14: ; preds = %4, %14
%15 = phi i64 [ %21, %14 ], [ 0, %4 ]
%16 = shl i64 %15, 3
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = tail call i64 @proc_read_word(i32 %0, i8* %17)
%19 = getelementptr inbounds i8, i8* %2, i64 %16
%20 = bitcast i8* %19 to i64*
store i64 %18, i64* %20
%21 = add nuw nsw i64 %15, 1
%22 = icmp eq i64 %21, %9
br i1 %22, label %13, label %14
}