#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 strace_read_buf(unsigned long long addr, pid_t child, size_t nbytes,
                    void *buffer) {
  unsigned long tmp;
  size_t readb;
  size_t written;
  readb = 0;
  while (readb < nbytes) {
    errno = 0;
    tmp = ptrace(PTRACE_PEEKDATA, child, addr + readb);
    if (errno)
      return (-1);
    if ((nbytes - readb) / sizeof(tmp))
      written = sizeof(tmp);
    else
      written = nbytes % sizeof(tmp);
    memcpy((void *)((uintptr_t)buffer + readb), &tmp, written);
    readb += written;
  }
  return (0);
}


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

x86 assembly (clang, -O2):
<strace_read_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x8(%rsp)
mov    %esi,0x4(%rsp)
test   %rdx,%rdx
je     11b9 <strace_read_buf+0x99>
mov    %rdx,%rbx
mov    %rdi,%r13
mov    %ebx,%r12d
and    $0x7,%r12d
xor    %r14d,%r14d
mov    0x2e8d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movq   $0x0,(%r15)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    (%r14,%r13,1),%rdx
mov    0x4(%rsp),%esi
call   1040 <ptrace@plt>
mov    %rax,0x10(%rsp)
cmpq   $0x0,(%r15)
jne    11bd <strace_read_buf+0x9d>
mov    %rbx,%rax
sub    %r14,%rax
cmp    $0x8,%rax
mov    $0x8,%ebp
cmovb  %r12,%rbp
mov    0x8(%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 <strace_read_buf+0x40>
xor    %eax,%eax
jmp    11c2 <strace_read_buf+0xa2>
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 @strace_read_buf(i64 %0, i32 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i64 %0, i64* %6
store i32 %1, i32* %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* %7
%20 = load i64, i64* %6
%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 @strace_read_buf(i64 %0, i32 %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, %0
%15 = call i64 @ptrace(i32 %13, i32 %1, 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
}