#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*/ *rip;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_DETACH;
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
            struct user_regs_struct *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  pid_t traced_process;
  struct user_regs_struct regs;
  long ins;
  if (argc != 2) {
    fprintf(stdout, "%s <pid to be traced> ", argv[0], argv[1]);
    exit(1);
  }
  traced_process = atoi(argv[1]);
  ptrace(PTRACE_ATTACH, traced_process, NULL, NULL);
  wait(NULL);
  ptrace(PTRACE_GETREGS, traced_process, NULL, &regs);
  ins = ptrace(PTRACE_PEEKTEXT, traced_process, regs.rip, NULL);
  fprintf(stdout, "lx Instruction executed: %lx", regs.rip, ins);
  ptrace(PTRACE_DETACH, traced_process, NULL, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a0 <main+0x50>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   1040 <ptrace@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    11ed <main+0x9d>
mov    0x8(%rsi),%rdi
call   1050 <atoi@plt>
mov    %eax,%ebx
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
xor    %edi,%edi
call   1070 <wait@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rcx
mov    %ebx,%esi
xor    %edx,%edx
call   1040 <ptrace@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
mov    %ebx,%esi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%edi
mov    0x8(%rsp),%rdx
lea    0xe50(%rip),%rsi
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
mov    0x8(%rsi),%rcx
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.user_regs_struct = type { i32* }
@stdout = global i32 0
@.str = [23 x i8] c"%s <pid to be traced> \00"
@PTRACE_ATTACH = global i32 0
@PTRACE_GETREGS = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@.str.1 = [29 x i8] c"lx Instruction executed: %lx\00"
@PTRACE_DETACH = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.user_regs_struct
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = load i32, i32* @stdout
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* @PTRACE_ATTACH
%27 = load i32, i32* %6
%28 = call i64 @ptrace(i32 %26, i32 %27, i32* null, %struct.user_regs_struct* null)
%29 = call i32 @wait(i32* null)
%30 = load i32, i32* @PTRACE_GETREGS
%31 = load i32, i32* %6
%32 = call i64 @ptrace(i32 %30, i32 %31, i32* null, %struct.user_regs_struct* %7)
%33 = load i32, i32* @PTRACE_PEEKTEXT
%34 = load i32, i32* %6
%35 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 0
%36 = load i32*, i32** %35
%37 = call i64 @ptrace(i32 %33, i32 %34, i32* %36, %struct.user_regs_struct* null)
store i64 %37, i64* %8
%38 = load i32, i32* @stdout
%39 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 0
%40 = load i32*, i32** %39
%41 = bitcast i32* %40 to i8*
%42 = load i64, i64* %8
%43 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %41, i64 %42)
%44 = load i32, i32* @PTRACE_DETACH
%45 = load i32, i32* %6
%46 = call i64 @ptrace(i32 %44, i32 %45, i32* null, %struct.user_regs_struct* null)
ret i32 0
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32* }
@stdout = global i32 0
@.str = [23 x i8] c"%s <pid to be traced> \00"
@PTRACE_ATTACH = global i32 0
@PTRACE_GETREGS = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@.str.1 = [29 x i8] c"lx Instruction executed: %lx\00"
@PTRACE_DETACH = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.user_regs_struct
%4 = bitcast %struct.user_regs_struct* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = load i32, i32* @stdout
%8 = load i8*, i8** %1
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = load i32, i32* @PTRACE_ATTACH
%18 = tail call i64 @ptrace(i32 %17, i32 %16, i32* null, %struct.user_regs_struct* null)
%19 = tail call i32 @wait(i32* null)
%20 = load i32, i32* @PTRACE_GETREGS
%21 = call i64 @ptrace(i32 %20, i32 %16, i32* null, %struct.user_regs_struct* %3)
%22 = load i32, i32* @PTRACE_PEEKTEXT
%23 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %3, i64 0, i32 0
%24 = load i32*, i32** %23
%25 = call i64 @ptrace(i32 %22, i32 %16, i32* %24, %struct.user_regs_struct* null)
%26 = load i32, i32* @stdout
%27 = bitcast %struct.user_regs_struct* %3 to i8**
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %28, i64 %25)
%30 = load i32, i32* @PTRACE_DETACH
%31 = call i64 @ptrace(i32 %30, i32 %16, i32* null, %struct.user_regs_struct* null)
ret i32 0
}