#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ str;
struct TYPE_3__ {
  scalar_t__ si_addr;
};
typedef TYPE_1__ siginfo_t;

/* Variables and functions */
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
scalar_t__ mprotect(scalar_t__, int, int);
int snprintf(char *, int, char *, ...);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void tracer(int sig, siginfo_t *si, void *u) {
  do {
    char str[8192];
    int n = snprintf(str, sizeof(str), "addr %#lx\n", (long)si->si_addr);
    write(1, str, n);
  } while (0);
  if (mprotect(si->si_addr, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
    do {
      char str[8192];
      int n = snprintf(str, sizeof(str), "%s\n", strerror(errno));
      write(1, str, n);
    } while (0);
    _exit(0);
  }
}


x86 assembly (clang, -O0):
<tracer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x2020(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    $0x2000,%esi
lea    0xe80(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x2024(%rbp)
lea    -0x2020(%rbp),%rsi
mov    -0x2024(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    0x2e0d(%rip),%rax
or     (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
cmp    $0x0,%rax
jge    123c <tracer+0xec>
jmp    11df <tracer+0x8f>
lea    -0x4030(%rbp),%rax
mov    %rax,-0x4040(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x4040(%rbp),%rdi
mov    %rax,%rcx
mov    $0x2000,%esi
lea    0xdfa(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x4034(%rbp)
lea    -0x4030(%rbp),%rsi
mov    -0x4034(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tracer>:
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%rbx
mov    (%rsi),%rcx
lea    0xe99(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x2000,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x2e69(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    (%rbx),%rdi
mov    0x2e3c(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
test   %rax,%rax
js     11c0 <tracer+0x70>
add    $0x2008,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe36(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x2000,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64 }
@.str = [11 x i8] c"addr %#lx\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define void @tracer(i32 %0, %struct.TYPE_3__* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca [8192 x i8]
%8 = alloca i32
%9 = alloca [8192 x i8]
%10 = alloca i32
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
br label %11
11: ; preds = %3
%12 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%13 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %12, i32 8192, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %15)
store i32 %16, i32* %8
%17 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%18 = load i32, i32* %8
%19 = call i32 @write(i32 1, i8* %17, i32 %18)
br label %20
20: ; preds = %11
%21 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @PROT_EXEC
%28 = or i32 %26, %27
%29 = call i64 @mprotect(i64 %23, i32 4096, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %20
br label %32
32: ; preds = %31
%33 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %33, i32 8192, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i32 %36, i32* %10
%37 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%38 = load i32, i32* %10
%39 = call i32 @write(i32 1, i8* %37, i32 %38)
br label %40
40: ; preds = %32
%41 = call i32 @_exit(i32 0)
unreachable
42: ; preds = %20
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64 }
@.str = [11 x i8] c"addr %#lx\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define void @tracer(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture readnone %2) {
%4 = alloca [8192 x i8]
%5 = alloca [8192 x i8]
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%8 = load i64, i64* %7
%9 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 8192, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %8)
%10 = call i32 @write(i32 1, i8* %6, i32 %9)
%11 = load i64, i64* %7
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @PROT_EXEC
%16 = or i32 %14, %15
%17 = call i64 @mprotect(i64 %11, i32 4096, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %3
%20 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %20, i32 8192, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32 @write(i32 1, i8* %20, i32 %23)
%25 = call i32 @_exit(i32 0)
unreachable
26: ; preds = %3
ret void
}