#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 scalar_t__ pid_t;

/* Variables and functions */
int ORIG_RAX;
int /*<<< orphan*/ PTRACE_PEEKUSER;
int /*<<< orphan*/ PTRACE_SYSCALL;
int /*<<< orphan*/ PTRACE_TRACEME;
int RAX;
int RBX;
int RCX;
int RDX;
long SYS_write;
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long, ...);
long ptrace(int /*<<< orphan*/, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int *);
int main(void) {
  pid_t child;
  long orig_rax, rax;
  long params[3];
  int status = 0;
  int insyscall = 0;
  child = fork();
  if (child == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);
    execl("/bin/ls", "ls", NULL);
  } else {
    while (1) {
      wait(&status);
      if (WIFEXITED(status)) {
        break;
      }
      orig_rax = ptrace(PTRACE_PEEKUSER, child, 8 * ORIG_RAX, NULL);
      if (orig_rax == SYS_write) {
        if (insyscall == 0) {
          insyscall = 1;
          params[0] = ptrace(PTRACE_PEEKUSER, child, 8 * RBX, NULL);
          params[1] = ptrace(PTRACE_PEEKUSER, child, 8 * RCX, NULL);
          params[2] = ptrace(PTRACE_PEEKUSER, child, 8 * RDX, NULL);
          fprintf(stdout,
                  "Write caled with "
                  "%ld, %ld, %ld \n",
                  params[0], params[1], params[2]);
        } else {
          rax = ptrace(PTRACE_PEEKUSER, child, 8 * RAX, NULL);
          fprintf(stdout,
                  "Write returned "
                  "with %ld ",
                  rax);
          insyscall = 0;
        }
      }
      ptrace(PTRACE_SYSCALL, child, NULL, NULL);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c6 <main+0x66>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   1040 <ptrace@plt>
lea    0xe4f(%rip),%rdi
lea    0xe4d(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <execl@plt>
jmp    133f <main+0x1df>
jmp    11cb <main+0x6b>
lea    -0x44(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x44(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11eb <main+0x8b>
jmp    133a <main+0x1da>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%rax
jne    131d <main+0x1bd>
cmpl   $0x0,-0x48(%rbp)
jne    12d0 <main+0x170>
movl   $0x1,-0x48(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x40(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x38(%rbp)
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x30(%rbp)
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1318 <main+0x1b8>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2cf4(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x20(%rbp)
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd1f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x48(%rbp)
jmp    131d <main+0x1bd>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
jmp    11cb <main+0x6b>
jmp    133f <main+0x1df>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     12dc <main+0x17c>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
call   1070 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
jne    1305 <main+0x1a5>
mov    0x2def(%rip),%r14
mov    0x2e30(%rip),%rbp
mov    0x2e19(%rip),%r13
mov    0x2dd2(%rip),%r12
xor    %r15d,%r15d
jmp    122f <main+0xcf>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %r15d,%r15d
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x2db8(%rip),%rcx
mov    (%rcx),%edi
lea    0xe30(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0x4(%rsp),%rdi
call   1070 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
jne    1305 <main+0x1a5>
mov    (%r14),%edi
mov    0x0(%rbp),%edx
shl    $0x3,%edx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
cmp    0x0(%r13),%rax
jne    1203 <main+0xa3>
mov    (%r14),%edi
test   %r15d,%r15d
jne    11d0 <main+0x70>
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %r12,%rbp
mov    %r13,%r12
mov    %rax,%r13
mov    (%r14),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,%r15
mov    (%r14),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x2cfd(%rip),%rcx
mov    (%rcx),%edi
lea    0xd54(%rip),%rsi
mov    %r13,%rdx
mov    %r12,%r13
mov    %rbp,%r12
mov    0x2d1c(%rip),%rbp
mov    %r15,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%r15d
jmp    1203 <main+0xa3>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0xd09(%rip),%rdi
lea    0xd07(%rip),%rsi
xor    %edx,%edx
call   1060 <execl@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PTRACE_TRACEME = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@PTRACE_PEEKUSER = global i32 0
@ORIG_RAX = global i32 0
@SYS_write = global i64 0
@RBX = global i32 0
@RCX = global i32 0
@RDX = global i32 0
@stdout = global i32 0
@.str.2 = [33 x i8] c"Write caled with %ld, %ld, %ld \0A\00"
@RAX = global i32 0
@.str.3 = [25 x i8] c"Write returned with %ld \00"
@PTRACE_SYSCALL = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca [3 x i64]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
store i32 0, i32* %7
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @PTRACE_TRACEME
%13 = call i64 @ptrace(i32 %12, i64 0, i32 0, i32* null)
%14 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %76
15: ; preds = %0
br label %16
16: ; preds = %15, %71
%17 = call i32 @wait(i32* %6)
%18 = load i32, i32* %6
%19 = call i64 @WIFEXITED(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %75
22: ; preds = %16
%23 = load i32, i32* @PTRACE_PEEKUSER
%24 = load i64, i64* %2
%25 = load i32, i32* @ORIG_RAX
%26 = mul nsw i32 8, %25
%27 = call i64 @ptrace(i32 %23, i64 %24, i32 %26, i32* null)
store i64 %27, i64* %3
%28 = load i64, i64* %3
%29 = load i64, i64* @SYS_write
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %71
31: ; preds = %22
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %61
34: ; preds = %31
store i32 1, i32* %7
%35 = load i32, i32* @PTRACE_PEEKUSER
%36 = load i64, i64* %2
%37 = load i32, i32* @RBX
%38 = mul nsw i32 8, %37
%39 = call i64 @ptrace(i32 %35, i64 %36, i32 %38, i32* null)
%40 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
store i64 %39, i64* %40
%41 = load i32, i32* @PTRACE_PEEKUSER
%42 = load i64, i64* %2
%43 = load i32, i32* @RCX
%44 = mul nsw i32 8, %43
%45 = call i64 @ptrace(i32 %41, i64 %42, i32 %44, i32* null)
%46 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 1
store i64 %45, i64* %46
%47 = load i32, i32* @PTRACE_PEEKUSER
%48 = load i64, i64* %2
%49 = load i32, i32* @RDX
%50 = mul nsw i32 8, %49
%51 = call i64 @ptrace(i32 %47, i64 %48, i32 %50, i32* null)
%52 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 2
store i64 %51, i64* %52
%53 = load i32, i32* @stdout
%54 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
%55 = load i64, i64* %54
%56 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 1
%57 = load i64, i64* %56
%58 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 2
%59 = load i64, i64* %58
%60 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 %55, i64 %57, i64 %59)
br label %70
61: ; preds = %31
%62 = load i32, i32* @PTRACE_PEEKUSER
%63 = load i64, i64* %2
%64 = load i32, i32* @RAX
%65 = mul nsw i32 8, %64
%66 = call i64 @ptrace(i32 %62, i64 %63, i32 %65, i32* null)
store i64 %66, i64* %4
%67 = load i32, i32* @stdout
%68 = load i64, i64* %4
%69 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %68)
store i32 0, i32* %7
br label %70
70: ; preds = %61, %34
br label %71
71: ; preds = %70, %22
%72 = load i32, i32* @PTRACE_SYSCALL
%73 = load i64, i64* %2
%74 = call i64 @ptrace(i32 %72, i64 %73, i32 0, i32* null)
br label %16
75: ; preds = %21
br label %76
76: ; preds = %75, %11
ret i32 0
}

LLVM IR (-O2):
@PTRACE_TRACEME = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@PTRACE_PEEKUSER = global i32 0
@ORIG_RAX = global i32 0
@SYS_write = global i64 0
@RBX = global i32 0
@RCX = global i32 0
@RDX = global i32 0
@stdout = global i32 0
@.str.2 = [33 x i8] c"Write caled with %ld, %ld, %ld \0A\00"
@RAX = global i32 0
@.str.3 = [25 x i8] c"Write returned with %ld \00"
@PTRACE_SYSCALL = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = call i32 @wait(i32* %1)
%7 = load i32, i32* %1
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %14, label %53
10: ; preds = %0
%11 = load i32, i32* @PTRACE_TRACEME
%12 = tail call i64 @ptrace(i32 %11, i64 0, i32 0, i32* null)
%13 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %53
14: ; preds = %5, %45
%15 = phi i32 [ %46, %45 ], [ 0, %5 ]
%16 = load i32, i32* @PTRACE_PEEKUSER
%17 = load i32, i32* @ORIG_RAX
%18 = shl nsw i32 %17, 3
%19 = call i64 @ptrace(i32 %16, i64 %3, i32 %18, i32* null)
%20 = load i64, i64* @SYS_write
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %45
22: ; preds = %14
%23 = icmp eq i32 %15, 0
%24 = load i32, i32* @PTRACE_PEEKUSER
br i1 %23, label %25, label %39
25: ; preds = %22
%26 = load i32, i32* @RBX
%27 = shl nsw i32 %26, 3
%28 = call i64 @ptrace(i32 %24, i64 %3, i32 %27, i32* null)
%29 = load i32, i32* @PTRACE_PEEKUSER
%30 = load i32, i32* @RCX
%31 = shl nsw i32 %30, 3
%32 = call i64 @ptrace(i32 %29, i64 %3, i32 %31, i32* null)
%33 = load i32, i32* @PTRACE_PEEKUSER
%34 = load i32, i32* @RDX
%35 = shl nsw i32 %34, 3
%36 = call i64 @ptrace(i32 %33, i64 %3, i32 %35, i32* null)
%37 = load i32, i32* @stdout
%38 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 %28, i64 %32, i64 %36)
br label %45
39: ; preds = %22
%40 = load i32, i32* @RAX
%41 = shl nsw i32 %40, 3
%42 = call i64 @ptrace(i32 %24, i64 %3, i32 %41, i32* null)
%43 = load i32, i32* @stdout
%44 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %42)
br label %45
45: ; preds = %25, %39, %14
%46 = phi i32 [ 1, %25 ], [ 0, %39 ], [ %15, %14 ]
%47 = load i32, i32* @PTRACE_SYSCALL
%48 = call i64 @ptrace(i32 %47, i64 %3, i32 0, i32* null)
%49 = call i32 @wait(i32* %1)
%50 = load i32, i32* %1
%51 = call i64 @WIFEXITED(i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %14, label %53
53: ; preds = %45, %5, %10
ret i32 0
}