#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 orig_rax;
  int /*<<< orphan*/ rdx;
  scalar_t__ rsi;
  int /*<<< orphan*/ rdi;
  int /*<<< orphan*/ member_0;
};
struct timespec {
  int /*<<< orphan*/ member_0;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_GETREGS;
struct user_regs_struct *PTRACE_O_TRACESYSGOOD;
int /*<<< orphan*/ PTRACE_SETOPTIONS;
int SYS_write;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ on_write(int, int /*<<< orphan*/, void *,
                            int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/,
                  struct user_regs_struct *);
int /*<<< orphan*/ stderr;
scalar_t__ timespec_leq(struct timespec *, struct timespec const *);
scalar_t__ wait(int *);
scalar_t__ wait_for_syscall(int);
void do_trace(pid_t pid, const struct timespec *deadline) {
  int status, syscall;
  int entering = 1;
  if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0) {
    perror("PTRACE_ATTACH");
    return;
  }
  if (wait(&status) < 0) {
    perror("wait");
    return;
  }
  fprintf(stderr, "Attached pid %d\n", pid);
  if (ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD) < 0) {
    perror("PTRACE_O_TRACESYSGOOD");
    return;
  }
  struct timespec now = {0};
  for (; timespec_leq(&now, deadline); clock_gettime(CLOCK_MONOTONIC, &now)) {
    if (wait_for_syscall(pid) < 0)
      break;
    struct user_regs_struct regs = {0};
    if (ptrace(PTRACE_GETREGS, pid, 0, &regs) < 0)
      perror("PTRACE_GETREGS");
    syscall = regs.orig_rax;
    if (syscall != SYS_write)
      continue;
    if (!entering) {
      entering = 1;
      continue;
    }
    entering = 0;
    on_write(pid, regs.rdi, (void *)regs.rsi, regs.rdx);
  }
}


