#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 */
struct TYPE_3__ {
  int sh_offset;
  scalar_t__ sh_size;
};
typedef TYPE_1__ Elf64_Shdr;

/* Variables and functions */
unsigned char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
TYPE_1__ *elfi_find_section(unsigned char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int get_file_size(int);
unsigned char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                    int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ xor_block(unsigned char *, scalar_t__);
void encode(char *f) {
  int fd, len;
  unsigned char *p;
  Elf64_Shdr *s;
  printf("+ Encoding\n");
  if ((fd = open(f, O_RDWR, 0)) < 0) {
    perror("open");
    exit(1);
  };
  len = (get_file_size(fd));
  if ((p = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    exit(1);
  };
  if ((s = elfi_find_section(p, ".secure")) == NULL) {
    fprintf(stderr, "- No secure section found... Done\n");
    close(fd);
    exit(1);
  }
  xor_block(p + s->sh_offset, s->sh_size);
  printf("+ Offsets: 0x%x, 0x%x\n", (unsigned int)s->sh_offset,
         (unsigned int)s->sh_size);
  close(fd);
  return;
}


x86 assembly (clang, -O0):
<encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11f0 <encode+0x50>
lea    0xe2b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1080 <get_file_size@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    0x2db2(%rip),%rax
or     (%rax),%edx
mov    0x2dd1(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
jne    1252 <encode+0xb2>
lea    0xdce(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   10c0 <elfi_find_section@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1297 <encode+0xf7>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
call   1070 <xor_block@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %eax,%edx
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     126a <encode+0xba>
mov    %eax,%r14d
mov    %eax,%edi
call   1090 <get_file_size@plt>
mov    0x2e09(%rip),%rcx
mov    0x2dca(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2d9c(%rip),%rax
cmp    (%rax),%rbx
je     1273 <encode+0xc3>
lea    0xde2(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <elfi_find_section@plt>
test   %rax,%rax
je     1289 <encode+0xd9>
mov    %rax,%rbp
movslq (%rax),%rax
add    %rax,%rbx
mov    0x8(%rbp),%rsi
mov    %rbx,%rdi
call   1080 <xor_block@plt>
mov    0x0(%rbp),%esi
mov    0x8(%rbp),%edx
lea    0xdde(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <close@plt>
lea    0xd8f(%rip),%rdi
jmp    127a <encode+0xca>
lea    0xd8b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
call   1070 <fprintf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i64 }
@.str = [12 x i8] c"+ Encoding\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [8 x i8] c".secure\00"
@stderr = global i32 0
@.str.4 = [35 x i8] c"- No secure section found... Done\0A\00"
@.str.5 = [23 x i8] c"+ Offsets: 0x%x, 0x%x\0A\00"
define void @encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.TYPE_3__*
store i8* %0, i8** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9, i32 0)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i32, i32* %3
%17 = call i32 @get_file_size(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %3
%24 = call i8* @mmap(i32 0, i32 %18, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %5
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %15
%31 = load i8*, i8** %5
%32 = call %struct.TYPE_3__* @elfi_find_section(i8* %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store %struct.TYPE_3__* %32, %struct.TYPE_3__** %6
%33 = icmp eq %struct.TYPE_3__* %32, null
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = load i8*, i8** %5
%42 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
%47 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%48 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %47, i32 0, i32 1
%49 = load i64, i64* %48
%50 = call i32 @xor_block(i8* %46, i64 %49)
%51 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%52 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%55 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %54, i32 0, i32 1
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %53, i32 %57)
%59 = load i32, i32* %3
%60 = call i32 @close(i32 %59)
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i64 }
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [8 x i8] c".secure\00"
@stderr = global i32 0
@.str.4 = [35 x i8] c"- No secure section found... Done\0A\00"
@.str.5 = [23 x i8] c"+ Offsets: 0x%x, 0x%x\0A\00"
@str = [11 x i8] c"+ Encoding\00"
define void @encode(i8* nocapture %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* %0, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = tail call i32 @get_file_size(i32 %4)
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32 0, i32 %10, i32 %13, i32 %14, i32 %4, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %9
%22 = tail call %struct.TYPE_3__* @elfi_find_section(i8* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq %struct.TYPE_3__* %22, null
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @close(i32 %4)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i64 0, i32 0
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %15, i64 %32
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i64 0, i32 1
%35 = load i64, i64* %34
%36 = tail call i32 @xor_block(i8* %33, i64 %35)
%37 = load i32, i32* %30
%38 = load i64, i64* %34
%39 = trunc i64 %38 to i32
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %37, i32 %39)
%41 = tail call i32 @close(i32 %4)
ret void
}