#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*/ tmp;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKDATA;
scalar_t__ errno;
int /*<<< orphan*/ *memchr(unsigned long *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memcpy(char *, unsigned long *, int);
unsigned long ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                     unsigned long long);
int ftrace_peek_string_slice(pid_t child, unsigned long long int addr,
                             size_t readb, char **strp) {
  unsigned long tmp;
  errno = 0;
  tmp = ptrace(PTRACE_PEEKDATA, child, addr + readb);
  if (errno) {
    (*strp)[readb] = '\0';
    return (-1);
  }
  memcpy(*strp + readb, &tmp, sizeof(tmp));
  if (memchr(&tmp, 0, sizeof(tmp)) != NULL)
    return (1);
  return (0);
}


x86 assembly (clang, -O0):
<ftrace_peek_string_slice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e92(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    -0x18(%rbp),%rdx
call   1050 <ptrace@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e67(%rip),%rax
cmpq   $0x0,(%rax)
je     119e <ftrace_peek_string_slice+0x6e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <ftrace_peek_string_slice+0xb4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memchr@plt>
cmp    $0x0,%rax
je     11dd <ftrace_peek_string_slice+0xad>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <ftrace_peek_string_slice+0xb4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftrace_peek_string_slice>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %edi,%eax
mov    0x2e98(%rip),%r15
movq   $0x0,(%r15)
mov    0x2e82(%rip),%rcx
mov    (%rcx),%edi
add    %rsi,%rdx
mov    %eax,%esi
call   1050 <ptrace@plt>
mov    %rax,0x8(%rsp)
add    (%r14),%rbx
cmpq   $0x0,(%r15)
je     117a <ftrace_peek_string_slice+0x4a>
movb   $0x0,(%rbx)
mov    $0xffffffff,%eax
jmp    11a9 <ftrace_peek_string_slice+0x79>
lea    0x8(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memchr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
define i32 @ftrace_peek_string_slice(i32 %0, i64 %1, i64 %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8**
%10 = alloca i64
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8** %3, i8*** %9
store i64 0, i64* @errno
%11 = load i32, i32* @PTRACE_PEEKDATA
%12 = load i32, i32* %6
%13 = load i64, i64* %7
%14 = load i64, i64* %8
%15 = add i64 %13, %14
%16 = call i64 @ptrace(i32 %11, i32 %12, i64 %15)
store i64 %16, i64* %10
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %4
%20 = load i8**, i8*** %9
%21 = load i8*, i8** %20
%22 = load i64, i64* %8
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
store i32 -1, i32* %5
br label %34
24: ; preds = %4
%25 = load i8**, i8*** %9
%26 = load i8*, i8** %25
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = call i32 @memcpy(i8* %28, i64* %10, i32 8)
%30 = call i32* @memchr(i64* %10, i32 0, i32 8)
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %24
store i32 1, i32* %5
br label %34
33: ; preds = %24
store i32 0, i32* %5
br label %34
34: ; preds = %33, %32, %19
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
define i32 @ftrace_peek_string_slice(i32 %0, i64 %1, i64 %2, i8** nocapture %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
store i64 0, i64* @errno
%7 = load i32, i32* @PTRACE_PEEKDATA
%8 = add i64 %2, %1
%9 = tail call i64 @ptrace(i32 %7, i32 %0, i64 %8)
store i64 %9, i64* %5
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 %2
br i1 %11, label %15, label %14
14: ; preds = %4
store i8 0, i8* %13
br label %20
15: ; preds = %4
%16 = call i32 @memcpy(i8* %13, i64* %5, i32 8)
%17 = call i32* @memchr(i64* %5, i32 0, i32 8)
%18 = icmp ne i32* %17, null
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %15, %14
%21 = phi i32 [ -1, %14 ], [ %19, %15 ]
ret i32 %21
}