#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 EINVAL;
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_SYSCALL;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/, struct user_regs_struct *);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int syscall_trace(pid_t pid, struct user_regs_struct *state) {
  int status;
  ptrace(PTRACE_SYSCALL, pid, 0, 0);
  waitpid(pid, &status, 0);
  if (WIFSTOPPED(status) && WSTOPSIG(status) & 0x80) {
    ptrace(PTRACE_GETREGS, pid, 0, state);
    return 0;
  } else if (WIFEXITED(status)) {
    exit(0);
  } else {
    return EINVAL;
  }
}


x86 assembly (clang, -O0):
<syscall_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x14(%rbp),%edi
call   1030 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     11df <syscall_trace+0x7f>
mov    -0x14(%rbp),%edi
call   1080 <WSTOPSIG@plt>
and    $0x80,%eax
cmp    $0x0,%eax
je     11df <syscall_trace+0x7f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rcx
xor    %edx,%edx
call   1040 <ptrace@plt>
movl   $0x0,-0x4(%rbp)
jmp    1204 <syscall_trace+0xa4>
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11f8 <syscall_trace+0x98>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<syscall_trace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1030 <WIFSTOPPED@plt>
test   %rax,%rax
je     11aa <syscall_trace+0x4a>
mov    0xc(%rsp),%edi
call   1080 <WSTOPSIG@plt>
test   $0x80,%al
jne    11c3 <syscall_trace+0x63>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    11e5 <syscall_trace+0x85>
mov    0x2e19(%rip),%rax
mov    (%rax),%ebx
jmp    11da <syscall_trace+0x7a>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1040 <ptrace@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@PTRACE_SYSCALL = global i32 0
@PTRACE_GETREGS = global i32 0
@EINVAL = global i32 0
define i32 @syscall_trace(i32 %0, %struct.user_regs_struct* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.user_regs_struct*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.user_regs_struct* %1, %struct.user_regs_struct** %5
%7 = load i32, i32* @PTRACE_SYSCALL
%8 = load i32, i32* %4
%9 = call i32 @ptrace(i32 %7, i32 %8, i32 0, %struct.user_regs_struct* null)
%10 = load i32, i32* %4
%11 = call i32 @waitpid(i32 %10, i32* %6, i32 0)
%12 = load i32, i32* %6
%13 = call i64 @WIFSTOPPED(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @WSTOPSIG(i32 %16)
%18 = and i32 %17, 128
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32, i32* @PTRACE_GETREGS
%22 = load i32, i32* %4
%23 = load %struct.user_regs_struct*, %struct.user_regs_struct** %5
%24 = call i32 @ptrace(i32 %21, i32 %22, i32 0, %struct.user_regs_struct* %23)
store i32 0, i32* %3
br label %33
25: ; preds = %15, %2
%26 = load i32, i32* %6
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %25
%32 = load i32, i32* @EINVAL
store i32 %32, i32* %3
br label %33
33: ; preds = %31, %20
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32 }
@PTRACE_SYSCALL = global i32 0
@PTRACE_GETREGS = global i32 0
@EINVAL = global i32 0
define i32 @syscall_trace(i32 %0, %struct.user_regs_struct* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @PTRACE_SYSCALL
%6 = tail call i32 @ptrace(i32 %5, i32 %0, i32 0, %struct.user_regs_struct* null)
%7 = call i32 @waitpid(i32 %0, i32* %3, i32 0)
%8 = load i32, i32* %3
%9 = call i64 @WIFSTOPPED(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %2
%12 = load i32, i32* %3
%13 = call i32 @WSTOPSIG(i32 %12)
%14 = and i32 %13, 128
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* @PTRACE_GETREGS
%18 = call i32 @ptrace(i32 %17, i32 %0, i32 0, %struct.user_regs_struct* %1)
br label %27
19: ; preds = %11, %2
%20 = load i32, i32* %3
%21 = call i64 @WIFEXITED(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %19
%26 = load i32, i32* @EINVAL
br label %27
27: ; preds = %25, %16
%28 = phi i32 [ 0, %16 ], [ %26, %25 ]
ret i32 %28
}