#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 iovec {
  unsigned long *iov_base;
  int iov_len;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ FAIL_IF(int /*<<< orphan*/);
int /*<<< orphan*/ PTRACE_GETREGSET;
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, unsigned long,
            struct iovec *);
int /*<<< orphan*/ start_trace(int /*<<< orphan*/);
int /*<<< orphan*/ stop_trace(int /*<<< orphan*/);
int ptrace_read_regs(pid_t child, unsigned long type, unsigned long regs[],
                     int n) {
  struct iovec iov;
  long ret;
  FAIL_IF(start_trace(child));
  iov.iov_base = regs;
  iov.iov_len = n * sizeof(unsigned long);
  ret = ptrace(PTRACE_GETREGSET, child, type, &iov);
  if (ret)
    return ret;
  FAIL_IF(stop_trace(child));
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_read_regs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <start_trace@plt>
mov    %eax,%edi
call   1040 <FAIL_IF@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,-0x28(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <ptrace@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11ac <ptrace_read_regs+0x6c>
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11c2 <ptrace_read_regs+0x82>
mov    -0x8(%rbp),%edi
call   1030 <stop_trace@plt>
mov    %eax,%edi
call   1040 <FAIL_IF@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_read_regs>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
call   1050 <start_trace@plt>
mov    %eax,%edi
call   1040 <FAIL_IF@plt>
mov    %r14,0x8(%rsp)
shl    $0x3,%ebp
mov    %ebp,0x10(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rcx
mov    %ebx,%esi
mov    %r15,%rdx
call   1060 <ptrace@plt>
test   %rax,%rax
jne    1199 <ptrace_read_regs+0x59>
mov    %ebx,%edi
call   1030 <stop_trace@plt>
mov    %eax,%edi
call   1040 <FAIL_IF@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i64*, i32 }
@PTRACE_GETREGSET = global i32 0
define i32 @ptrace_read_regs(i32 %0, i64 %1, i64* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i32
%10 = alloca %struct.iovec
%11 = alloca i64
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64* %2, i64** %8
store i32 %3, i32* %9
%12 = load i32, i32* %6
%13 = call i32 @start_trace(i32 %12)
%14 = call i32 @FAIL_IF(i32 %13)
%15 = load i64*, i64** %8
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
store i64* %15, i64** %16
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @PTRACE_GETREGSET
%23 = load i32, i32* %6
%24 = load i64, i64* %7
%25 = call i64 @ptrace(i32 %22, i32 %23, i64 %24, %struct.iovec* %10)
store i64 %25, i64* %11
%26 = load i64, i64* %11
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %4
%29 = load i64, i64* %11
%30 = trunc i64 %29 to i32
store i32 %30, i32* %5
br label %35
31: ; preds = %4
%32 = load i32, i32* %6
%33 = call i32 @stop_trace(i32 %32)
%34 = call i32 @FAIL_IF(i32 %33)
store i32 0, i32* %5
br label %35
35: ; preds = %31, %28
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
%struct.iovec = type { i64*, i32 }
@PTRACE_GETREGSET = global i32 0
define i32 @ptrace_read_regs(i32 %0, i64 %1, i64* %2, i32 %3) {
%5 = alloca %struct.iovec
%6 = bitcast %struct.iovec* %5 to i8*
%7 = tail call i32 @start_trace(i32 %0)
%8 = tail call i32 @FAIL_IF(i32 %7)
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i64* %2, i64** %9
%10 = shl i32 %3, 3
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @PTRACE_GETREGSET
%13 = call i64 @ptrace(i32 %12, i32 %0, i64 %1, %struct.iovec* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %4
%16 = trunc i64 %13 to i32
br label %20
17: ; preds = %4
%18 = call i32 @stop_trace(i32 %0)
%19 = call i32 @FAIL_IF(i32 %18)
br label %20
20: ; preds = %17, %15
%21 = phi i32 [ %16, %15 ], [ 0, %17 ]
ret i32 %21
}