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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKDATA;
int /*<<< orphan*/ memcpy(char *, char *, int);
void *ptrace(int /*<<< orphan*/, int /*<<< orphan*/, long,
             int /*<<< orphan*/ *);
void getdata(pid_t child, long addr, char *str, int len) {
  char *laddr;
  int i, j;
  union u {
    long val;
    char chars[sizeof(long)];
  } data;
  i = 0;
  j = len / sizeof(long);
  laddr = str;
  while (i < j) {
    data.val = ptrace(PTRACE_PEEKDATA, child, addr + i * 4, NULL);
    memcpy(laddr, data.chars, sizeof(long));
    ++i;
    laddr += sizeof(long);
  }
  j = len % sizeof(long);
  if (j != 0) {
    data.val = ptrace(PTRACE_PEEKDATA, child, addr + i * 4, NULL);
    memcpy(laddr, data.chars, j);
  }
  str[len] = '\0';
}


x86 assembly (clang, -O0):
<getdata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x1c(%rbp),%rax
shr    $0x3,%rax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    11b0 <getdata+0x90>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    1150 <getdata+0x30>
movslq -0x1c(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     11fd <getdata+0xdd>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r15d
mov    %rdx,%rbp
mov    %edi,%ebx
cmp    $0x8,%ecx
mov    %rdx,0x20(%rsp)
jl     11b6 <getdata+0x96>
mov    %r15d,0xc(%rsp)
mov    %r15d,%r13d
shr    $0x3,%r13d
mov    0x2e8d(%rip),%r12
mov    %rsp,%r15
mov    %r13,0x10(%rsp)
mov    %rsi,0x18(%rsp)
mov    %rsi,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%edi
mov    %ebx,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,(%rsp)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
add    $0x8,%rbp
add    $0x4,%r14
add    $0xffffffffffffffff,%r13
jne    1170 <getdata+0x50>
mov    0x10(%rsp),%rcx
shl    $0x2,%ecx
mov    0xc(%rsp),%r15d
mov    0x18(%rsp),%rsi
jmp    11b8 <getdata+0x98>
xor    %ecx,%ecx
mov    %r15d,%r14d
and    $0x7,%r14d
je     11eb <getdata+0xcb>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
add    %rsi,%rcx
mov    %ebx,%esi
mov    %rcx,%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
call   1030 <memcpy@plt>
movslq %r15d,%rax
mov    0x20(%rsp),%rcx
movb   $0x0,(%rcx,%rax,1)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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