#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 */
char *realloc(char *, size_t);
scalar_t__ strace_read_string_slice(int /*<<< orphan*/, unsigned long long,
                                    size_t, char **);
void strace_read_string(char **strp, unsigned long long int addr, pid_t child) {
  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 (strace_read_string_slice(child, addr, readb, strp))
      return;
    readb += sizeof(unsigned long);
  }
}


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

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

LLVM IR (-O0):
define void @strace_read_string(i8** %0, i64 %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i64 16, i64* %7
store i64 0, i64* %8
%9 = load i8**, i8*** %4
%10 = load i8*, i8** %9
%11 = load i64, i64* %7
%12 = call i8* @realloc(i8* %10, i64 %11)
%13 = load i8**, i8*** %4
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*** %4
%26 = load i8*, i8** %25
%27 = load i64, i64* %7
%28 = call i8* @realloc(i8* %26, i64 %27)
%29 = load i8**, i8*** %4
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* %6
%35 = load i64, i64* %5
%36 = load i64, i64* %8
%37 = load i8**, i8*** %4
%38 = call i64 @strace_read_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 @strace_read_string(i8** %0, i64 %1, i32 %2) {
%4 = load i8*, i8** %0
%5 = tail call dereferenceable_or_null(16) i8* @realloc(i8* %4, i64 16)
store i8* %5, i8** %0
%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** %0
%15 = tail call i8* @realloc(i8* %14, i64 %13)
store i8* %15, i8** %0
%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 @strace_read_string_slice(i32 %2, i64 %1, i64 %9, i8** %0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %7, label %21
21: ; preds = %17, %12, %3
ret void
}