#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 */
scalar_t__ ftrace_peek_string_slice(int /*<<< orphan*/, unsigned long long,
                                    size_t, char **);
char *realloc(char *, size_t);
void ftrace_peek_string(pid_t child, unsigned long long int addr, char **strp) {
  size_t allocated;
  size_t readb;
  allocated = 16;
  readb = 0;
  if ((*strp = realloc(*strp, allocated)) == NULL)
    return;
  while (true) {
    if ((readb + sizeof(unsigned long)) > allocated) {
      allocated <<= 1;
      if ((*strp = realloc(*strp, allocated)) == NULL)
        return;
    }
    if (ftrace_peek_string_slice(child, addr, readb, strp))
      return;
    readb += sizeof(unsigned long);
  }
}


x86 assembly (clang, -O0):
<ftrace_peek_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x10,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <realloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1169 <ftrace_peek_string+0x49>
jmp    11eb <ftrace_peek_string+0xcb>
jmp    116e <ftrace_peek_string+0x4e>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
cmp    -0x20(%rbp),%rax
jbe    11b7 <ftrace_peek_string+0x97>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <realloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11b2 <ftrace_peek_string+0x92>
jmp    11eb <ftrace_peek_string+0xcb>
jmp    11b7 <ftrace_peek_string+0x97>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <ftrace_peek_string_slice@plt>
cmp    $0x0,%rax
je     11da <ftrace_peek_string+0xba>
jmp    11eb <ftrace_peek_string+0xcb>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    116e <ftrace_peek_string+0x4e>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftrace_peek_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
mov    (%rdx),%rdi
mov    $0x10,%ebp
mov    $0x10,%esi
call   1040 <realloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     119a <ftrace_peek_string+0x7a>
xor    %ebx,%ebx
jmp    1179 <ftrace_peek_string+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1030 <ftrace_peek_string_slice@plt>
mov    %r13,%rbx
test   %rax,%rax
jne    119a <ftrace_peek_string+0x7a>
lea    0x8(%rbx),%r13
cmp    %rbp,%r13
jbe    1160 <ftrace_peek_string+0x40>
add    %rbp,%rbp
mov    (%r12),%rdi
mov    %rbp,%rsi
call   1040 <realloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
jne    1160 <ftrace_peek_string+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @ftrace_peek_string(i32 %0, i64 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
store i8** %2, i8*** %6
store i64 16, i64* %7
store i64 0, i64* %8
%9 = load i8**, i8*** %6
%10 = load i8*, i8** %9
%11 = load i64, i64* %7
%12 = call i8* @realloc(i8* %10, i64 %11)
%13 = load i8**, i8*** %6
store i8* %12, i8** %13
%14 = icmp eq i8* %12, null
br i1 %14, label %15, label %16
15: ; preds = %3
br label %44
16: ; preds = %3
br label %17
17: ; preds = %16, %41
%18 = load i64, i64* %8
%19 = add i64 %18, 8
%20 = load i64, i64* %7
%21 = icmp ugt i64 %19, %20
br i1 %21, label %22, label %33
22: ; preds = %17
%23 = load i64, i64* %7
%24 = shl i64 %23, 1
store i64 %24, i64* %7
%25 = load i8**, i8*** %6
%26 = load i8*, i8** %25
%27 = load i64, i64* %7
%28 = call i8* @realloc(i8* %26, i64 %27)
%29 = load i8**, i8*** %6
store i8* %28, i8** %29
%30 = icmp eq i8* %28, null
br i1 %30, label %31, label %32
31: ; preds = %22
br label %44
32: ; preds = %22
br label %33
33: ; preds = %32, %17
%34 = load i32, i32* %4
%35 = load i64, i64* %5
%36 = load i64, i64* %8
%37 = load i8**, i8*** %6
%38 = call i64 @ftrace_peek_string_slice(i32 %34, i64 %35, i64 %36, i8** %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %33
br label %44
41: ; preds = %33
%42 = load i64, i64* %8
%43 = add i64 %42, 8
store i64 %43, i64* %8
br label %17
44: ; preds = %40, %31, %15
ret void
}

LLVM IR (-O2):
define void @ftrace_peek_string(i32 %0, i64 %1, i8** %2) {
%4 = load i8*, i8** %2
%5 = tail call dereferenceable_or_null(16) i8* @realloc(i8* %4, i64 16)
store i8* %5, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %21, label %7
7: ; preds = %3, %17
%8 = phi i64 [ %18, %17 ], [ 16, %3 ]
%9 = phi i64 [ %10, %17 ], [ 0, %3 ]
%10 = add i64 %9, 8
%11 = icmp ugt i64 %10, %8
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = shl i64 %8, 1
%14 = load i8*, i8** %2
%15 = tail call i8* @realloc(i8* %14, i64 %13)
store i8* %15, i8** %2
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %12, %7
%18 = phi i64 [ %13, %12 ], [ %8, %7 ]
%19 = tail call i64 @ftrace_peek_string_slice(i32 %0, i64 %1, i64 %9, i8** %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %7, label %21
21: ; preds = %17, %12, %3
ret void
}