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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int sscanf(char *, char *, int /*<<< orphan*/ *, char *, char *);
void write_maps(char *fname) {
  char buf[512], path[128];
  char exec;
  uintmax_t addr;
  FILE *maps = fopen("/proc/self/maps", "r");
  FILE *out = fopen(fname, "w");
  if (!maps || !out)
    exit(EXIT_FAILURE);
  while (fgets(buf, sizeof(buf), maps)) {
    if (sscanf(buf, "%jx-%*x %*c%*c%c%*c %*x %*s %*d /%126[^\n]", &addr, &exec,
               path + 1) != 3)
      continue;
    if (exec != 'x')
      continue;
    path[0] = '/';
    fprintf(out, "0x%jx:%s ", addr, path);
  }
  fprintf(out, "\n");
  fclose(out);
  fclose(maps);
}


x86 assembly (clang, -O0):
<write_maps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2b0,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x2a0(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x2a8(%rbp)
cmpq   $0x0,-0x2a0(%rbp)
je     11bc <write_maps+0x5c>
cmpq   $0x0,-0x2a8(%rbp)
jne    11ca <write_maps+0x6a>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11cf <write_maps+0x6f>
lea    -0x210(%rbp),%rdi
mov    -0x2a0(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     126e <write_maps+0x10e>
lea    -0x210(%rbp),%rdi
lea    -0x290(%rbp),%r8
add    $0x1,%r8
lea    0xe0a(%rip),%rsi
lea    -0x298(%rbp),%rdx
lea    -0x291(%rbp),%rcx
call   1060 <sscanf@plt>
cmp    $0x3,%eax
je     122b <write_maps+0xcb>
jmp    11cf <write_maps+0x6f>
movsbl -0x291(%rbp),%eax
cmp    $0x78,%eax
je     1240 <write_maps+0xe0>
jmp    11cf <write_maps+0x6f>
movb   $0x2f,-0x290(%rbp)
mov    -0x2a8(%rbp),%rdi
mov    -0x298(%rbp),%edx
lea    -0x290(%rbp),%rcx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11cf <write_maps+0x6f>
mov    -0x2a8(%rbp),%rdi
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x2a8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2a0(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x2b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_maps>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x298,%rsp
mov    %rdi,%rbx
lea    0xe75(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r13
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %r13,%r13
je     128c <write_maps+0x11c>
test   %rax,%rax
je     128c <write_maps+0x11c>
mov    %rax,0x8(%rsp)
lea    0x90(%rsp),%rdi
mov    $0x200,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1258 <write_maps+0xe8>
lea    0x11(%rsp),%r15
lea    0xe2a(%rip),%r12
lea    0x90(%rsp),%rbx
lea    0x4(%rsp),%rbp
lea    0x3(%rsp),%r14
jmp    1215 <write_maps+0xa5>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1258 <write_maps+0xe8>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1070 <sscanf@plt>
cmp    $0x3,%eax
jne    1200 <write_maps+0x90>
cmpb   $0x78,0x3(%rsp)
jne    1200 <write_maps+0x90>
movb   $0x2f,0x10(%rsp)
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%rdi
lea    0xdf4(%rip),%rsi
lea    0x10(%rsp),%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1200 <write_maps+0x90>
mov    $0xa,%edi
mov    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r13,%rdi
call   1030 <fclose@plt>
add    $0x298,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [42 x i8] c"%jx-%*x %*c%*c%c%*c %*x %*s %*d /%126[^\0A]\00"
@.str.4 = [10 x i8] c"0x%jx:%s \00"
@.str.5 = [2 x i8] c"\0A\00"
define void @write_maps(i8* %0) {
%2 = alloca i8*
%3 = alloca [512 x i8]
%4 = alloca [128 x i8]
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
store i8* %0, i8** %2
%9 = 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* %9, i32** %7
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32*, i32** %8
%16 = icmp ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %14, %1
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %14
br label %21
21: ; preds = %38, %37, %32, %20
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%23 = load i32*, i32** %7
%24 = call i64 @fgets(i8* %22, i32 512, i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %44
26: ; preds = %21
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32* %6, i8* %5, i8* %29)
%31 = icmp ne i32 %30, 3
br i1 %31, label %32, label %33
32: ; preds = %26
br label %21
33: ; preds = %26
%34 = load i8, i8* %5
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 120
br i1 %36, label %37, label %38
37: ; preds = %33
br label %21
38: ; preds = %33
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
store i8 47, i8* %39
%40 = load i32*, i32** %8
%41 = load i32, i32* %6
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %41, i8* %42)
br label %21
44: ; preds = %21
%45 = load i32*, i32** %8
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32*, i32** %8
%48 = call i32 @fclose(i32* %47)
%49 = load i32*, i32** %7
%50 = call i32 @fclose(i32* %49)
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [42 x i8] c"%jx-%*x %*c%*c%c%*c %*x %*s %*d /%126[^\0A]\00"
@.str.4 = [10 x i8] c"0x%jx:%s \00"
define void @write_maps(i8* nocapture %0) {
%2 = alloca [512 x i8]
%3 = alloca [128 x i8]
%4 = alloca i8
%5 = alloca i32
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = 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))
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp ne i32* %9, null
%12 = icmp ne i32* %10, null
%13 = and i1 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = call i64 @fgets(i8* %6, i32 512, i32* %9)
%16 = icmp eq i64 %15, 0
br i1 %16, label %34, label %17
17: ; preds = %14
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 1
br label %22
19: ; preds = %1
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %17, %31
%23 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32* %5, i8* %4, i8* %18)
%24 = icmp ne i32 %23, 3
%25 = load i8, i8* %4
%26 = icmp ne i8 %25, 120
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %31, label %28
28: ; preds = %22
store i8 47, i8* %7
%29 = load i32, i32* %5
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %29, i8* %7)
br label %31
31: ; preds = %28, %22
%32 = call i64 @fgets(i8* %6, i32 512, i32* %9)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %22
34: ; preds = %31, %14
%35 = call i32 @fputc(i32 10, i32* %10)
%36 = call i32 @fclose(i32* %10)
%37 = call i32 @fclose(i32* %9)
ret void
}