#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*/ PTRACE_POKEDATA;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, unsigned long long *,
                  unsigned long long);
int /*<<< orphan*/ strcpy(unsigned long long *, char *);
int strlen(char *);
int cpy_str(pid_t _pid, char *str, unsigned long long int *p) {
  int i;
  int len = strlen(str);
  int len1 = (len / 8) + 1;
  char *aux = malloc(len1 * 8);
  unsigned long long int *d = (unsigned long long int *)aux;
  printf("!! Reallocating %d to %d bytes\n", len, len1);
  memset(aux, 0, len1);
  strcpy(d, str);
  for (i = 0; i < len1 + 1; i++) {
    if ((ptrace(PTRACE_POKEDATA, _pid, p, *d)) < 0)
      perror("POKE Stack:");
    p++;
    d++;
  }
  free(aux);
  return len;
}


x86 assembly (clang, -O0):
<cpy_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
shl    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xe2c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1264 <cpy_str+0xe4>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rax
mov    (%rax),%rcx
call   1090 <ptrace@plt>
cmp    $0x0,%rax
jge    123e <cpy_str+0xbe>
lea    0xde7(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11fb <cpy_str+0x7b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpy_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,(%rsp)
mov    %edi,%r15d
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
lea    0x7(%r14),%ebp
test   %eax,%eax
cmovns %eax,%ebp
sar    $0x3,%ebp
lea    0x8(,%rbp,8),%edi
add    $0x1,%ebp
call   1080 <malloc@plt>
mov    %rax,%r12
lea    0xe3b(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1060 <printf@plt>
xor    %r13d,%r13d
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <memset@plt>
mov    %r12,%rdi
mov    (%rsp),%rsi
call   1040 <strcpy@plt>
mov    %r14,(%rsp)
cmp    $0xfffffff1,%r14d
jl     1242 <cpy_str+0xc2>
test   %ebp,%ebp
cmovle %r13d,%ebp
add    $0x1,%rbp
mov    0x2ddb(%rip),%r14
jmp    121d <cpy_str+0x9d>
nopw   0x0(%rax,%rax,1)
add    $0x8,%rbx
add    $0x1,%r13
cmp    %r13d,%ebp
je     1242 <cpy_str+0xc2>
mov    (%r14),%edi
mov    (%r12,%r13,8),%rcx
mov    %r15d,%esi
mov    %rbx,%rdx
call   1090 <ptrace@plt>
test   %rax,%rax
jns    1210 <cpy_str+0x90>
lea    0xde5(%rip),%rdi
call   10a0 <perror@plt>
jmp    1210 <cpy_str+0x90>
mov    %r12,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"!! Reallocating %d to %d bytes\0A\00"
@PTRACE_POKEDATA = global i32 0
@.str.1 = [12 x i8] c"POKE Stack:\00"
define i32 @cpy_str(i32 %0, i8* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64* %2, i64** %6
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = sdiv i32 %14, 8
%16 = add nsw i32 %15, 1
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = mul nsw i32 %17, 8
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %10
%20 = load i8*, i8** %10
%21 = bitcast i8* %20 to i64*
store i64* %21, i64** %11
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i8*, i8** %10
%26 = load i32, i32* %9
%27 = call i32 @memset(i8* %25, i32 0, i32 %26)
%28 = load i64*, i64** %11
%29 = load i8*, i8** %5
%30 = call i32 @strcpy(i64* %28, i8* %29)
store i32 0, i32* %7
br label %31
31: ; preds = %51, %3
%32 = load i32, i32* %7
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
%35 = icmp slt i32 %32, %34
br i1 %35, label %36, label %54
36: ; preds = %31
%37 = load i32, i32* @PTRACE_POKEDATA
%38 = load i32, i32* %4
%39 = load i64*, i64** %6
%40 = load i64*, i64** %11
%41 = load i64, i64* %40
%42 = call i64 @ptrace(i32 %37, i32 %38, i64* %39, i64 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %44, %36
%47 = load i64*, i64** %6
%48 = getelementptr inbounds i64, i64* %47, i32 1
store i64* %48, i64** %6
%49 = load i64*, i64** %11
%50 = getelementptr inbounds i64, i64* %49, i32 1
store i64* %50, i64** %11
br label %51
51: ; preds = %46
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %31
54: ; preds = %31
%55 = load i8*, i8** %10
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %8
ret i32 %57
}

LLVM IR (-O2):
@.str = [32 x i8] c"!! Reallocating %d to %d bytes\0A\00"
@PTRACE_POKEDATA = global i32 0
@.str.1 = [12 x i8] c"POKE Stack:\00"
define i32 @cpy_str(i32 %0, i8* %1, i64* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = sdiv i32 %4, 8
%6 = add nsw i32 %5, 1
%7 = shl nsw i32 %6, 3
%8 = tail call i8* @malloc(i32 %7)
%9 = bitcast i8* %8 to i64*
%10 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %6)
%11 = tail call i32 @memset(i8* %8, i32 0, i32 %6)
%12 = tail call i32 @strcpy(i64* %9, i8* %1)
%13 = icmp sgt i32 %4, -16
br i1 %13, label %14, label %31
14: ; preds = %3
%15 = call i32 @llvm.smax.i32(i32 %6, i32 0)
br label %16
16: ; preds = %14, %26
%17 = phi i64* [ %28, %26 ], [ %9, %14 ]
%18 = phi i64* [ %27, %26 ], [ %2, %14 ]
%19 = phi i32 [ %29, %26 ], [ 0, %14 ]
%20 = load i32, i32* @PTRACE_POKEDATA
%21 = load i64, i64* %17
%22 = tail call i64 @ptrace(i32 %20, i32 %0, i64* %18, i64 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %16
%27 = getelementptr inbounds i64, i64* %18, i64 1
%28 = getelementptr inbounds i64, i64* %17, i64 1
%29 = add nuw nsw i32 %19, 1
%30 = icmp eq i32 %19, %15
br i1 %30, label %31, label %16
31: ; preds = %26, %3
%32 = tail call i32 @free(i8* %8)
ret i32 %4
}