#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_POKEDATA;
int /*<<< orphan*/ PTRACE_POKETEXT;
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, long, long);
int putDataIntoProc(pid_t child, long addr, char *str, int len) {
  char *laddr = str;
  int i = 0, j = len / sizeof(long);
  union u {
    long val;
    char chars[sizeof(long)];
  } data;
  while (i < j) {
    memcpy(data.chars, laddr, sizeof(long));
    ptrace(PTRACE_POKEDATA, child, addr + i * 4, data.val);
    i++;
    laddr += sizeof(long);
  }
  j = len % sizeof(long);
  if (j != 0) {
    memcpy(data.chars, laddr, j);
    ptrace(PTRACE_POKETEXT, child, addr + i * 4, data.val);
  }
  return 0;
}


x86 assembly (clang, -O0):
<putDataIntoProc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x1c(%rbp),%rax
shr    $0x3,%rax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    11ac <putDataIntoProc+0x8c>
lea    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdx
mov    -0x38(%rbp),%rcx
call   1040 <ptrace@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 <putDataIntoProc+0x30>
movslq -0x1c(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     11f5 <putDataIntoProc+0xd5>
lea    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <memcpy@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdx
mov    -0x38(%rbp),%rcx
call   1040 <ptrace@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putDataIntoProc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %edi,%ebx
cmp    $0x8,%ecx
jl     11aa <putDataIntoProc+0x8a>
mov    %ecx,0x14(%rsp)
mov    %ecx,%r15d
shr    $0x3,%r15d
lea    0x8(%rsp),%r13
mov    0x2e8e(%rip),%r12
mov    %r15,0x18(%rsp)
mov    %r14,0x20(%rsp)
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    (%r12),%edi
mov    0x8(%rsp),%rcx
mov    %ebx,%esi
mov    %r14,%rdx
call   1040 <ptrace@plt>
add    $0x8,%rbp
add    $0x4,%r14
add    $0xffffffffffffffff,%r15
jne    1160 <putDataIntoProc+0x40>
mov    0x18(%rsp),%r15
shl    $0x2,%r15d
mov    0x20(%rsp),%r14
mov    0x14(%rsp),%ecx
and    $0x7,%ecx
jne    11b2 <putDataIntoProc+0x92>
jmp    11dc <putDataIntoProc+0xbc>
xor    %r15d,%r15d
and    $0x7,%ecx
je     11dc <putDataIntoProc+0xbc>
lea    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    %ecx,%edx
call   1030 <memcpy@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
add    %r14,%r15
mov    0x8(%rsp),%rcx
mov    %ebx,%esi
mov    %r15,%rdx
call   1040 <ptrace@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.u = type { i64 }
@PTRACE_POKEDATA = global i32 0
@PTRACE_POKETEXT = global i32 0
define i32 @putDataIntoProc(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
%13 = load i8*, i8** %7
store i8* %13, i8** %9
store i32 0, i32* %10
%14 = load i32, i32* %8
%15 = sext i32 %14 to i64
%16 = udiv i64 %15, 8
%17 = trunc i64 %16 to i32
store i32 %17, i32* %11
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 = bitcast %union.u* %12 to [8 x i8]*
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %23, i64 0, i64 0
%25 = load i8*, i8** %9
%26 = call i32 @memcpy(i8* %24, i8* %25, i32 8)
%27 = load i32, i32* @PTRACE_POKEDATA
%28 = load i32, i32* %5
%29 = load i64, i64* %6
%30 = load i32, i32* %10
%31 = mul nsw i32 %30, 4
%32 = sext i32 %31 to i64
%33 = add nsw i64 %29, %32
%34 = bitcast %union.u* %12 to i64*
%35 = load i64, i64* %34
%36 = call i32 @ptrace(i32 %27, i32 %28, i64 %33, i64 %35)
%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 = bitcast %union.u* %12 to [8 x i8]*
%50 = getelementptr inbounds [8 x i8], [8 x i8]* %49, i64 0, i64 0
%51 = load i8*, i8** %9
%52 = load i32, i32* %11
%53 = call i32 @memcpy(i8* %50, i8* %51, i32 %52)
%54 = load i32, i32* @PTRACE_POKETEXT
%55 = load i32, i32* %5
%56 = load i64, i64* %6
%57 = load i32, i32* %10
%58 = mul nsw i32 %57, 4
%59 = sext i32 %58 to i64
%60 = add nsw i64 %56, %59
%61 = bitcast %union.u* %12 to i64*
%62 = load i64, i64* %61
%63 = call i32 @ptrace(i32 %54, i32 %55, i64 %60, i64 %62)
br label %64
64: ; preds = %48, %41
ret i32 0
}

LLVM IR (-O2):
%union.u = type { i64 }
@PTRACE_POKEDATA = global i32 0
@PTRACE_POKETEXT = global i32 0
define i32 @putDataIntoProc(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 = call i32 @memcpy(i8* %6, i8* %14, i32 8)
%16 = load i32, i32* @PTRACE_POKEDATA
%17 = shl nsw i64 %13, 2
%18 = add nsw i64 %17, %1
%19 = load i64, i64* %10
%20 = call i32 @ptrace(i32 %16, i32 %0, i64 %18, i64 %19)
%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 = call i32 @memcpy(i8* %6, i8* %28, i32 %30)
%34 = load i32, i32* @PTRACE_POKETEXT
%35 = add nsw i64 %29, %1
%36 = getelementptr inbounds %union.u, %union.u* %5, i64 0, i32 0
%37 = load i64, i64* %36
%38 = call i32 @ptrace(i32 %34, i32 %0, i64 %35, i64 %37)
br label %39
39: ; preds = %32, %27
ret i32 0
}