#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;
typedef int /*<<< orphan*/ args;

/* Variables and functions */
int CS;
int /*<<< orphan*/ PTRACE_PEEKUSER;
#define R10 136
#define R8 135
#define R9 134
#define RBP 133
#define RBX 132
#define RCX 131
#define RDI 130
#define RDX 129
#define RSI 128
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(unsigned long *, int /*<<< orphan*/, int);
void *ptrace(int /*<<< orphan*/, int /*<<< orphan*/ const, int,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int syscall_get_args(const pid_t pid, const int arg, unsigned long args[]) {
  memset(args, 0, sizeof(args[0]) * arg);
  int i;
  long cs;
  int *regs;
  static int regs32[] = {RBX, RCX, RDX, RSI, RDI, RBP};
  static int regs64[] = {RDI, RSI, RDX, R10, R8, R9};
  cs = ptrace(PTRACE_PEEKUSER, pid, 8 * CS, NULL);
  switch (cs) {
  case 0x23:
    regs = regs32;
    break;
  case 0x33:
    regs = regs64;
    break;
  default:
    fprintf(stderr, "Unknown personality, CS=0x%x.\n", (int)cs);
    return -1;
  }
  for (i = 0; i <= arg; i++)
    args[i] = ptrace(PTRACE_PEEKUSER, pid, regs[i] * 8, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<syscall_get_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
shl    $0x3,%edx
xor    %esi,%esi
mov    %esi,-0x3c(%rbp)
call   1030 <memset@plt>
mov    -0x3c(%rbp),%eax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%edi
mov    -0x8(%rbp),%esi
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%edx
shl    $0x3,%edx
mov    %eax,%ecx
call   1050 <ptrace@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
sub    $0x23,%rax
je     11a6 <syscall_get_args+0x76>
jmp    1193 <syscall_get_args+0x63>
mov    -0x38(%rbp),%rax
sub    $0x33,%rax
je     11b6 <syscall_get_args+0x86>
jmp    11c6 <syscall_get_args+0x96>
lea    0x2e93(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11ed <syscall_get_args+0xbd>
lea    0x2ea3(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11ed <syscall_get_args+0xbd>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
lea    0xe24(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <syscall_get_args+0x117>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1240 <syscall_get_args+0x110>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
shl    $0x3,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <ptrace@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f4 <syscall_get_args+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<syscall_get_args>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%r13d
lea    0x0(,%rbx,8),%edx
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    0x2e6a(%rip),%r12
mov    (%r12),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
mov    %r13d,%esi
xor    %ecx,%ecx
call   1050 <ptrace@plt>
cmp    $0x23,%rax
je     1191 <syscall_get_args+0x61>
cmp    $0x33,%rax
jne    11d8 <syscall_get_args+0xa8>
lea    0xe95(%rip),%rbp
test   %ebx,%ebx
jns    119c <syscall_get_args+0x6c>
jmp    11f5 <syscall_get_args+0xc5>
lea    0xe68(%rip),%rbp
test   %ebx,%ebx
js     11f5 <syscall_get_args+0xc5>
add    $0x1,%ebx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%edi
mov    0x0(%rbp,%r15,4),%edx
shl    $0x3,%edx
mov    %r13d,%esi
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    %rax,(%r14,%r15,8)
add    $0x1,%r15
cmp    %r15,%rbx
jne    11b0 <syscall_get_args+0x80>
xor    %r15d,%r15d
jmp    11f5 <syscall_get_args+0xc5>
mov    0x2df9(%rip),%rcx
mov    (%rcx),%edi
lea    0xe50(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@syscall_get_args.regs32 = internal global [6 x i32] [i32 132, i32 131, i32 129, i32 128, i32 130, i32 133]
@syscall_get_args.regs64 = internal global [6 x i32] [i32 130, i32 128, i32 129, i32 136, i32 135, i32 134]
@PTRACE_PEEKUSER = global i32 0
@CS = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Unknown personality, CS=0x%x.\0A\00"
define i32 @syscall_get_args(i32 %0, i32 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64* %2, i64** %7
%11 = load i64*, i64** %7
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = mul i64 8, %13
%15 = trunc i64 %14 to i32
%16 = call i32 @memset(i64* %11, i32 0, i32 %15)
%17 = load i32, i32* @PTRACE_PEEKUSER
%18 = load i32, i32* %5
%19 = load i32, i32* @CS
%20 = mul nsw i32 8, %19
%21 = call i8* @ptrace(i32 %17, i32 %18, i32 %20, i32* null)
%22 = ptrtoint i8* %21 to i64
store i64 %22, i64* %9
%23 = load i64, i64* %9
switch i64 %23, label %26 [
i64 35, label %24
i64 51, label %25
]
24: ; preds = %3
store i32* getelementptr inbounds ([6 x i32], [6 x i32]* @syscall_get_args.regs32, i64 0, i64 0), i32** %10
br label %31
25: ; preds = %3
store i32* getelementptr inbounds ([6 x i32], [6 x i32]* @syscall_get_args.regs64, i64 0, i64 0), i32** %10
br label %31
26: ; preds = %3
%27 = load i32, i32* @stderr
%28 = load i64, i64* %9
%29 = trunc i64 %28 to i32
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 -1, i32* %4
br label %55
31: ; preds = %25, %24
store i32 0, i32* %8
br label %32
32: ; preds = %51, %31
%33 = load i32, i32* %8
%34 = load i32, i32* %6
%35 = icmp sle i32 %33, %34
br i1 %35, label %36, label %54
36: ; preds = %32
%37 = load i32, i32* @PTRACE_PEEKUSER
%38 = load i32, i32* %5
%39 = load i32*, i32** %10
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = mul nsw i32 %43, 8
%45 = call i8* @ptrace(i32 %37, i32 %38, i32 %44, i32* null)
%46 = ptrtoint i8* %45 to i64
%47 = load i64*, i64** %7
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i64, i64* %47, i64 %49
store i64 %46, i64* %50
br label %51
51: ; preds = %36
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %32
54: ; preds = %32
store i32 0, i32* %4
br label %55
55: ; preds = %54, %26
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@syscall_get_args.regs32 = internal unnamed_addr constant [6 x i32] [i32 132, i32 131, i32 129, i32 128, i32 130, i32 133]
@syscall_get_args.regs64 = internal unnamed_addr constant [6 x i32] [i32 130, i32 128, i32 129, i32 136, i32 135, i32 134]
@PTRACE_PEEKUSER = global i32 0
@CS = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Unknown personality, CS=0x%x.\0A\00"
define i32 @syscall_get_args(i32 %0, i32 %1, i64* %2) {
%4 = shl i32 %1, 3
%5 = tail call i32 @memset(i64* %2, i32 0, i32 %4)
%6 = load i32, i32* @PTRACE_PEEKUSER
%7 = load i32, i32* @CS
%8 = shl nsw i32 %7, 3
%9 = tail call i8* @ptrace(i32 %6, i32 %0, i32 %8, i32* null)
%10 = ptrtoint i8* %9 to i64
switch i64 %10, label %12 [
i64 35, label %16
i64 51, label %11
]
11: ; preds = %3
br label %16
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = trunc i64 %10 to i32
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %33
16: ; preds = %3, %11
%17 = phi i32* [ getelementptr inbounds ([6 x i32], [6 x i32]* @syscall_get_args.regs64, i64 0, i64 0), %11 ], [ getelementptr inbounds ([6 x i32], [6 x i32]* @syscall_get_args.regs32, i64 0, i64 0), %3 ]
%18 = icmp slt i32 %1, 0
br i1 %18, label %33, label %19
19: ; preds = %16
%20 = add nuw i32 %1, 1
%21 = zext i32 %20 to i64
br label %22
22: ; preds = %19, %22
%23 = phi i64 [ 0, %19 ], [ %31, %22 ]
%24 = load i32, i32* @PTRACE_PEEKUSER
%25 = getelementptr inbounds i32, i32* %17, i64 %23
%26 = load i32, i32* %25
%27 = shl nsw i32 %26, 3
%28 = tail call i8* @ptrace(i32 %24, i32 %0, i32 %27, i32* null)
%29 = ptrtoint i8* %28 to i64
%30 = getelementptr inbounds i64, i64* %2, i64 %23
store i64 %29, i64* %30
%31 = add nuw nsw i64 %23, 1
%32 = icmp eq i64 %31, %21
br i1 %32, label %33, label %22
33: ; preds = %22, %16, %12
%34 = phi i32 [ -1, %12 ], [ 0, %16 ], [ 0, %22 ]
ret i32 %34
}