#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*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PTRACE_PEEKDATA;
int /*<<< orphan*/ PTRACE_POKEDATA;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, long);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *, long);
void write_heap(pid_t pid, unsigned value, unsigned *addr32) {
  void *addr;
  long ret;
  unsigned offset;
  addr = (void *)((size_t)addr32 & ~(sizeof(long) - 1));
  offset = (size_t)addr32 & (sizeof(long) - 1);
  errno = 0;
  ret = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
  if (errno) {
    err(EXIT_FAILURE, "ptrace (PEEKDATA)");
  } else {
    printf("Old value: 0x%016zx\n", ret);
    ret = ret & ~(0x00000000ffffffff << offset) | (value << offset);
    printf("New value: 0x%016zx\n\n", ret);
    if (ptrace(PTRACE_POKEDATA, pid, addr, ret) < 0)
      err(EXIT_FAILURE, "ptrace (POKEDATA)");
  }
}


x86 assembly (clang, -O0):
<write_heap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
and    $0xfffffffffffffff8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x24(%rbp)
mov    0x2e78(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <ptrace@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
cmpq   $0x0,(%rax)
je     11af <write_heap+0x7f>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1040 <err@plt>
jmp    1237 <write_heap+0x107>
mov    -0x20(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    $0xffffffff,%edx
shl    %cl,%edx
mov    %edx,%ecx
xor    $0xffffffff,%ecx
mov    %ecx,%ecx
and    %rcx,%rax
mov    -0x8(%rbp),%edx
mov    -0x24(%rbp),%ecx
shl    %cl,%edx
mov    %edx,%ecx
mov    %ecx,%ecx
or     %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <ptrace@plt>
cmp    $0x0,%rax
jge    1232 <write_heap+0x102>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1040 <err@plt>
jmp    1237 <write_heap+0x107>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_heap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rdx,%rbx
and    $0xfffffffffffffff8,%rbx
mov    0x2e86(%rip),%r13
movq   $0x0,0x0(%r13)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
cmpq   $0x0,0x0(%r13)
je     119a <write_heap+0x6a>
lea    0xe82(%rip),%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <err@plt>
mov    %rax,%r12
and    $0x7,%ebp
lea    0xe6b(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
mov    %ebp,%ecx
shl    %cl,%eax
not    %eax
and    %eax,%r12d
shl    %cl,%r14d
or     %r12d,%r14d
lea    0xe5d(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <ptrace@plt>
test   %rax,%rax
js     11fd <write_heap+0xcd>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe39(%rip),%rsi
jmp    117e <write_heap+0x4e>

LLVM IR (-O0):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [18 x i8] c"ptrace (PEEKDATA)\00"
@.str.1 = [21 x i8] c"Old value: 0x%016zx\0A\00"
@.str.2 = [22 x i8] c"New value: 0x%016zx\0A\0A\00"
@PTRACE_POKEDATA = global i32 0
@.str.3 = [18 x i8] c"ptrace (POKEDATA)\00"
define void @write_heap(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%10 = load i32*, i32** %6
%11 = ptrtoint i32* %10 to i64
%12 = and i64 %11, -8
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %7
%14 = load i32*, i32** %6
%15 = ptrtoint i32* %14 to i64
%16 = and i64 %15, 7
%17 = trunc i64 %16 to i32
store i32 %17, i32* %9
store i64 0, i64* @errno
%18 = load i32, i32* @PTRACE_PEEKDATA
%19 = load i32, i32* %4
%20 = load i8*, i8** %7
%21 = call i64 @ptrace(i32 %18, i32 %19, i8* %20, i64 0)
store i64 %21, i64* %8
%22 = load i64, i64* @errno
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %3
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @err(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %53
27: ; preds = %3
%28 = load i64, i64* %8
%29 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %28)
%30 = load i64, i64* %8
%31 = load i32, i32* %9
%32 = shl i32 -1, %31
%33 = xor i32 %32, -1
%34 = zext i32 %33 to i64
%35 = and i64 %30, %34
%36 = load i32, i32* %5
%37 = load i32, i32* %9
%38 = shl i32 %36, %37
%39 = zext i32 %38 to i64
%40 = or i64 %35, %39
store i64 %40, i64* %8
%41 = load i64, i64* %8
%42 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %41)
%43 = load i32, i32* @PTRACE_POKEDATA
%44 = load i32, i32* %4
%45 = load i8*, i8** %7
%46 = load i64, i64* %8
%47 = call i64 @ptrace(i32 %43, i32 %44, i8* %45, i64 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %27
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @err(i32 %50, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %49, %27
br label %53
53: ; preds = %52, %24
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [18 x i8] c"ptrace (PEEKDATA)\00"
@.str.1 = [21 x i8] c"Old value: 0x%016zx\0A\00"
@.str.2 = [22 x i8] c"New value: 0x%016zx\0A\0A\00"
@PTRACE_POKEDATA = global i32 0
@.str.3 = [18 x i8] c"ptrace (POKEDATA)\00"
define void @write_heap(i32 %0, i32 %1, i32* %2) {
%4 = ptrtoint i32* %2 to i64
%5 = and i64 %4, -8
%6 = inttoptr i64 %5 to i8*
%7 = trunc i64 %4 to i32
%8 = and i32 %7, 7
store i64 0, i64* @errno
%9 = load i32, i32* @PTRACE_PEEKDATA
%10 = tail call i64 @ptrace(i32 %9, i32 %0, i8* %6, i64 0)
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %3
%14 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %10)
%15 = shl nsw i32 -1, %8
%16 = xor i32 %15, -1
%17 = zext i32 %16 to i64
%18 = and i64 %10, %17
%19 = shl i32 %1, %8
%20 = zext i32 %19 to i64
%21 = or i64 %18, %20
%22 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%23 = load i32, i32* @PTRACE_POKEDATA
%24 = tail call i64 @ptrace(i32 %23, i32 %0, i8* %6, i64 %21)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %13, %3
%27 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), %13 ]
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @err(i32 %28, i8* %27)
br label %30
30: ; preds = %26, %13
ret void
}