#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 pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, long *, char *, char *);
int /*<<< orphan*/ *strstr(char *, char *);
long freespaceaddr(pid_t pid) {
  FILE *fp;
  char filename[30];
  char line[850];
  long addr;
  char str[20];
  char perms[5];
  sprintf(filename, "/proc/%d/maps", pid);
  fp = fopen(filename, "r");
  if (fp == NULL)
    exit(1);
  while (fgets(line, 850, fp) != NULL) {
    sscanf(line, "%lx-%*lx %s %*s %s %*d", &addr, perms, str);
    if (strstr(perms, "x") != NULL) {
      break;
    }
  }
  fclose(fp);
  return addr;
}


x86 assembly (clang, -O0):
<freespaceaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3c0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe74(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <freespaceaddr+0x4a>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11bf <freespaceaddr+0x4f>
lea    -0x390(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x352,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     122d <freespaceaddr+0xbd>
lea    -0x390(%rbp),%rdi
lea    -0x3b5(%rbp),%rcx
lea    -0x3b0(%rbp),%r8
lea    0xe16(%rip),%rsi
lea    -0x398(%rbp),%rdx
call   1050 <sscanf@plt>
lea    -0x3b5(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1228 <freespaceaddr+0xb8>
jmp    122d <freespaceaddr+0xbd>
jmp    11bf <freespaceaddr+0x4f>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x398(%rbp),%rax
add    $0x3c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freespaceaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3a8,%rsp
mov    %edi,%edx
lea    0xe76(%rip),%rsi
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1229 <freespaceaddr+0xb9>
mov    %rax,%r13
lea    0x50(%rsp),%r14
lea    0xe56(%rip),%r15
mov    %rsp,%r12
lea    0xb(%rsp),%rbx
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x352,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     120b <freespaceaddr+0x9b>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1060 <sscanf@plt>
mov    %rbx,%rdi
mov    $0x78,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d0 <freespaceaddr+0x60>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    (%rsp),%rax
add    $0x3a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"%lx-%*lx %s %*s %s %*d\00"
@.str.3 = [2 x i8] c"x\00"
define i64 @freespaceaddr(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [30 x i8]
%5 = alloca [850 x i8]
%6 = alloca i64
%7 = alloca [20 x i8]
%8 = alloca [5 x i8]
store i32 %0, i32* %2
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %3
%14 = load i32*, i32** %3
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
br label %19
19: ; preds = %33, %18
%20 = getelementptr inbounds [850 x i8], [850 x i8]* %5, i64 0, i64 0
%21 = load i32*, i32** %3
%22 = call i32* @fgets(i8* %20, i32 850, i32* %21)
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = getelementptr inbounds [850 x i8], [850 x i8]* %5, i64 0, i64 0
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%28 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64* %6, i8* %26, i8* %27)
%29 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%30 = call i32* @strstr(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %24
br label %34
33: ; preds = %24
br label %19
34: ; preds = %32, %19
%35 = load i32*, i32** %3
%36 = call i32 @fclose(i32* %35)
%37 = load i64, i64* %6
ret i64 %37
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"%lx-%*lx %s %*s %s %*d\00"
define i64 @freespaceaddr(i32 %0) {
%2 = alloca [30 x i8]
%3 = alloca [850 x i8]
%4 = alloca i64
%5 = alloca [20 x i8]
%6 = alloca [5 x i8]
%7 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [850 x i8], [850 x i8]* %3, i64 0, i64 0
%9 = bitcast i64* %4 to i8*
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%12 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%13 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %1, %20
%18 = call i32* @fgets(i8* %8, i32 850, i32* %13)
%19 = icmp eq i32* %18, null
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64* %4, i8* %11, i8* %10)
%22 = call i8* @strchr(i8* %11, i32 120)
%23 = icmp eq i8* %22, null
br i1 %23, label %17, label %24
24: ; preds = %20, %17
%25 = call i32 @fclose(i32* %13)
%26 = load i64, i64* %4
ret i64 %26
}