#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
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 /*<<< orphan*/ stderr;
int vfork();
int waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
void untraceable(char *argv0) {
  char proc[80];
  int pid, mine;
  switch (pid = vfork()) {
  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 {
      fprintf(stderr, "%s: Being traced!\n", 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)
call   10b0 <vfork@plt>
mov    %eax,-0x64(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x6c(%rbp)
sub    $0xffffffff,%eax
je     12cf <untraceable+0x11f>
jmp    11d7 <untraceable+0x27>
mov    -0x6c(%rbp),%eax
test   %eax,%eax
jne    12d4 <untraceable+0x124>
jmp    11e7 <untraceable+0x37>
mov    $0x0,%al
call   10d0 <getppid@plt>
mov    %eax,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%edx
lea    0xe01(%rip),%rsi
call   10c0 <sprintf@plt>
xor    %edi,%edi
call   1040 <close@plt>
lea    -0x60(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
or     (%rax),%esi
call   1090 <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    127d <untraceable+0xcd>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    0x2d58(%rip),%rcx
cmp    (%rcx),%eax
je     127d <untraceable+0xcd>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x64(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <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     129d <untraceable+0xed>
mov    -0x64(%rbp),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
jmp    12c7 <untraceable+0x117>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x64(%rbp),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x68(%rbp),%edi
call   1030 <_exit@plt>
jmp    12fe <untraceable+0x14e>
mov    -0x64(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x64(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <waitpid@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%eax
cmp    %ecx,%eax
jne    12f9 <untraceable+0x149>
add    $0x70,%rsp
pop    %rbp
ret
jmp    12fe <untraceable+0x14e>
mov    -0x8(%rbp),%rdi
call   10a0 <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
call   10b0 <vfork@plt>
cmp    $0xffffffff,%eax
je     11e7 <untraceable+0x37>
mov    %eax,%ebp
test   %eax,%eax
je     11f9 <untraceable+0x49>
mov    %ebp,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <waitpid@plt>
cmp    %eax,%ebp
jne    11e7 <untraceable+0x37>
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
call   10d0 <getppid@plt>
mov    %eax,%r14d
lea    0xdf4(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %eax,%edx
call   10c0 <sprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2dc1(%rip),%rax
mov    0x2d92(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
mov    $0x1,%r15d
mov    0x2d99(%rip),%rbp
test   %eax,%eax
je     129a <untraceable+0xea>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%eax
je     1278 <untraceable+0xc8>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    %r14d,%esi
mov    %ecx,%edx
call   1070 <ptrace@plt>
test   %eax,%eax
je     129a <untraceable+0xea>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
xor    %r15d,%r15d
mov    0x2d16(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r14d,%edi
call   1060 <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
@stderr = global i32 0
@.str.1 = [19 x i8] c"%s: Being traced!\0A\00"
@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 @vfork()
store i32 %6, i32* %4
switch i32 %6, label %52 [
i32 0, label %7
i32 -1, label %51
]
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 %48
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %2
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %4
%46 = load i32, i32* @SIGKILL
%47 = call i32 @kill(i32 %45, i32 %46)
br label %48
48: ; preds = %41, %37
%49 = load i32, i32* %5
%50 = call i32 @_exit(i32 %49)
unreachable
51: ; preds = %1
br label %59
52: ; preds = %1
%53 = load i32, i32* %4
%54 = load i32, i32* %4
%55 = call i32 @waitpid(i32 %54, i32 0, i32 0)
%56 = icmp eq i32 %53, %55
br i1 %56, label %57, label %58
57: ; preds = %52
ret void
58: ; preds = %52
br label %59
59: ; preds = %58, %51
%60 = load i8*, i8** %2
%61 = call i32 @perror(i8* %60)
%62 = 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
@stderr = global i32 0
@.str.1 = [19 x i8] c"%s: Being traced!\0A\00"
@SIGKILL = global i32 0
define void @untraceable(i8* %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = call i32 @vfork()
switch i32 %4, label %31 [
i32 0, label %5
i32 -1, label %35
]
5: ; preds = %1
%6 = 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 = 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 %25, 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 = call i32 @ptrace(i32 %19, i32 %6, i32 0, i32 0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %14, %18
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %18, %5, %22
%26 = phi i32* [ @SIGKILL, %22 ], [ @SIGCONT, %5 ], [ @SIGCONT, %18 ]
%27 = phi i32 [ 0, %22 ], [ 1, %5 ], [ 1, %18 ]
%28 = load i32, i32* %26
%29 = call i32 @kill(i32 %6, i32 %28)
%30 = call i32 @_exit(i32 %27)
unreachable
31: ; preds = %1
%32 = call i32 @waitpid(i32 %4, i32 0, i32 0)
%33 = icmp eq i32 %4, %32
br i1 %33, label %34, label %35
34: ; preds = %31
ret void
35: ; preds = %31, %1
%36 = call i32 @perror(i8* %0)
%37 = call i32 @_exit(i32 1)
unreachable
}