#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_4__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ header;
struct TYPE_4__ {
  int e_shoff;
  int e_shnum;
  int e_shstrndx;
};
typedef TYPE_1__ Elf32_Ehdr;

/* Variables and functions */
TYPE_1__ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ MS_SYNC;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int msync(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(TYPE_1__ *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int f;
  static Elf32_Ehdr *header;
  printf(".: Elf corrupt :.\n");
  if (argc < 2) {
    printf("Usage: %s file", argv[0]);
    return 1;
  }
  if ((f = open(argv[1], O_RDWR)) < 0) {
    perror("open");
    return 1;
  }
  if ((header = (Elf32_Ehdr *)mmap(NULL, sizeof(header), PROT_READ | PROT_WRITE,
                                   MAP_SHARED, f, 0)) == MAP_FAILED) {
    perror("mmap");
    close(f);
    return 1;
  }
  printf("[*] Current header values:\n");
  printf("\te_shoff:%d\n\te_shnum:%d\n\te_shstrndx:%d\n", header->e_shoff,
         header->e_shnum, header->e_shstrndx);
  header->e_shoff = 0xffff;
  header->e_shnum = 0xffff;
  header->e_shstrndx = 0xffff;
  printf("[*] Patched header values:\n");
  printf("\te_shoff:%d\n\te_shnum:%d\n\te_shstrndx:%d\n", header->e_shoff,
         header->e_shnum, header->e_shstrndx);
  if (msync(NULL, 0, MS_SYNC) == -1) {
    perror("msync");
    close(f);
    return 1;
  }
  close(f);
  munmap(header, 0);
  printf("You should no more be able to run \"%s\" inside GDB\n", argv[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11bf <main+0x4f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136b <main+0x1fb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f9 <main+0x89>
lea    0xe3a(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136b <main+0x1fb>
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    0x2db7(%rip),%rax
or     (%rax),%edx
mov    0x2dd6(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x8,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,0x2e30(%rip)
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
jne    1260 <main+0xf0>
lea    0xde0(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    136b <main+0x1fb>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
mov    0x4(%rax),%edx
mov    0x2dd8(%rip),%rax
mov    0x8(%rax),%ecx
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dc0(%rip),%rax
movl   $0xffff,(%rax)
mov    0x2db3(%rip),%rax
movl   $0xffff,0x4(%rax)
mov    0x2da5(%rip),%rax
movl   $0xffff,0x8(%rax)
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
mov    0x4(%rax),%edx
mov    0x2d76(%rip),%rax
mov    0x8(%rax),%ecx
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1030 <msync@plt>
cmp    $0xffffffff,%eax
jne    1338 <main+0x1c8>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    136b <main+0x1fb>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2d19(%rip),%rdi
xor    %esi,%esi
call   1070 <munmap@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xee7(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x1,%ebp
jg     11b8 <main+0x38>
mov    (%rbx),%rsi
lea    0xe59(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    12c6 <main+0x146>
mov    0x8(%rbx),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     12d3 <main+0x153>
mov    %eax,%r14d
mov    0x2e1c(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dfa(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,0x2e5f(%rip)
mov    0x2da8(%rip),%rcx
cmp    %rax,(%rcx)
je     12e6 <main+0x166>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edx
mov    0x8(%rax),%ecx
lea    0xdde(%rip),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e1a(%rip),%rax
movabs $0xffff0000ffff,%rcx
mov    %rcx,(%rax)
movl   $0xffff,0x8(%rax)
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edx
mov    0x8(%rax),%ecx
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <msync@plt>
cmp    $0xffffffff,%eax
je     12ef <main+0x16f>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x2dbb(%rip),%rdi
xor    %esi,%esi
call   1080 <munmap@plt>
mov    0x8(%rbx),%rsi
lea    0xd88(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    12c6 <main+0x146>
lea    0xd27(%rip),%rdi
jmp    12f6 <main+0x176>
lea    0xd4b(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0x1,%ebp
jmp    12c6 <main+0x146>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i32 }
@main.header = internal global %struct.TYPE_4__* null
@.str = [19 x i8] c".: Elf corrupt :.\0A\00"
@.str.1 = [15 x i8] c"Usage: %s file\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global %struct.TYPE_4__* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [28 x i8] c"[*] Current header values:\0A\00"
@.str.5 = [40 x i8] c"\09e_shoff:%d\0A\09e_shnum:%d\0A\09e_shstrndx:%d\0A\00"
@.str.6 = [28 x i8] c"[*] Patched header values:\0A\00"
@MS_SYNC = global i32 0
@.str.7 = [6 x i8] c"msync\00"
@.str.8 = [51 x i8] c"You should no more be able to run \22%s\22 inside GDB\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i32 1, i32* %3
br label %83
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %83
24: ; preds = %15
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %6
%30 = call i64 @mmap(i32* null, i32 8, i32 %27, i32 %28, i32 %29, i32 0)
%31 = inttoptr i64 %30 to %struct.TYPE_4__*
store %struct.TYPE_4__* %31, %struct.TYPE_4__** @main.header
%32 = load %struct.TYPE_4__*, %struct.TYPE_4__** @MAP_FAILED
%33 = icmp eq %struct.TYPE_4__* %31, %32
br i1 %33, label %34, label %38
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
store i32 1, i32* %3
br label %83
38: ; preds = %24
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%40 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%44 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%47 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %46, i32 0, i32 2
%48 = load i32, i32* %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %42, i32 %45, i32 %48)
%50 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%51 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %50, i32 0, i32 0
store i32 65535, i32* %51
%52 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%53 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %52, i32 0, i32 1
store i32 65535, i32* %53
%54 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%55 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %54, i32 0, i32 2
store i32 65535, i32* %55
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%57 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%61 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%64 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %63, i32 0, i32 2
%65 = load i32, i32* %64
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %59, i32 %62, i32 %65)
%67 = load i32, i32* @MS_SYNC
%68 = call i32 @msync(i32* null, i32 0, i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %74
70: ; preds = %38
%71 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
store i32 1, i32* %3
br label %83
74: ; preds = %38
%75 = load i32, i32* %6
%76 = call i32 @close(i32 %75)
%77 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%78 = call i32 @munmap(%struct.TYPE_4__* %77, i32 0)
%79 = load i8**, i8*** %5
%80 = getelementptr inbounds i8*, i8** %79, i64 1
%81 = load i8*, i8** %80
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i8* %81)
store i32 0, i32* %3
br label %83
83: ; preds = %74, %70, %34, %22, %10
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32, i32 }
@main.header = internal unnamed_addr global %struct.TYPE_4__* null
@.str.1 = [15 x i8] c"Usage: %s file\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global %struct.TYPE_4__* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.5 = [40 x i8] c"\09e_shoff:%d\0A\09e_shnum:%d\0A\09e_shstrndx:%d\0A\00"
@MS_SYNC = global i32 0
@.str.7 = [6 x i8] c"msync\00"
@.str.8 = [51 x i8] c"You should no more be able to run \22%s\22 inside GDB\0A\00"
@str = [18 x i8] c".: Elf corrupt :.\00"
@str.9 = [27 x i8] c"[*] Current header values:\00"
@str.10 = [27 x i8] c"[*] Patched header values:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %63
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %63
16: ; preds = %8
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i64 @mmap(i32* null, i32 8, i32 %19, i32 %20, i32 %12, i32 0)
%22 = inttoptr i64 %21 to %struct.TYPE_4__*
store %struct.TYPE_4__* %22, %struct.TYPE_4__** @main.header
%23 = load %struct.TYPE_4__*, %struct.TYPE_4__** @MAP_FAILED
%24 = icmp eq %struct.TYPE_4__* %23, %22
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @close(i32 %12)
br label %63
28: ; preds = %16
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
%30 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%31 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %30, i64 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %30, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %30, i64 0, i32 2
%36 = load i32, i32* %35
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %32, i32 %34, i32 %36)
%38 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%39 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %38, i64 0, i32 0
store i32 65535, i32* %39
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %38, i64 0, i32 1
store i32 65535, i32* %40
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %38, i64 0, i32 2
store i32 65535, i32* %41
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.10, i64 0, i64 0))
%43 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%44 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %43, i64 0, i32 0
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %43, i64 0, i32 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %43, i64 0, i32 2
%49 = load i32, i32* %48
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %45, i32 %47, i32 %49)
%51 = load i32, i32* @MS_SYNC
%52 = tail call i32 @msync(i32* null, i32 0, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %28
%55 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%56 = tail call i32 @close(i32 %12)
br label %63
57: ; preds = %28
%58 = tail call i32 @close(i32 %12)
%59 = load %struct.TYPE_4__*, %struct.TYPE_4__** @main.header
%60 = tail call i32 @munmap(%struct.TYPE_4__* %59, i32 0)
%61 = load i8*, i8** %9
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %57, %54, %25, %14, %5
%64 = phi i32 [ 1, %5 ], [ 1, %14 ], [ 1, %25 ], [ 1, %54 ], [ 0, %57 ]
ret i32 %64
}