#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*/ ulong;
typedef int /*<<< orphan*/ uint8_t;
typedef int uint32_t;
struct u {
  long val;
  char *chars;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, char *, int);
void *ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ const *,
             int /*<<< orphan*/);
int ptrace_readdata(pid_t pid, const uint8_t *src, uint8_t *buf, size_t size) {
  uint32_t i, j, remain;
  uint8_t *laddr;
  union u {
    long val;
    char chars[sizeof(long)];
  } d;
  j = size / sizeof(ulong);
  remain = size % sizeof(ulong);
  laddr = buf;
  for (i = 0; i < j; i++) {
    d.val = ptrace(PTRACE_PEEKTEXT, pid, src, 0);
    memcpy(laddr, d.chars, sizeof(ulong));
    src += sizeof(ulong);
    laddr += sizeof(ulong);
  }
  if (remain > 0) {
    d.val = ptrace(PTRACE_PEEKTEXT, pid, src, 0);
    memcpy(laddr, d.chars, remain);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_readdata>:
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    -0x20(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
and    $0x3,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    11bb <ptrace_readdata+0x9b>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115c <ptrace_readdata+0x3c>
cmpl   $0x0,-0x2c(%rbp)
jle    11f0 <ptrace_readdata+0xd0>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <memcpy@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_readdata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r13d
mov    %rcx,%rbp
shr    $0x2,%rbp
and    $0x3,%ecx
mov    %rcx,0x10(%rsp)
test   %ebp,%ebp
jle    1192 <ptrace_readdata+0x72>
mov    0x2e8f(%rip),%r14
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %r13d,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,0x8(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
add    $0x10,%rbx
add    $0x10,%r15
add    $0xffffffff,%ebp
jne    1160 <ptrace_readdata+0x40>
mov    0x10(%rsp),%rbp
test   %ebp,%ebp
je     11c5 <ptrace_readdata+0xa5>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    %r13d,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    %ebp,%edx
call   1030 <memcpy@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.u = type { i64 }
@PTRACE_PEEKTEXT = global i32 0
define i32 @ptrace_readdata(i32 %0, i32* %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca %union.u
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i64 %3, i64* %8
%14 = load i64, i64* %8
%15 = udiv i64 %14, 4
%16 = trunc i64 %15 to i32
store i32 %16, i32* %10
%17 = load i64, i64* %8
%18 = urem i64 %17, 4
%19 = trunc i64 %18 to i32
store i32 %19, i32* %11
%20 = load i32*, i32** %7
store i32* %20, i32** %12
store i32 0, i32* %9
br label %21
21: ; preds = %40, %4
%22 = load i32, i32* %9
%23 = load i32, i32* %10
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %43
25: ; preds = %21
%26 = load i32, i32* @PTRACE_PEEKTEXT
%27 = load i32, i32* %5
%28 = load i32*, i32** %6
%29 = call i8* @ptrace(i32 %26, i32 %27, i32* %28, i32 0)
%30 = ptrtoint i8* %29 to i64
%31 = bitcast %union.u* %13 to i64*
store i64 %30, i64* %31
%32 = load i32*, i32** %12
%33 = bitcast %union.u* %13 to [8 x i8]*
%34 = getelementptr inbounds [8 x i8], [8 x i8]* %33, i64 0, i64 0
%35 = call i32 @memcpy(i32* %32, i8* %34, i32 4)
%36 = load i32*, i32** %6
%37 = getelementptr inbounds i32, i32* %36, i64 4
store i32* %37, i32** %6
%38 = load i32*, i32** %12
%39 = getelementptr inbounds i32, i32* %38, i64 4
store i32* %39, i32** %12
br label %40
40: ; preds = %25
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %21
43: ; preds = %21
%44 = load i32, i32* %11
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %58
46: ; preds = %43
%47 = load i32, i32* @PTRACE_PEEKTEXT
%48 = load i32, i32* %5
%49 = load i32*, i32** %6
%50 = call i8* @ptrace(i32 %47, i32 %48, i32* %49, i32 0)
%51 = ptrtoint i8* %50 to i64
%52 = bitcast %union.u* %13 to i64*
store i64 %51, i64* %52
%53 = load i32*, i32** %12
%54 = bitcast %union.u* %13 to [8 x i8]*
%55 = getelementptr inbounds [8 x i8], [8 x i8]* %54, i64 0, i64 0
%56 = load i32, i32* %11
%57 = call i32 @memcpy(i32* %53, i8* %55, i32 %56)
br label %58
58: ; preds = %46, %43
ret i32 0
}

LLVM IR (-O2):
%union.u = type { i64 }
@PTRACE_PEEKTEXT = global i32 0
define i32 @ptrace_readdata(i32 %0, i32* %1, i32* %2, i64 %3) {
%5 = alloca %union.u
%6 = bitcast %union.u* %5 to i8*
%7 = lshr i64 %3, 2
%8 = trunc i64 %7 to i32
%9 = trunc i64 %3 to i32
%10 = and i32 %9, 3
%11 = icmp sgt i32 %8, 0
br i1 %11, label %12, label %26
12: ; preds = %4
%13 = getelementptr inbounds %union.u, %union.u* %5, i64 0, i32 0
br label %14
14: ; preds = %12, %14
%15 = phi i32* [ %2, %12 ], [ %23, %14 ]
%16 = phi i32* [ %1, %12 ], [ %22, %14 ]
%17 = phi i32 [ 0, %12 ], [ %24, %14 ]
%18 = load i32, i32* @PTRACE_PEEKTEXT
%19 = call i8* @ptrace(i32 %18, i32 %0, i32* %16, i32 0)
%20 = ptrtoint i8* %19 to i64
store i64 %20, i64* %13
%21 = call i32 @memcpy(i32* %15, i8* %6, i32 4)
%22 = getelementptr inbounds i32, i32* %16, i64 4
%23 = getelementptr inbounds i32, i32* %15, i64 4
%24 = add nuw nsw i32 %17, 1
%25 = icmp eq i32 %24, %8
br i1 %25, label %26, label %14
26: ; preds = %14, %4
%27 = phi i32* [ %1, %4 ], [ %22, %14 ]
%28 = phi i32* [ %2, %4 ], [ %23, %14 ]
%29 = icmp eq i32 %10, 0
br i1 %29, label %36, label %30
30: ; preds = %26
%31 = load i32, i32* @PTRACE_PEEKTEXT
%32 = call i8* @ptrace(i32 %31, i32 %0, i32* %27, i32 0)
%33 = ptrtoint i8* %32 to i64
%34 = getelementptr inbounds %union.u, %union.u* %5, i64 0, i32 0
store i64 %33, i64* %34
%35 = call i32 @memcpy(i32* %28, i8* %6, i32 %10)
br label %36
36: ; preds = %30, %26
ret i32 0
}