#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_CONT;
int /*<<< orphan*/ PTRACE_DETACH;
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_SETREGS;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ getdata(int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ hex_dump(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
            struct user_regs_struct *);
int /*<<< orphan*/ putdata(int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
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};
  char backup[20];
  char backup2[20];
  if (argc != 2) {
    printf("Usage: %s <pid to be traced>\n", argv[0]);
    exit(1);
  }
  traced_process = atoi(argv[1]);
  ins = ptrace(PTRACE_ATTACH, traced_process, NULL, NULL);
  printf("attrch: %d\n", ins);
  if (ins == -1) {
    printf("traced failed\n");
    exit(1);
  }
  wait(NULL);
  ins = ptrace(PTRACE_GETREGS, traced_process, NULL, &regs);
  assert(ins == 0);
  getdata(traced_process, regs.rip, backup, 16);
  hex_dump("backup", backup, 16);
  putdata(traced_process, regs.rip, code, 3);
  getdata(traced_process, regs.rip, backup2, 16);
  hex_dump("backup2", backup2, 16);
  ptrace(PTRACE_CONT, traced_process, NULL, NULL);
  wait(NULL);
  printf("Process stopped\n");
  printf("Press <enter> to continue: \n");
  getchar();
  putdata(traced_process, regs.rip, backup, 3);
  ptrace(PTRACE_SETREGS, traced_process, NULL, &regs);
  getdata(traced_process, regs.rip, backup2, 8);
  hex_dump("backup2", backup2, 8);
  ptrace(PTRACE_DETACH, traced_process, NULL, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe4a(%rip),%ax
mov    %ax,-0x2b(%rbp)
mov    0xe42(%rip),%al
mov    %al,-0x29(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11ec <_main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <ptrace@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    124d <_main+0xad>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1080 <ptrace@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x40(%rbp),%rdx
mov    $0x10,%ecx
call   1070 <getdata@plt>
lea    -0x40(%rbp),%rsi
lea    0xd98(%rip),%rdi
mov    $0x10,%edx
call   10b0 <hex_dump@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x2b(%rbp),%rdx
mov    $0x3,%ecx
call   1030 <putdata@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x60(%rbp),%rdx
mov    $0x10,%ecx
call   1070 <getdata@plt>
lea    -0x60(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x10,%edx
call   10b0 <hex_dump@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <ptrace@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x40(%rbp),%rdx
mov    $0x3,%ecx
call   1030 <putdata@plt>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1080 <ptrace@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x60(%rbp),%rdx
mov    $0x8,%ecx
call   1070 <getdata@plt>
lea    -0x60(%rbp),%rsi
lea    0xcc8(%rip),%rdi
mov    $0x8,%edx
call   10b0 <hex_dump@plt>
mov    0x2c54(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <ptrace@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
movb   $0xcc,0xe(%rsp)
movw   $0x80cd,0xc(%rsp)
cmp    $0x2,%edi
jne    1359 <_main+0x1a9>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <ptrace@plt>
mov    %rax,%rbx
lea    0xe25(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xffffffffffffffff,%rbx
je     1374 <_main+0x1c4>
xor    %edi,%edi
call   10d0 <wait@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%r14
mov    %ebp,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1090 <ptrace@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x8(%rsp),%esi
lea    0x30(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rdx
mov    $0x10,%ecx
call   1080 <getdata@plt>
lea    0xdcf(%rip),%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   10c0 <hex_dump@plt>
mov    0x8(%rsp),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
mov    $0x3,%ecx
call   1040 <putdata@plt>
mov    0x8(%rsp),%esi
lea    0x10(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rdx
mov    $0x10,%ecx
call   1080 <getdata@plt>
lea    0xd95(%rip),%r13
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x10,%edx
call   10c0 <hex_dump@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <ptrace@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
lea    0xd6b(%rip),%rdi
call   1030 <puts@plt>
lea    0xd6f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    0x8(%rsp),%esi
mov    %ebp,%edi
mov    %r15,%rdx
mov    $0x3,%ecx
call   1040 <putdata@plt>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1090 <ptrace@plt>
mov    0x8(%rsp),%esi
mov    %ebp,%edi
mov    %r12,%rdx
mov    $0x8,%ecx
call   1080 <getdata@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x8,%edx
call   10c0 <hex_dump@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <ptrace@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xca0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xced(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@__const._main.code = [3 x i8] c"\CD\80\CC"
@.str = [30 x i8] c"Usage: %s <pid to be traced>\0A\00"
@PTRACE_ATTACH = global i32 0
@.str.1 = [12 x i8] c"attrch: %d\0A\00"
@.str.2 = [15 x i8] c"traced failed\0A\00"
@PTRACE_GETREGS = global i32 0
@.str.3 = [7 x i8] c"backup\00"
@.str.4 = [8 x i8] c"backup2\00"
@PTRACE_CONT = global i32 0
@.str.5 = [17 x i8] c"Process stopped\0A\00"
@.str.6 = [29 x i8] c"Press <enter> to continue: \0A\00"
@PTRACE_SETREGS = global i32 0
@PTRACE_DETACH = global i32 0
define i32 @_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca %struct.user_regs_struct
%7 = alloca %struct.user_regs_struct
%8 = alloca i64
%9 = alloca [3 x i8]
%10 = alloca [20 x i8]
%11 = alloca [20 x i8]
store i32 %0, i32* %3
store i8** %1, i8*** %4
%12 = bitcast [3 x i8]* %9 to i8*
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %4
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %4
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %5
%26 = load i32, i32* @PTRACE_ATTACH
%27 = load i32, i32* %5
%28 = call i64 @ptrace(i32 %26, i32 %27, i32* null, %struct.user_regs_struct* null)
store i64 %28, i64* %8
%29 = load i64, i64* %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %29)
%31 = load i64, i64* %8
%32 = icmp eq i64 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %21
%37 = call i32 @wait(i32* null)
%38 = load i32, i32* @PTRACE_GETREGS
%39 = load i32, i32* %5
%40 = call i64 @ptrace(i32 %38, i32 %39, i32* null, %struct.user_regs_struct* %6)
store i64 %40, i64* %8
%41 = load i64, i64* %8
%42 = icmp eq i64 %41, 0
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i32, i32* %5
%46 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 0
%47 = load i32, i32* %46
%48 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%49 = call i32 @getdata(i32 %45, i32 %47, i8* %48, i32 16)
%50 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%51 = call i32 @hex_dump(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %50, i32 16)
%52 = load i32, i32* %5
%53 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%56 = call i32 @putdata(i32 %52, i32 %54, i8* %55, i32 3)
%57 = load i32, i32* %5
%58 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 0
%59 = load i32, i32* %58
%60 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%61 = call i32 @getdata(i32 %57, i32 %59, i8* %60, i32 16)
%62 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%63 = call i32 @hex_dump(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %62, i32 16)
%64 = load i32, i32* @PTRACE_CONT
%65 = load i32, i32* %5
%66 = call i64 @ptrace(i32 %64, i32 %65, i32* null, %struct.user_regs_struct* null)
%67 = call i32 @wait(i32* null)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%70 = call i32 (...) @getchar()
%71 = load i32, i32* %5
%72 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 0
%73 = load i32, i32* %72
%74 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%75 = call i32 @putdata(i32 %71, i32 %73, i8* %74, i32 3)
%76 = load i32, i32* @PTRACE_SETREGS
%77 = load i32, i32* %5
%78 = call i64 @ptrace(i32 %76, i32 %77, i32* null, %struct.user_regs_struct* %6)
%79 = load i32, i32* %5
%80 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 0
%81 = load i32, i32* %80
%82 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%83 = call i32 @getdata(i32 %79, i32 %81, i8* %82, i32 8)
%84 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%85 = call i32 @hex_dump(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %84, i32 8)
%86 = load i32, i32* @PTRACE_DETACH
%87 = load i32, i32* %5
%88 = call i64 @ptrace(i32 %86, i32 %87, i32* null, %struct.user_regs_struct* null)
ret i32 0
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32 }
@__const._main.code = [3 x i8] c"\CD\80\CC"
@.str = [30 x i8] c"Usage: %s <pid to be traced>\0A\00"
@PTRACE_ATTACH = global i32 0
@.str.1 = [12 x i8] c"attrch: %d\0A\00"
@PTRACE_GETREGS = global i32 0
@.str.3 = [7 x i8] c"backup\00"
@.str.4 = [8 x i8] c"backup2\00"
@PTRACE_CONT = global i32 0
@PTRACE_SETREGS = global i32 0
@PTRACE_DETACH = global i32 0
@str = [16 x i8] c"Process stopped\00"
@str.7 = [28 x i8] c"Press <enter> to continue: \00"
@str.8 = [14 x i8] c"traced failed\00"
define i32 @_main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.user_regs_struct
%4 = alloca [3 x i8]
%5 = alloca [20 x i8]
%6 = alloca [20 x i8]
%7 = bitcast %struct.user_regs_struct* %3 to i8*
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i8*, i8** %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = load i32, i32* @PTRACE_ATTACH
%21 = tail call i64 @ptrace(i32 %20, i32 %19, i32* null, %struct.user_regs_struct* null)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %21)
%23 = icmp eq i64 %21, -1
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = tail call i32 @wait(i32* null)
%29 = load i32, i32* @PTRACE_GETREGS
%30 = call i64 @ptrace(i32 %29, i32 %19, i32* null, %struct.user_regs_struct* %3)
%31 = icmp eq i64 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %3, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @getdata(i32 %19, i32 %35, i8* %9, i32 16)
%37 = call i32 @hex_dump(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %9, i32 16)
%38 = load i32, i32* %34
%39 = call i32 @putdata(i32 %19, i32 %38, i8* %8, i32 3)
%40 = load i32, i32* %34
%41 = call i32 @getdata(i32 %19, i32 %40, i8* %10, i32 16)
%42 = call i32 @hex_dump(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %10, i32 16)
%43 = load i32, i32* @PTRACE_CONT
%44 = call i64 @ptrace(i32 %43, i32 %19, i32* null, %struct.user_regs_struct* null)
%45 = call i32 @wait(i32* null)
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%48 = call i32 (...) @getchar()
%49 = load i32, i32* %34
%50 = call i32 @putdata(i32 %19, i32 %49, i8* %9, i32 3)
%51 = load i32, i32* @PTRACE_SETREGS
%52 = call i64 @ptrace(i32 %51, i32 %19, i32* null, %struct.user_regs_struct* %3)
%53 = load i32, i32* %34
%54 = call i32 @getdata(i32 %19, i32 %53, i8* %10, i32 8)
%55 = call i32 @hex_dump(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %10, i32 8)
%56 = load i32, i32* @PTRACE_DETACH
%57 = call i64 @ptrace(i32 %56, i32 %19, i32* null, %struct.user_regs_struct* null)
ret i32 0
}