#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 /*<<< orphan*/ rdx;
  int /*<<< orphan*/ rcx;
  int /*<<< orphan*/ rbx;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int ORIG_RAX;
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_PEEKUSER;
int /*<<< orphan*/ PTRACE_SYSCALL;
int /*<<< orphan*/ PTRACE_TRACEME;
int RAX;
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, struct user_regs_struct *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int *);
int main(void) {
  pid_t child;
  long orig_rax, rax;
  int status;
  int insyscall = 0;
  struct user_regs_struct regs;
  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;
          ptrace(PTRACE_GETREGS, child, NULL, &regs);
          fprintf(stdout,
                  "Write called with "
                  "%llu, %llu, %llu \n",
                  regs.rbx, regs.rcx, regs.rdx);
        } else {
          rax = ptrace(PTRACE_PEEKUSER, child, 8 * RAX, NULL);
          fprintf(stdout,
                  "Write returned "
                  "with %ld ",
                  rax);
          insyscall = 0;
        }
      }
      ptrace(PTRACE_SYSCALL, child, NULL, NULL);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bf <main+0x5f>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   1040 <ptrace@plt>
lea    0xe56(%rip),%rdi
lea    0xe54(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <execl@plt>
jmp    12dd <main+0x17d>
jmp    11c4 <main+0x64>
lea    -0x24(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x24(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e4 <main+0x84>
jmp    12d8 <main+0x178>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df0(%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    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    12bb <main+0x15b>
cmpl   $0x0,-0x28(%rbp)
jne    126e <main+0x10e>
movl   $0x1,-0x28(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
lea    -0x38(%rbp),%rcx
call   1040 <ptrace@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
movslq -0x30(%rbp),%rdx
mov    -0x34(%rbp),%ecx
mov    -0x38(%rbp),%r8d
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    12b6 <main+0x156>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x28(%rbp)
jmp    12bb <main+0x15b>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
jmp    11c4 <main+0x64>
jmp    12dd <main+0x17d>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     128f <main+0x12f>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
call   1070 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
jne    12b8 <main+0x158>
mov    0x2e04(%rip),%rbp
mov    0x2e2d(%rip),%r15
mov    0x2dee(%rip),%r12
lea    0x4(%rsp),%r13
xor    %r14d,%r14d
jmp    1220 <main+0xc0>
nopl   0x0(%rax)
mov    0x0(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%edi
lea    0xe41(%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>
mov    %r13,%rdi
call   1070 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
jne    12b8 <main+0x158>
mov    0x0(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
cmp    (%r15),%rax
jne    11f6 <main+0x96>
test   %r14d,%r14d
jne    11c0 <main+0x60>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
lea    0x8(%rsp),%rcx
call   1040 <ptrace@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
movslq 0x10(%rsp),%rdx
mov    0x8(%rsp),%r8d
mov    0xc(%rsp),%ecx
lea    0xd8b(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%r14d
jmp    11f6 <main+0x96>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0xd56(%rip),%rdi
lea    0xd54(%rip),%rsi
xor    %edx,%edx
call   1060 <execl@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.user_regs_struct = type { i32, i32, i32 }
@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
@PTRACE_GETREGS = global i32 0
@stdout = global i32 0
@.str.2 = [37 x i8] c"Write called with %llu, %llu, %llu \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 i32
%6 = alloca i32
%7 = alloca %struct.user_regs_struct
store i32 0, i32* %1
store i32 0, i32* %6
%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, %struct.user_regs_struct* 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 %62
15: ; preds = %0
br label %16
16: ; preds = %15, %57
%17 = call i32 @wait(i32* %5)
%18 = load i32, i32* %5
%19 = call i64 @WIFEXITED(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %61
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, %struct.user_regs_struct* 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 %57
31: ; preds = %22
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %31
store i32 1, i32* %6
%35 = load i32, i32* @PTRACE_GETREGS
%36 = load i64, i64* %2
%37 = call i64 @ptrace(i32 %35, i64 %36, i32 0, %struct.user_regs_struct* %7)
%38 = load i32, i32* @stdout
%39 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 2
%40 = load i32, i32* %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i64 %41, i32 %43, i32 %45)
br label %56
47: ; preds = %31
%48 = load i32, i32* @PTRACE_PEEKUSER
%49 = load i64, i64* %2
%50 = load i32, i32* @RAX
%51 = mul nsw i32 8, %50
%52 = call i64 @ptrace(i32 %48, i64 %49, i32 %51, %struct.user_regs_struct* null)
store i64 %52, i64* %4
%53 = load i32, i32* @stdout
%54 = load i64, i64* %4
%55 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %54)
store i32 0, i32* %6
br label %56
56: ; preds = %47, %34
br label %57
57: ; preds = %56, %22
%58 = load i32, i32* @PTRACE_SYSCALL
%59 = load i64, i64* %2
%60 = call i64 @ptrace(i32 %58, i64 %59, i32 0, %struct.user_regs_struct* null)
br label %16
61: ; preds = %21
br label %62
62: ; preds = %61, %11
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32, i32, i32 }
@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
@PTRACE_GETREGS = global i32 0
@stdout = global i32 0
@.str.2 = [37 x i8] c"Write called with %llu, %llu, %llu \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 %struct.user_regs_struct
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.user_regs_struct* %2 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %0
%8 = call i32 @wait(i32* %1)
%9 = load i32, i32* %1
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %54
12: ; preds = %7
%13 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %2, i64 0, i32 2
%14 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %2, i64 0, i32 1
%15 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %2, i64 0, i32 0
br label %20
16: ; preds = %0
%17 = load i32, i32* @PTRACE_TRACEME
%18 = tail call i64 @ptrace(i32 %17, i64 0, i32 0, %struct.user_regs_struct* null)
%19 = 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 %54
20: ; preds = %12, %46
%21 = phi i32 [ 0, %12 ], [ %47, %46 ]
%22 = load i32, i32* @PTRACE_PEEKUSER
%23 = load i32, i32* @ORIG_RAX
%24 = shl nsw i32 %23, 3
%25 = call i64 @ptrace(i32 %22, i64 %5, i32 %24, %struct.user_regs_struct* null)
%26 = load i64, i64* @SYS_write
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %46
28: ; preds = %20
%29 = icmp eq i32 %21, 0
br i1 %29, label %30, label %39
30: ; preds = %28
%31 = load i32, i32* @PTRACE_GETREGS
%32 = call i64 @ptrace(i32 %31, i64 %5, i32 0, %struct.user_regs_struct* %2)
%33 = load i32, i32* @stdout
%34 = load i32, i32* %13
%35 = sext i32 %34 to i64
%36 = load i32, i32* %14
%37 = load i32, i32* %15
%38 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i64 %35, i32 %36, i32 %37)
br label %46
39: ; preds = %28
%40 = load i32, i32* @PTRACE_PEEKUSER
%41 = load i32, i32* @RAX
%42 = shl nsw i32 %41, 3
%43 = call i64 @ptrace(i32 %40, i64 %5, i32 %42, %struct.user_regs_struct* null)
%44 = load i32, i32* @stdout
%45 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %43)
br label %46
46: ; preds = %30, %39, %20
%47 = phi i32 [ 1, %30 ], [ 0, %39 ], [ %21, %20 ]
%48 = load i32, i32* @PTRACE_SYSCALL
%49 = call i64 @ptrace(i32 %48, i64 %5, i32 0, %struct.user_regs_struct* null)
%50 = call i32 @wait(i32* %1)
%51 = load i32, i32* %1
%52 = call i64 @WIFEXITED(i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %20, label %54
54: ; preds = %46, %7, %16
ret i32 0
}