#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 */

/* Variables and functions */
int /*<<< orphan*/ EBUSY;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ SIGCONT;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int fork();
int getppid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
void untraceable(char *argv0) {
  char proc[80];
  int pid, mine;
  switch (pid = fork()) {
  case 0:
    pid = getppid();
    sprintf(proc, "/proc/%d/as", (int)pid);
    close(0);
    mine = !open(proc, O_RDWR | O_EXCL);
    if (!mine && errno != EBUSY)
      mine = !ptrace(PTRACE_ATTACH, pid, 0, 0);
    if (mine) {
      kill(pid, SIGCONT);
    } else {
      perror(argv0);
      kill(pid, SIGKILL);
    }
    _exit(mine);
  case -1:
    break;
  default:
    if (pid == waitpid(pid, 0, 0))
      return;
  }
  perror(argv0);
  _exit(1);
}


x86 assembly (clang, -O0):
<untraceable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x64(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x6c(%rbp)
sub    $0xffffffff,%eax
je     12b1 <untraceable+0x111>
jmp    11c9 <untraceable+0x29>
mov    -0x6c(%rbp),%eax
test   %eax,%eax
jne    12b6 <untraceable+0x116>
jmp    11d9 <untraceable+0x39>
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    %eax,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%edx
lea    0xe0f(%rip),%rsi
call   10a0 <sprintf@plt>
xor    %edi,%edi
call   1040 <close@plt>
lea    -0x60(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jne    126f <untraceable+0xcf>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%eax
je     126f <untraceable+0xcf>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    -0x64(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1060 <ptrace@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
je     128f <untraceable+0xef>
mov    -0x64(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
jmp    12a9 <untraceable+0x109>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    -0x64(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    -0x68(%rbp),%edi
call   1030 <_exit@plt>
jmp    12e0 <untraceable+0x140>
mov    -0x64(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x64(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <waitpid@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%eax
cmp    %ecx,%eax
jne    12db <untraceable+0x13b>
add    $0x70,%rsp
pop    %rbp
ret
jmp    12e0 <untraceable+0x140>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<untraceable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     11d9 <untraceable+0x39>
mov    %eax,%ebp
test   %eax,%eax
je     11eb <untraceable+0x4b>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    %eax,%ebp
jne    11d9 <untraceable+0x39>
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
xor    %eax,%eax
call   10b0 <getppid@plt>
mov    %eax,%r14d
lea    0xe04(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x2dd1(%rip),%rax
mov    0x2daa(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    $0x1,%r15d
mov    0x2da9(%rip),%rbp
test   %eax,%eax
je     127a <untraceable+0xda>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%eax
je     1268 <untraceable+0xc8>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
test   %eax,%eax
je     127a <untraceable+0xda>
mov    %rbx,%rdi
call   1090 <perror@plt>
xor    %r15d,%r15d
mov    0x2d3e(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r14d,%edi
call   1050 <kill@plt>
mov    %r15d,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/proc/%d/as\00"
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@errno = global i32 0
@EBUSY = global i32 0
@PTRACE_ATTACH = global i32 0
@SIGCONT = global i32 0
@SIGKILL = global i32 0
define void @untraceable(i8* %0) {
%2 = alloca i8*
%3 = alloca [80 x i8]
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = call i32 (...) @fork()
store i32 %6, i32* %4
switch i32 %6, label %51 [
i32 0, label %7
i32 -1, label %50
]
7: ; preds = %1
%8 = call i32 (...) @getppid()
store i32 %8, i32* %4
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 @close(i32 0)
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
%20 = zext i1 %19 to i32
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %34, label %23
23: ; preds = %7
%24 = load i32, i32* @errno
%25 = load i32, i32* @EBUSY
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %34
27: ; preds = %23
%28 = load i32, i32* @PTRACE_ATTACH
%29 = load i32, i32* %4
%30 = call i32 @ptrace(i32 %28, i32 %29, i32 0, i32 0)
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
%33 = zext i1 %32 to i32
store i32 %33, i32* %5
br label %34
34: ; preds = %27, %23, %7
%35 = load i32, i32* %5
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i32, i32* %4
%39 = load i32, i32* @SIGCONT
%40 = call i32 @kill(i32 %38, i32 %39)
br label %47
41: ; preds = %34
%42 = load i8*, i8** %2
%43 = call i32 @perror(i8* %42)
%44 = load i32, i32* %4
%45 = load i32, i32* @SIGKILL
%46 = call i32 @kill(i32 %44, i32 %45)
br label %47
47: ; preds = %41, %37
%48 = load i32, i32* %5
%49 = call i32 @_exit(i32 %48)
unreachable
50: ; preds = %1
br label %58
51: ; preds = %1
%52 = load i32, i32* %4
%53 = load i32, i32* %4
%54 = call i32 @waitpid(i32 %53, i32 0, i32 0)
%55 = icmp eq i32 %52, %54
br i1 %55, label %56, label %57
56: ; preds = %51
ret void
57: ; preds = %51
br label %58
58: ; preds = %57, %50
%59 = load i8*, i8** %2
%60 = call i32 @perror(i8* %59)
%61 = call i32 @_exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"/proc/%d/as\00"
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@errno = global i32 0
@EBUSY = global i32 0
@PTRACE_ATTACH = global i32 0
@SIGCONT = global i32 0
@SIGKILL = global i32 0
define void @untraceable(i8* nocapture %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @fork()
switch i32 %4, label %30 [
i32 0, label %5
i32 -1, label %34
]
5: ; preds = %1
%6 = tail call i32 (...) @getppid()
%7 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @close(i32 0)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_EXCL
%11 = or i32 %10, %9
%12 = call i32 @open(i8* %3, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %5
%15 = load i32, i32* @errno
%16 = load i32, i32* @EBUSY
%17 = icmp eq i32 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* @PTRACE_ATTACH
%20 = tail call i32 @ptrace(i32 %19, i32 %6, i32 0, i32 0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %14, %18
%23 = tail call i32 @perror(i8* %0)
br label %24
24: ; preds = %18, %5, %22
%25 = phi i32* [ @SIGKILL, %22 ], [ @SIGCONT, %5 ], [ @SIGCONT, %18 ]
%26 = phi i32 [ 0, %22 ], [ 1, %5 ], [ 1, %18 ]
%27 = load i32, i32* %25
%28 = tail call i32 @kill(i32 %6, i32 %27)
%29 = tail call i32 @_exit(i32 %26)
unreachable
30: ; preds = %1
%31 = tail call i32 @waitpid(i32 %4, i32 0, i32 0)
%32 = icmp eq i32 %4, %31
br i1 %32, label %33, label %34
33: ; preds = %30
ret void
34: ; preds = %30, %1
%35 = tail call i32 @perror(i8* %0)
%36 = tail call i32 @_exit(i32 1)
unreachable
}