#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, void *);
int gnu_ptrace_init();
void __attribute__((__no_instrument_function__))
gnu_ptrace(char *what, void *p) {
  static int first = 1;
  static int active = 1;
  FILE *trace;
  if (first) {
    active = gnu_ptrace_init();
  }
  if (active == 0)
    return;
  if ((trace = fopen("TRACE", "a")) == NULL) {
    return;
  }
  if (first) {
    first = 0;
  };
  fprintf(trace, "%s %p\n", what, p);
  fclose(trace);
  return;
}


x86 assembly (clang, -O0):
<gnu_ptrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,0x2ee9(%rip)
je     116a <gnu_ptrace+0x2a>
mov    $0x0,%al
call   1060 <gnu_ptrace_init@plt>
mov    %eax,0x2eda(%rip)
cmpl   $0x0,0x2ed3(%rip)
jne    117c <gnu_ptrace+0x3c>
jmp    11da <gnu_ptrace+0x9a>
lea    0xe7d(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a2 <gnu_ptrace+0x62>
jmp    11da <gnu_ptrace+0x9a>
cmpl   $0x0,0x2e97(%rip)
je     11b9 <gnu_ptrace+0x79>
movl   $0x0,0x2e87(%rip)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe3c(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gnu_ptrace>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
cmpb   $0x0,0x2ef6(%rip)
je     1160 <gnu_ptrace+0x20>
mov    0x2ee6(%rip),%eax
test   %eax,%eax
jne    1171 <gnu_ptrace+0x31>
jmp    11be <gnu_ptrace+0x7e>
xor    %eax,%eax
call   1060 <gnu_ptrace_init@plt>
mov    %eax,0x2ed3(%rip)
test   %eax,%eax
je     11be <gnu_ptrace+0x7e>
lea    0xe88(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11be <gnu_ptrace+0x7e>
mov    %rax,%rbx
cmpb   $0x0,0x2eb5(%rip)
jne    119c <gnu_ptrace+0x5c>
movb   $0x1,0x2eac(%rip)
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@gnu_ptrace.first = internal global i32 1
@gnu_ptrace.active = internal global i32 1
@.str = [6 x i8] c"TRACE\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [7 x i8] c"%s %p\0A\00"
define void @gnu_ptrace(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i32, i32* @gnu_ptrace.first
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 (...) @gnu_ptrace_init()
store i32 %9, i32* @gnu_ptrace.active
br label %10
10: ; preds = %8, %2
%11 = load i32, i32* @gnu_ptrace.active
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10
br label %29
14: ; preds = %10
%15 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %14
br label %29
18: ; preds = %14
%19 = load i32, i32* @gnu_ptrace.first
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* @gnu_ptrace.first
br label %22
22: ; preds = %21, %18
%23 = load i32*, i32** %5
%24 = load i8*, i8** %3
%25 = load i8*, i8** %4
%26 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %25)
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
br label %29
29: ; preds = %22, %17, %13
ret void
}

LLVM IR (-O2):
@gnu_ptrace.first = internal unnamed_addr global i1 false
@gnu_ptrace.active = internal unnamed_addr global i32 1
@.str = [6 x i8] c"TRACE\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [7 x i8] c"%s %p\0A\00"
define void @gnu_ptrace(i8* %0, i8* %1) {
%3 = load i1, i1* @gnu_ptrace.first
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (...) @gnu_ptrace_init()
store i32 %5, i32* @gnu_ptrace.active
br label %8
6: ; preds = %2
%7 = load i32, i32* @gnu_ptrace.active
br label %8
8: ; preds = %6, %4
%9 = phi i32 [ %7, %6 ], [ %5, %4 ]
%10 = icmp eq i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = load i1, i1* @gnu_ptrace.first
br i1 %15, label %17, label %16
16: ; preds = %14
store i1 true, i1* @gnu_ptrace.first
br label %17
17: ; preds = %16, %14
%18 = tail call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%19 = tail call i32 @fclose(i32* %12)
br label %20
20: ; preds = %11, %8, %17
ret void
}