x86 assembly (clang, -O0):
<do_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <ptrace@plt>
cmp    $0x0,%rax
jge    11d8 <do_trace+0x48>
lea    0xe32(%rip),%rdi
call   10a0 <perror@plt>
jmp    132a <do_trace+0x19a>
lea    -0x14(%rbp),%rdi
call   10b0 <wait@plt>
cmp    $0x0,%rax
jge    11fc <do_trace+0x6c>
lea    0xe1c(%rip),%rdi
call   10a0 <perror@plt>
jmp    132a <do_trace+0x19a>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe04(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    0x2db9(%rip),%rax
mov    (%rax),%rcx
xor    %edx,%edx
call   1080 <ptrace@plt>
cmp    $0x0,%rax
jge    124c <do_trace+0xbc>
lea    0xde2(%rip),%rdi
call   10a0 <perror@plt>
jmp    132a <do_trace+0x19a>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1070 <timespec_leq@plt>
cmp    $0x0,%rax
je     132a <do_trace+0x19a>
mov    -0x4(%rbp),%edi
call   1090 <wait_for_syscall@plt>
cmp    $0x0,%rax
jge    128a <do_trace+0xfa>
jmp    132a <do_trace+0x19a>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %edx,%edx
lea    -0x38(%rbp),%rcx
call   1080 <ptrace@plt>
cmp    $0x0,%rax
jge    12c7 <do_trace+0x137>
lea    0xd78(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2cf9(%rip),%rcx
cmp    (%rcx),%eax
je     12e4 <do_trace+0x154>
jmp    1313 <do_trace+0x183>
cmpl   $0x0,-0x1c(%rbp)
jne    12fa <do_trace+0x16a>
movl   $0x1,-0x1c(%rbp)
jmp    1313 <do_trace+0x183>
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x34(%rbp),%ecx
call   1060 <on_write@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
jmp    125c <do_trace+0xcc>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_trace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
test   %rax,%rax
js     12db <do_trace+0x15b>
lea    0x34(%rsp),%rdi
call   10a0 <wait@plt>
test   %rax,%rax
js     12f5 <do_trace+0x175>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%rcx
mov    %ebp,%esi
xor    %edx,%edx
call   1070 <ptrace@plt>
test   %rax,%rax
js     12fe <do_trace+0x17e>
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1060 <timespec_leq@plt>
test   %rax,%rax
je     130a <do_trace+0x18a>
mov    $0x1,%r14d
mov    0x2da6(%rip),%r15
mov    0x2db7(%rip),%r13
lea    0x8(%rsp),%rbx
jmp    1260 <do_trace+0xe0>
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <timespec_leq@plt>
test   %rax,%rax
je     130a <do_trace+0x18a>
mov    %ebp,%edi
call   1080 <wait_for_syscall@plt>
test   %rax,%rax
js     130a <do_trace+0x18a>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
lea    0x10(%rsp),%rcx
call   1070 <ptrace@plt>
test   %rax,%rax
js     12cd <do_trace+0x14d>
mov    0x10(%rsp),%eax
cmp    (%r15),%eax
jne    1240 <do_trace+0xc0>
test   %r14d,%r14d
mov    $0x1,%r14d
je     1240 <do_trace+0xc0>
mov    0x20(%rsp),%esi
mov    0x18(%rsp),%rdx
mov    0x14(%rsp),%ecx
mov    %ebp,%edi
call   1050 <on_write@plt>
xor    %r14d,%r14d
jmp    1240 <do_trace+0xc0>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
jmp    129d <do_trace+0x11d>
lea    0xd2e(%rip),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <perror@plt>
lea    0xd22(%rip),%rdi
jmp    1305 <do_trace+0x185>
lea    0xd2f(%rip),%rdi
call   1090 <perror@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.user_regs_struct = type { i32, i32, i64, i32, i32 }
%struct.timespec = type { i32 }
@PTRACE_ATTACH = global i32 0
@.str = [14 x i8] c"PTRACE_ATTACH\00"
@.str.1 = [5 x i8] c"wait\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"Attached pid %d\0A\00"
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global %struct.user_regs_struct* null
@.str.3 = [22 x i8] c"PTRACE_O_TRACESYSGOOD\00"
@PTRACE_GETREGS = global i32 0
@.str.4 = [15 x i8] c"PTRACE_GETREGS\00"
@SYS_write = global i32 0
@CLOCK_MONOTONIC = global i32 0
define void @do_trace(i32 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
%9 = alloca %struct.user_regs_struct
store i32 %0, i32* %3
store %struct.timespec* %1, %struct.timespec** %4
store i32 1, i32* %7
%10 = load i32, i32* @PTRACE_ATTACH
%11 = load i32, i32* %3
%12 = call i64 @ptrace(i32 %10, i32 %11, i32 0, %struct.user_regs_struct* null)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %75
16: ; preds = %2
%17 = call i64 @wait(i32* %5)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %75
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i32, i32* %3
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @PTRACE_SETOPTIONS
%26 = load i32, i32* %3
%27 = load %struct.user_regs_struct*, %struct.user_regs_struct** @PTRACE_O_TRACESYSGOOD
%28 = call i64 @ptrace(i32 %25, i32 %26, i32 0, %struct.user_regs_struct* %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %75
32: ; preds = %21
%33 = bitcast %struct.timespec* %8 to i8*
br label %34
34: ; preds = %72, %32
%35 = load %struct.timespec*, %struct.timespec** %4
%36 = call i64 @timespec_leq(%struct.timespec* %8, %struct.timespec* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %75
38: ; preds = %34
%39 = load i32, i32* %3
%40 = call i64 @wait_for_syscall(i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
br label %75
43: ; preds = %38
%44 = bitcast %struct.user_regs_struct* %9 to i8*
%45 = load i32, i32* @PTRACE_GETREGS
%46 = load i32, i32* %3
%47 = call i64 @ptrace(i32 %45, i32 %46, i32 0, %struct.user_regs_struct* %9)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %43
%52 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %9, i32 0, i32 0
%53 = load i32, i32* %52
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = load i32, i32* @SYS_write
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %51
br label %72
58: ; preds = %51
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %58
store i32 1, i32* %7
br label %72
62: ; preds = %58
store i32 0, i32* %7
%63 = load i32, i32* %3
%64 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %9, i32 0, i32 3
%65 = load i32, i32* %64
%66 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %9, i32 0, i32 2
%67 = load i64, i64* %66
%68 = inttoptr i64 %67 to i8*
%69 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %9, i32 0, i32 1
%70 = load i32, i32* %69
%71 = call i32 @on_write(i32 %63, i32 %65, i8* %68, i32 %70)
br label %72
72: ; preds = %62, %61, %57
%73 = load i32, i32* @CLOCK_MONOTONIC
%74 = call i32 @clock_gettime(i32 %73, %struct.timespec* %8)
br label %34
75: ; preds = %14, %19, %30, %42, %34
ret void
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32, i32, i64, i32, i32 }
%struct.timespec = type { i32 }
@PTRACE_ATTACH = global i32 0
@.str = [14 x i8] c"PTRACE_ATTACH\00"
@.str.1 = [5 x i8] c"wait\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"Attached pid %d\0A\00"
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global %struct.user_regs_struct* null
@.str.3 = [22 x i8] c"PTRACE_O_TRACESYSGOOD\00"
@PTRACE_GETREGS = global i32 0
@.str.4 = [15 x i8] c"PTRACE_GETREGS\00"
@SYS_write = global i32 0
@CLOCK_MONOTONIC = global i32 0
define void @do_trace(i32 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.user_regs_struct
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @PTRACE_ATTACH
%8 = tail call i64 @ptrace(i32 %7, i32 %0, i32 0, %struct.user_regs_struct* null)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %66
12: ; preds = %2
%13 = call i64 @wait(i32* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %66
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%20 = load i32, i32* @PTRACE_SETOPTIONS
%21 = load %struct.user_regs_struct*, %struct.user_regs_struct** @PTRACE_O_TRACESYSGOOD
%22 = call i64 @ptrace(i32 %20, i32 %0, i32 0, %struct.user_regs_struct* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %66
26: ; preds = %17
%27 = bitcast %struct.timespec* %4 to i8*
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
store i32 0, i32* %28
%29 = call i64 @timespec_leq(%struct.timespec* %4, %struct.timespec* %1)
%30 = icmp eq i64 %29, 0
br i1 %30, label %65, label %31
31: ; preds = %26
%32 = bitcast %struct.user_regs_struct* %5 to i8*
%33 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %5, i64 0, i32 0
%34 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %5, i64 0, i32 3
%35 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %5, i64 0, i32 2
%36 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %5, i64 0, i32 1
br label %37
37: ; preds = %31, %59
%38 = phi i32 [ 1, %31 ], [ %60, %59 ]
%39 = call i64 @wait_for_syscall(i32 %0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %65, label %41
41: ; preds = %37
%42 = load i32, i32* @PTRACE_GETREGS
%43 = call i64 @ptrace(i32 %42, i32 %0, i32 0, %struct.user_regs_struct* %5)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %41
%48 = load i32, i32* %33
%49 = load i32, i32* @SYS_write
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %59
51: ; preds = %47
%52 = icmp eq i32 %38, 0
br i1 %52, label %59, label %53
53: ; preds = %51
%54 = load i32, i32* %34
%55 = load i64, i64* %35
%56 = inttoptr i64 %55 to i8*
%57 = load i32, i32* %36
%58 = call i32 @on_write(i32 %0, i32 %54, i8* %56, i32 %57)
br label %59
59: ; preds = %51, %47, %53
%60 = phi i32 [ 0, %53 ], [ %38, %47 ], [ 1, %51 ]
%61 = load i32, i32* @CLOCK_MONOTONIC
%62 = call i32 @clock_gettime(i32 %61, %struct.timespec* %4)
%63 = call i64 @timespec_leq(%struct.timespec* %4, %struct.timespec* %1)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %37
65: ; preds = %59, %37, %26
br label %66
66: ; preds = %65, %24, %15, %10
ret void
}