#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_PEEKTEXT;
int /*<<< orphan*/ PTRACE_PEEKUSER;
int RDI;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void read_file(pid_t child, char *file) {
  char *child_addr;
  int i;
  child_addr = (char *)ptrace(PTRACE_PEEKUSER, child, sizeof(long) * RDI, 0);
  do {
    long val;
    char *p;
    val = ptrace(PTRACE_PEEKTEXT, child, child_addr, NULL);
    if (val == -1) {
      fprintf(stderr, "PTRACE_PEEKTEXT error: %s", strerror(errno));
      exit(1);
    }
    child_addr += sizeof(long);
    p = (char *)&val;
    for (i = 0; i < sizeof(long); ++i, ++file) {
      *file = *p++;
      if (*file == '\0')
        break;
    }
  } while (i == sizeof(long));
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    0x2e8e(%rip),%rax
movslq (%rax),%rdx
shl    $0x3,%rdx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <ptrace@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1040 <ptrace@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11d6 <read_file+0x96>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe39(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
jae    124a <read_file+0x10a>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    122b <read_file+0xeb>
jmp    124a <read_file+0x10a>
jmp    1230 <read_file+0xf0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11f1 <read_file+0xb1>
jmp    124f <read_file+0x10f>
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
je     1176 <read_file+0x36>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e93(%rip),%rax
movslq (%rax),%rdx
shl    $0x3,%rdx
mov    %r14d,%esi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <ptrace@plt>
mov    %rax,%rbp
mov    0x2e4e(%rip),%r15
mov    (%r15),%edi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <ptrace@plt>
cmp    $0xffffffffffffffff,%rax
je     1233 <read_file+0xf3>
add    $0x8,%rbp
mov    %al,(%rbx)
test   %al,%al
je     1265 <read_file+0x125>
mov    %rax,%rcx
shr    $0x8,%rcx
mov    %cl,0x1(%rbx)
test   %cl,%cl
je     1265 <read_file+0x125>
mov    %rax,%rcx
shr    $0x10,%rcx
mov    %cl,0x2(%rbx)
test   %cl,%cl
je     1265 <read_file+0x125>
mov    %rax,%rcx
shr    $0x18,%rcx
mov    %cl,0x3(%rbx)
test   %cl,%cl
je     1265 <read_file+0x125>
mov    %rax,%rcx
shr    $0x20,%rcx
mov    %cl,0x4(%rbx)
test   %cl,%cl
je     1265 <read_file+0x125>
mov    %rax,%rcx
shr    $0x28,%rcx
mov    %cl,0x5(%rbx)
test   %cl,%cl
je     1265 <read_file+0x125>
mov    %rax,%rcx
shr    $0x30,%rcx
mov    %cl,0x6(%rbx)
test   %cl,%cl
je     1265 <read_file+0x125>
shr    $0x38,%rax
mov    %al,0x7(%rbx)
test   %al,%al
je     1265 <read_file+0x125>
mov    (%r15),%edi
mov    %r14d,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <ptrace@plt>
add    $0x8,%rbp
add    $0x8,%rbx
cmp    $0xffffffffffffffff,%rax
jne    119a <read_file+0x5a>
mov    0x2d9e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xdaf(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PTRACE_PEEKUSER = global i32 0
@RDI = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"PTRACE_PEEKTEXT error: %s\00"
@errno = global i32 0
define void @read_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i32, i32* @PTRACE_PEEKUSER
%10 = load i32, i32* %3
%11 = load i32, i32* @RDI
%12 = sext i32 %11 to i64
%13 = mul i64 8, %12
%14 = call i64 (i32, i32, ...) @ptrace(i32 %9, i32 %10, i64 %13, i32 0)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
br label %16
16: ; preds = %54, %2
%17 = load i32, i32* @PTRACE_PEEKTEXT
%18 = load i32, i32* %3
%19 = load i8*, i8** %5
%20 = call i64 (i32, i32, ...) @ptrace(i32 %17, i32 %18, i8* %19, i8* null)
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %16
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 8
store i8* %31, i8** %5
%32 = bitcast i64* %7 to i8*
store i8* %32, i8** %8
store i32 0, i32* %6
br label %33
33: ; preds = %48, %29
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = icmp ult i64 %35, 8
br i1 %36, label %37, label %53
37: ; preds = %33
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %8
%40 = load i8, i8* %38
%41 = load i8*, i8** %4
store i8 %40, i8* %41
%42 = load i8*, i8** %4
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %37
br label %53
47: ; preds = %37
br label %48
48: ; preds = %47
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
%51 = load i8*, i8** %4
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %4
br label %33
53: ; preds = %46, %33
br label %54
54: ; preds = %53
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = icmp eq i64 %56, 8
br i1 %57, label %16, label %58
58: ; preds = %54
ret void
}

LLVM IR (-O2):
@PTRACE_PEEKUSER = global i32 0
@RDI = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"PTRACE_PEEKTEXT error: %s\00"
@errno = global i32 0
define void @read_file(i32 %0, i8* nocapture writeonly %1) {
%3 = load i32, i32* @PTRACE_PEEKUSER
%4 = load i32, i32* @RDI
%5 = sext i32 %4 to i64
%6 = shl nsw i64 %5, 3
%7 = tail call i64 (i32, i32, ...) @ptrace(i32 %3, i32 %0, i64 %6, i32 0)
%8 = inttoptr i64 %7 to i8*
%9 = load i32, i32* @PTRACE_PEEKTEXT
%10 = tail call i64 (i32, i32, ...) @ptrace(i32 %9, i32 %0, i8* %8, i8* null)
%11 = icmp eq i64 %10, -1
br i1 %11, label %17, label %23
12: ; preds = %62
%13 = getelementptr inbounds i8, i8* %26, i64 8
%14 = load i32, i32* @PTRACE_PEEKTEXT
%15 = tail call i64 (i32, i32, ...) @ptrace(i32 %14, i32 %0, i8* %42, i8* null)
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %12, %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %2, %12
%24 = phi i64 [ %15, %12 ], [ %10, %2 ]
%25 = phi i8* [ %42, %12 ], [ %8, %2 ]
%26 = phi i8* [ %13, %12 ], [ %1, %2 ]
%27 = trunc i64 %24 to i8
%28 = lshr i64 %24, 8
%29 = trunc i64 %28 to i8
%30 = lshr i64 %24, 16
%31 = trunc i64 %30 to i8
%32 = lshr i64 %24, 24
%33 = trunc i64 %32 to i8
%34 = lshr i64 %24, 32
%35 = trunc i64 %34 to i8
%36 = lshr i64 %24, 40
%37 = trunc i64 %36 to i8
%38 = lshr i64 %24, 48
%39 = trunc i64 %38 to i8
%40 = lshr i64 %24, 56
%41 = trunc i64 %40 to i8
%42 = getelementptr inbounds i8, i8* %25, i64 8
store i8 %27, i8* %26
%43 = icmp eq i8 %27, 0
br i1 %43, label %65, label %44
44: ; preds = %23
%45 = getelementptr inbounds i8, i8* %26, i64 1
store i8 %29, i8* %45
%46 = icmp eq i8 %29, 0
br i1 %46, label %65, label %47
47: ; preds = %44
%48 = getelementptr inbounds i8, i8* %26, i64 2
store i8 %31, i8* %48
%49 = icmp eq i8 %31, 0
br i1 %49, label %65, label %50
50: ; preds = %47
%51 = getelementptr inbounds i8, i8* %26, i64 3
store i8 %33, i8* %51
%52 = icmp eq i8 %33, 0
br i1 %52, label %65, label %53
53: ; preds = %50
%54 = getelementptr inbounds i8, i8* %26, i64 4
store i8 %35, i8* %54
%55 = icmp eq i8 %35, 0
br i1 %55, label %65, label %56
56: ; preds = %53
%57 = getelementptr inbounds i8, i8* %26, i64 5
store i8 %37, i8* %57
%58 = icmp eq i8 %37, 0
br i1 %58, label %65, label %59
59: ; preds = %56
%60 = getelementptr inbounds i8, i8* %26, i64 6
store i8 %39, i8* %60
%61 = icmp eq i8 %39, 0
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = getelementptr inbounds i8, i8* %26, i64 7
store i8 %41, i8* %63
%64 = icmp eq i8 %41, 0
br i1 %64, label %65, label %12
65: ; preds = %23, %44, %47, %50, %53, %56, %59, %62
ret void
}