#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*/ Get_Data(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                            int);
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_DETACH;
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_SETREGS;
int /*<<< orphan*/ Put_Data(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                            int);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ 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, newregs;
  long ins;
  char code[] = {0xcd, 0x80, 0xcc, 0};
  char backup[4];
  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);
  Get_Data(traced_process, regs.rip, backup, 3);
  Put_Data(traced_process, regs.rip, code, 3);
  ptrace(PTRACE_CONT, traced_process, NULL, NULL);
  wait(NULL);
  fprintf(stdout, "The process stopped, putting back "
                  "the originak Instructions ");
  fprintf(stdout, "Press <enter> to continue ");
  getchar();
  Put_Data(traced_process, regs.rip, backup, 3);
  ptrace(PTRACE_SETREGS, traced_process, NULL, &regs);
  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)
mov    0xe64(%rip),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d9 <main+0x59>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <ptrace@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1070 <ptrace@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x30(%rbp),%rdx
mov    $0x3,%ecx
call   1060 <Get_Data@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x2c(%rbp),%rdx
mov    $0x3,%ecx
call   1050 <Put_Data@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <ptrace@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x30(%rbp),%rdx
mov    $0x3,%ecx
call   1050 <Put_Data@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1070 <ptrace@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <ptrace@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0xcc80cd,0x10(%rsp)
cmp    $0x2,%edi
jne    12a0 <main+0x120>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%r14
mov    %ebx,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1070 <ptrace@plt>
mov    0x8(%rsp),%esi
lea    0x14(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rdx
mov    $0x3,%ecx
call   1060 <Get_Data@plt>
mov    0x8(%rsp),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    $0x3,%ecx
call   1050 <Put_Data@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x2d95(%rip),%r12
mov    (%r12),%edi
lea    0xde1(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r12),%edi
lea    0xe0c(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    0x8(%rsp),%esi
mov    %ebx,%edi
mov    %r15,%rdx
mov    $0x3,%ecx
call   1050 <Put_Data@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1070 <ptrace@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
mov    0x8(%rsi),%rcx
lea    0xd49(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@__const.main.code = [4 x i8] c"\CD\80\CC\00"
@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_CONT = global i32 0
@.str.1 = [61 x i8] c"The process stopped, putting back the originak Instructions \00"
@.str.2 = [27 x i8] c"Press <enter> to continue \00"
@PTRACE_SETREGS = global i32 0
@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 %struct.user_regs_struct
%9 = alloca i64
%10 = alloca [4 x i8]
%11 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [4 x i8]* %10 to i8*
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %25
15: ; preds = %2
%16 = load i32, i32* @stdout
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %6
%30 = load i32, i32* @PTRACE_ATTACH
%31 = load i32, i32* %6
%32 = call i32 @ptrace(i32 %30, i32 %31, i32* null, %struct.user_regs_struct* null)
%33 = call i32 @wait(i32* null)
%34 = load i32, i32* @PTRACE_GETREGS
%35 = load i32, i32* %6
%36 = call i32 @ptrace(i32 %34, i32 %35, i32* null, %struct.user_regs_struct* %7)
%37 = load i32, i32* %6
%38 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%41 = call i32 @Get_Data(i32 %37, i32 %39, i8* %40, i32 3)
%42 = load i32, i32* %6
%43 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%46 = call i32 @Put_Data(i32 %42, i32 %44, i8* %45, i32 3)
%47 = load i32, i32* @PTRACE_CONT
%48 = load i32, i32* %6
%49 = call i32 @ptrace(i32 %47, i32 %48, i32* null, %struct.user_regs_struct* null)
%50 = call i32 @wait(i32* null)
%51 = load i32, i32* @stdout
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* @stdout
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 (...) @getchar()
%56 = load i32, i32* %6
%57 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %7, i32 0, i32 0
%58 = load i32, i32* %57
%59 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%60 = call i32 @Put_Data(i32 %56, i32 %58, i8* %59, i32 3)
%61 = load i32, i32* @PTRACE_SETREGS
%62 = load i32, i32* %6
%63 = call i32 @ptrace(i32 %61, i32 %62, i32* null, %struct.user_regs_struct* %7)
%64 = load i32, i32* @PTRACE_DETACH
%65 = load i32, i32* %6
%66 = call i32 @ptrace(i32 %64, i32 %65, 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_CONT = global i32 0
@.str.1 = [61 x i8] c"The process stopped, putting back the originak Instructions \00"
@.str.2 = [27 x i8] c"Press <enter> to continue \00"
@PTRACE_SETREGS = global i32 0
@PTRACE_DETACH = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.user_regs_struct
%4 = alloca i32
%5 = alloca [4 x i8]
%6 = bitcast %struct.user_regs_struct* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 13402317, i32* %4
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %17, label %10
10: ; preds = %2
%11 = load i32, i32* @stdout
%12 = load i8*, i8** %1
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = load i32, i32* @PTRACE_ATTACH
%22 = tail call i32 @ptrace(i32 %21, i32 %20, i32* null, %struct.user_regs_struct* null)
%23 = tail call i32 @wait(i32* null)
%24 = load i32, i32* @PTRACE_GETREGS
%25 = call i32 @ptrace(i32 %24, i32 %20, i32* null, %struct.user_regs_struct* %3)
%26 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %3, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @Get_Data(i32 %20, i32 %27, i8* %8, i32 3)
%29 = load i32, i32* %26
%30 = call i32 @Put_Data(i32 %20, i32 %29, i8* %7, i32 3)
%31 = load i32, i32* @PTRACE_CONT
%32 = call i32 @ptrace(i32 %31, i32 %20, i32* null, %struct.user_regs_struct* null)
%33 = call i32 @wait(i32* null)
%34 = load i32, i32* @stdout
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @stdout
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 (...) @getchar()
%39 = load i32, i32* %26
%40 = call i32 @Put_Data(i32 %20, i32 %39, i8* %8, i32 3)
%41 = load i32, i32* @PTRACE_SETREGS
%42 = call i32 @ptrace(i32 %41, i32 %20, i32* null, %struct.user_regs_struct* %3)
%43 = load i32, i32* @PTRACE_DETACH
%44 = call i32 @ptrace(i32 %43, i32 %20, i32* null, %struct.user_regs_struct* null)
ret i32 0
}