#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*/ bzero(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, long *, int);
int /*<<< orphan*/ null_byes_in_word(long);
long peek_data(int /*<<< orphan*/, long);
char *peek_string(pid_t pid, long addr) {
  long data;
  char *string;
  unsigned i;
  int next;
  string = (char *)malloc(sizeof(char) * (32 + 1));
  bzero(string, 32 + 1);
  i = 0;
  do {
    data = peek_data(pid, addr + i);
    memcpy(string + i, &data, sizeof(long));
    i += sizeof(long);
    next = i < 32;
    next &= !null_byes_in_word(data);
  } while (next);
  return (string);
}


x86 assembly (clang, -O0):
<peek_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x21,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x21,%esi
call   1070 <bzero@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%eax
add    %rax,%rsi
call   1060 <peek_data@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    %rax,%rdi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    -0x24(%rbp),%eax
add    $0x8,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x20,-0x24(%rbp)
setb   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <null_byes_in_word@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1182 <peek_string+0x32>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
mov    $0x21,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x21,%esi
call   1070 <bzero@plt>
xor    %ebp,%ebp
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%ebp
lea    (%r14,%rbp,1),%rsi
mov    %r12d,%edi
call   1060 <peek_data@plt>
mov    %rax,0x8(%rsp)
lea    (%rbx,%rbp,1),%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
add    $0x8,%rbp
mov    0x8(%rsp),%rdi
call   1050 <null_byes_in_word@plt>
test   $0xffffffe0,%ebp
jne    11ce <peek_string+0x7e>
test   %eax,%eax
je     1190 <peek_string+0x40>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @peek_string(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%9 = call i64 @malloc(i32 33)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = call i32 @bzero(i8* %11, i32 33)
store i32 0, i32* %7
br label %13
13: ; preds = %39, %2
%14 = load i32, i32* %3
%15 = load i64, i64* %4
%16 = load i32, i32* %7
%17 = zext i32 %16 to i64
%18 = add nsw i64 %15, %17
%19 = call i64 @peek_data(i32 %14, i64 %18)
store i64 %19, i64* %5
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = call i32 @memcpy(i8* %23, i64* %5, i32 8)
%25 = load i32, i32* %7
%26 = zext i32 %25 to i64
%27 = add i64 %26, 8
%28 = trunc i64 %27 to i32
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp ult i32 %29, 32
%31 = zext i1 %30 to i32
store i32 %31, i32* %8
%32 = load i64, i64* %5
%33 = call i32 @null_byes_in_word(i64 %32)
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
%36 = zext i1 %35 to i32
%37 = load i32, i32* %8
%38 = and i32 %37, %36
store i32 %38, i32* %8
br label %39
39: ; preds = %13
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %13, label %42
42: ; preds = %39
%43 = load i8*, i8** %6
ret i8* %43
}

LLVM IR (-O2):
define i8* @peek_string(i32 %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i64 @malloc(i32 33)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @bzero(i8* %6, i32 33)
br label %8
8: ; preds = %8, %2
%9 = phi i64 [ 0, %2 ], [ %15, %8 ]
%10 = and i64 %9, 4294967295
%11 = add nsw i64 %10, %1
%12 = call i64 @peek_data(i32 %0, i64 %11)
store i64 %12, i64* %3
%13 = getelementptr inbounds i8, i8* %6, i64 %10
%14 = call i32 @memcpy(i8* %13, i64* %3, i32 8)
%15 = add nuw nsw i64 %10, 8
%16 = and i64 %15, 4294967264
%17 = icmp eq i64 %16, 0
%18 = load i64, i64* %3
%19 = call i32 @null_byes_in_word(i64 %18)
%20 = icmp eq i32 %19, 0
%21 = and i1 %17, %20
br i1 %21, label %8, label %22
22: ; preds = %8
ret i8* %6
}