#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*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
unsigned long long strtol(char *, char **, int);
int main() {
  FILE *f;
  char line[256];
  char *ptr;
  f = fopen("/proc/self/maps", "r");
  if (f == NULL) {
    perror("Error opening maps file\n");
    exit(1);
  }
  fgets(line, 256, f);
  unsigned long long start = strtol(line, &ptr, 16);
  printf("start is %llx\n", start);
  char *wr = (char *)(start - 100);
  *wr = 4;
  int val = *wr;
  printf("val is %d\n", val);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11aa <main+0x4a>
lea    0xe77(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
mov    %eax,%esi
lea    0xe3d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x120(%rbp),%rax
sub    $0x64,%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rax
movb   $0x4,(%rax)
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x12c(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x110,%rsp
lea    0xe91(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d8 <main+0x78>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,%rbx
lea    0xe7a(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
movb   $0x4,-0x64(%rbx)
lea    0xe77(%rip),%rdi
mov    $0x4,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
ret
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"Error opening maps file\0A\00"
@.str.3 = [15 x i8] c"start is %llx\0A\00"
@.str.4 = [11 x i8] c"val is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %2
%17 = call i32 @fgets(i8* %15, i32 256, i32* %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%19 = call i64 @strtol(i8* %18, i8** %4, i32 16)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = trunc i64 %20 to i32
%22 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i64, i64* %5
%24 = sub i64 %23, 100
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %6
%26 = load i8*, i8** %6
store i8 4, i8* %26
%27 = load i8*, i8** %6
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"Error opening maps file\0A\00"
@.str.3 = [15 x i8] c"start is %llx\0A\00"
@.str.4 = [11 x i8] c"val is %d\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca i8*
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast i8** %2 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i32 @fgets(i8* %3, i32 256, i32* %5)
%12 = call i64 @strtol(i8* %3, i8** %2, i32 16)
%13 = trunc i64 %12 to i32
%14 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = add i64 %12, -100
%16 = inttoptr i64 %15 to i8*
store i8 4, i8* %16
%17 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 4)
ret i32 0
}