#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*/ printf(char *, char *, ...);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
            struct user_regs_struct *);
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) {
    printf("Usage: %s <pid to be traced>\n", 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);
  printf("EIP: %lx Instruction executed: %lx\n", 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     1197 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@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    0x2e32(%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    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   1040 <ptrace@plt>
mov    0x2de0(%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    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dcd(%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    11e4 <main+0x94>
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    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
mov    %ebx,%esi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x8(%rsp),%rsi
lea    0xe60(%rip),%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e19(%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    (%rsi),%rax
mov    0x8(%rsi),%rdx
lea    0xe0e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

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