#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*/ tmp;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKDATA;
scalar_t__ errno;
int /*<<< orphan*/ memcpy(void *, unsigned long *, size_t);
unsigned long ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                     unsigned long long);
int ftrace_peek_buffer(pid_t child, unsigned long long addr, size_t n,
                       void *buffer) {
  unsigned long tmp;
  size_t readb;
  size_t written;
  readb = 0;
  while (readb < n) {
    errno = 0;
    tmp = ptrace(PTRACE_PEEKDATA, child, addr + readb);
    if (errno)
      return (-1);
    if ((n - readb) / sizeof(tmp))
      written = sizeof(tmp);
    else
      written = n % sizeof(tmp);
    memcpy((void *)((uintptr_t)buffer + readb), &tmp, written);
    readb += written;
  }
  return (0);
}


x86 assembly (clang, -O0):
<ftrace_peek_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11ea <ftrace_peek_buffer+0xca>
mov    0x2e8c(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    -0x30(%rbp),%rdx
call   1040 <ptrace@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e61(%rip),%rax
cmpq   $0x0,(%rax)
je     1195 <ftrace_peek_buffer+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <ftrace_peek_buffer+0xd1>
mov    -0x18(%rbp),%rax
sub    -0x30(%rbp),%rax
shr    $0x3,%rax
cmp    $0x0,%rax
je     11b8 <ftrace_peek_buffer+0x98>
movq   $0x8,-0x38(%rbp)
jmp    11c4 <ftrace_peek_buffer+0xa4>
mov    -0x18(%rbp),%rax
and    $0x7,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    -0x28(%rbp),%rsi
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    113f <ftrace_peek_buffer+0x1f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftrace_peek_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,(%rsp)
mov    %rsi,0x8(%rsp)
test   %rdx,%rdx
je     11bc <ftrace_peek_buffer+0x9c>
mov    %rdx,%rbx
mov    %edi,%r13d
mov    %ebx,%r12d
and    $0x7,%r12d
xor    %r14d,%r14d
mov    0x2e89(%rip),%r15
nopw   0x0(%rax,%rax,1)
movq   $0x0,(%r15)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rax
lea    (%r14,%rax,1),%rdx
mov    %r13d,%esi
call   1040 <ptrace@plt>
mov    %rax,0x10(%rsp)
cmpq   $0x0,(%r15)
jne    11c0 <ftrace_peek_buffer+0xa0>
mov    %rbx,%rax
sub    %r14,%rax
cmp    $0x8,%rax
mov    $0x8,%ebp
cmovb  %r12,%rbp
mov    (%rsp),%rax
lea    (%r14,%rax,1),%rdi
lea    0x10(%rsp),%rsi
mov    %rbp,%rdx
call   1030 <memcpy@plt>
add    %rbp,%r14
cmp    %rbx,%r14
jb     1160 <ftrace_peek_buffer+0x40>
xor    %eax,%eax
jmp    11c5 <ftrace_peek_buffer+0xa5>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
define i32 @ftrace_peek_buffer(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 0, i64* %11
br label %13
13: ; preds = %37, %4
%14 = load i64, i64* %11
%15 = load i64, i64* %8
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %48
17: ; preds = %13
store i64 0, i64* @errno
%18 = load i32, i32* @PTRACE_PEEKDATA
%19 = load i32, i32* %6
%20 = load i64, i64* %7
%21 = load i64, i64* %11
%22 = add i64 %20, %21
%23 = call i64 @ptrace(i32 %18, i32 %19, i64 %22)
store i64 %23, i64* %10
%24 = load i64, i64* @errno
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %17
store i32 -1, i32* %5
br label %49
27: ; preds = %17
%28 = load i64, i64* %8
%29 = load i64, i64* %11
%30 = sub i64 %28, %29
%31 = udiv i64 %30, 8
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
store i64 8, i64* %12
br label %37
34: ; preds = %27
%35 = load i64, i64* %8
%36 = urem i64 %35, 8
store i64 %36, i64* %12
br label %37
37: ; preds = %34, %33
%38 = load i8*, i8** %9
%39 = ptrtoint i8* %38 to i64
%40 = load i64, i64* %11
%41 = add i64 %39, %40
%42 = inttoptr i64 %41 to i8*
%43 = load i64, i64* %12
%44 = call i32 @memcpy(i8* %42, i64* %10, i64 %43)
%45 = load i64, i64* %12
%46 = load i64, i64* %11
%47 = add i64 %46, %45
store i64 %47, i64* %11
br label %13
48: ; preds = %13
store i32 0, i32* %5
br label %49
49: ; preds = %48, %26
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
define i32 @ftrace_peek_buffer(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = icmp eq i64 %2, 0
br i1 %7, label %27, label %8
8: ; preds = %4
%9 = and i64 %2, 7
%10 = ptrtoint i8* %3 to i64
br label %11
11: ; preds = %8, %18
%12 = phi i64 [ 0, %8 ], [ %25, %18 ]
store i64 0, i64* @errno
%13 = load i32, i32* @PTRACE_PEEKDATA
%14 = add i64 %12, %1
%15 = call i64 @ptrace(i32 %13, i32 %0, i64 %14)
store i64 %15, i64* %5
%16 = load i64, i64* @errno
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %11
%19 = sub i64 %2, %12
%20 = icmp ult i64 %19, 8
%21 = select i1 %20, i64 %9, i64 8
%22 = add i64 %12, %10
%23 = inttoptr i64 %22 to i8*
%24 = call i32 @memcpy(i8* %23, i64* %5, i64 %21)
%25 = add i64 %21, %12
%26 = icmp ult i64 %25, %2
br i1 %26, label %11, label %27
27: ; preds = %11, %18, %4
%28 = phi i32 [ 0, %4 ], [ 0, %18 ], [ -1, %11 ]
ret i32 %28
}