#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 */
struct user_regs_struct {
  int dummy;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ perror(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                  struct user_regs_struct *);
long get_child_reg(pid_t pid, int offset) {
  struct user_regs_struct regs;
  if (ptrace(PTRACE_GETREGS, pid, 0, &regs) < 0) {
    perror("ptrace3");
    return -1;
  }
  long *regsArrayPtr = (long *)&regs;
  if (offset >= 17) {
    perror("offset too large");
  }
  return regsArrayPtr[offset];
}


x86 assembly (clang, -O0):
<get_child_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    0x2eab(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
lea    -0x18(%rbp),%rcx
call   1030 <ptrace@plt>
cmp    $0x0,%rax
jge    1168 <get_child_reg+0x48>
lea    0xeaa(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1196 <get_child_reg+0x76>
lea    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x11,-0x10(%rbp)
jl     1186 <get_child_reg+0x66>
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_child_reg>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%esi
mov    0x2eb0(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1030 <ptrace@plt>
test   %rax,%rax
js     1153 <get_child_reg+0x33>
cmp    $0x11,%ebx
jge    1168 <get_child_reg+0x48>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xea6(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    114d <get_child_reg+0x2d>
lea    0xe99(%rip),%rdi
call   1040 <perror@plt>
jmp    1148 <get_child_reg+0x28>

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@PTRACE_GETREGS = global i32 0
@.str = [8 x i8] c"ptrace3\00"
@.str.1 = [17 x i8] c"offset too large\00"
define i64 @get_child_reg(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.user_regs_struct
%7 = alloca i64*
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* @PTRACE_GETREGS
%9 = load i32, i32* %4
%10 = call i64 @ptrace(i32 %8, i32 %9, i32 0, %struct.user_regs_struct* %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %3
br label %26
14: ; preds = %2
%15 = bitcast %struct.user_regs_struct* %6 to i64*
store i64* %15, i64** %7
%16 = load i32, i32* %5
%17 = icmp sge i32 %16, 17
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load i64*, i64** %7
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i64, i64* %21, i64 %23
%25 = load i64, i64* %24
store i64 %25, i64* %3
br label %26
26: ; preds = %20, %12
%27 = load i64, i64* %3
ret i64 %27
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32 }
@PTRACE_GETREGS = global i32 0
@.str = [8 x i8] c"ptrace3\00"
@.str.1 = [17 x i8] c"offset too large\00"
define i64 @get_child_reg(i32 %0, i32 %1) {
%3 = alloca %struct.user_regs_struct
%4 = bitcast %struct.user_regs_struct* %3 to i8*
%5 = load i32, i32* @PTRACE_GETREGS
%6 = call i64 @ptrace(i32 %5, i32 %0, i32 0, %struct.user_regs_struct* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %17
10: ; preds = %2
%11 = bitcast %struct.user_regs_struct* %3 to i64*
%12 = icmp sgt i32 %1, 16
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i64, i64* %11
br label %17
17: ; preds = %15, %8
%18 = phi i64 [ -1, %8 ], [ %16, %15 ]
ret i64 %18
}