#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*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned int *, unsigned int *,
           char *);
int writeable_size(const char *maps_name) {
  FILE *maps = fopen(maps_name, "r");
  if (!maps)
    return 0;
  unsigned writeable = 0;
  while (true) {
    unsigned start_addr, end_addr;
    char permission[4];
    if (fscanf(maps, "%8x-%8x %4c", &start_addr, &end_addr, permission) != 3)
      break;
    if (permission[1] == 'w' && permission[3] == 'p')
      writeable += (end_addr - start_addr);
    while (fgetc(maps) != '\n')
      ;
  }
  fclose(maps);
  return writeable;
}


x86 assembly (clang, -O0):
<writeable_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <writeable_size+0x37>
movl   $0x0,-0x4(%rbp)
jmp    1201 <writeable_size+0xc1>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%r8
lea    0xe75(%rip),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
call   1040 <fscanf@plt>
cmp    $0x3,%eax
je     11a8 <writeable_size+0x68>
jmp    11f2 <writeable_size+0xb2>
movsbl -0x27(%rbp),%eax
cmp    $0x77,%eax
jne    11ce <writeable_size+0x8e>
movsbl -0x25(%rbp),%eax
cmp    $0x70,%eax
jne    11ce <writeable_size+0x8e>
mov    -0x24(%rbp),%eax
sub    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11d3 <writeable_size+0x93>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     11ed <writeable_size+0xad>
jmp    11d3 <writeable_size+0x93>
jmp    117e <writeable_size+0x3e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeable_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1206 <writeable_size+0xc6>
mov    %rax,%rbx
lea    0xe95(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %rax,%rdi
call   1040 <fscanf@plt>
xor    %ebp,%ebp
cmp    $0x3,%eax
jne    11fc <writeable_size+0xbc>
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    0xd(%rsp),%al
mov    0xf(%rsp),%cl
xor    $0x77,%al
mov    0x10(%rsp),%r14d
sub    0x14(%rsp),%r14d
xor    $0x70,%cl
or     %al,%cl
mov    $0x0,%eax
cmovne %eax,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    $0xa,%al
jne    11d0 <writeable_size+0x90>
add    %r14d,%ebp
mov    %rbx,%rdi
lea    0xe19(%rip),%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %r13,%r8
call   1040 <fscanf@plt>
cmp    $0x3,%eax
je     11a0 <writeable_size+0x60>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1208 <writeable_size+0xc8>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"%8x-%8x %4c\00"
define i32 @writeable_size(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i8]
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i32 0, i32* %2
br label %49
14: ; preds = %1
store i32 0, i32* %5
br label %15
15: ; preds = %14, %44
%16 = load i32*, i32** %4
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%18 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %6, i32* %7, i8* %17)
%19 = icmp ne i32 %18, 3
br i1 %19, label %20, label %21
20: ; preds = %15
br label %45
21: ; preds = %15
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 1
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 119
br i1 %25, label %26, label %37
26: ; preds = %21
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 3
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 112
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = sub i32 %32, %33
%35 = load i32, i32* %5
%36 = add i32 %35, %34
store i32 %36, i32* %5
br label %37
37: ; preds = %31, %26, %21
br label %38
38: ; preds = %43, %37
%39 = load i32*, i32** %4
%40 = call signext i8 @fgetc(i32* %39)
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 10
br i1 %42, label %43, label %44
43: ; preds = %38
br label %38
44: ; preds = %38
br label %15
45: ; preds = %20
%46 = load i32*, i32** %4
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %5
store i32 %48, i32* %2
br label %49
49: ; preds = %45, %13
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"%8x-%8x %4c\00"
define i32 @writeable_size(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4 x i8]
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %37, label %7
7: ; preds = %1
%8 = bitcast i32* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%11 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3, i8* %10)
%12 = icmp eq i32 %11, 3
br i1 %12, label %13, label %34
13: ; preds = %7
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
br label %16
16: ; preds = %13, %30
%17 = phi i32 [ 0, %13 ], [ %31, %30 ]
%18 = load i8, i8* %14
%19 = icmp eq i8 %18, 119
%20 = load i8, i8* %15
%21 = icmp eq i8 %20, 112
%22 = select i1 %19, i1 %21, i1 false
%23 = load i32, i32* %3
%24 = load i32, i32* %2
%25 = sub i32 %23, %24
%26 = select i1 %22, i32 %25, i32 0
br label %27
27: ; preds = %27, %16
%28 = call signext i8 @fgetc(i32* %5)
%29 = icmp eq i8 %28, 10
br i1 %29, label %30, label %27
30: ; preds = %27
%31 = add i32 %26, %17
%32 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3, i8* %10)
%33 = icmp eq i32 %32, 3
br i1 %33, label %16, label %34
34: ; preds = %30, %7
%35 = phi i32 [ 0, %7 ], [ %31, %30 ]
%36 = call i32 @fclose(i32* %5)
br label %37
37: ; preds = %1, %34
%38 = phi i32 [ %35, %34 ], [ 0, %1 ]
ret i32 %38
}