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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int print_map_listing(int pid) {
  FILE *fmaps;
  char path[256] = {0};
  char chunk[256] = {0};
  size_t read;
  snprintf(path, sizeof(path), "/proc/%d/maps", pid);
  if ((fmaps = fopen(path, "r")) == 0) {
    fprintf(stderr, "Could not open maps file: %s", path);
    return EXIT_FAILURE;
  }
  while ((read = fread(chunk, 1, sizeof(chunk), fmaps)) > 0) {
    fwrite(chunk, 1, read, stdout);
  }
  fclose(fmaps);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<print_map_listing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe46(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    120d <print_map_listing+0x9d>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xe19(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1278 <print_map_listing+0x108>
jmp    1212 <print_map_listing+0xa2>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1030 <fread@plt>
mov    %rax,-0x218(%rbp)
cmp    $0x0,%rax
jbe    1263 <print_map_listing+0xf3>
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rdx
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1090 <fwrite@plt>
jmp    1212 <print_map_listing+0xa2>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_map_listing>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %edi,%ecx
xorps  %xmm0,%xmm0
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xda1(%rip),%rdx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
lea    0xd93(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     12ee <print_map_listing+0x18e>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     12dd <print_map_listing+0x17d>
mov    0x2d24(%rip),%r15
mov    %rsp,%rbx
nop
mov    (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    12b0 <print_map_listing+0x150>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2cdc(%rip),%rax
jmp    1312 <print_map_listing+0x1b2>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd12(%rip),%rsi
lea    0x100(%rsp),%rdx
call   1060 <fprintf@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Could not open maps file: %s\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @print_map_listing(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = alloca i64
store i32 %0, i32* %3
%8 = bitcast [256 x i8]* %5 to i8*
%9 = bitcast [256 x i8]* %6 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
store i32 %20, i32* %2
br label %36
21: ; preds = %1
br label %22
22: ; preds = %27, %21
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%24 = load i32*, i32** %4
%25 = call i64 @fread(i8* %23, i32 1, i32 256, i32* %24)
store i64 %25, i64* %7
%26 = icmp ugt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%29 = load i64, i64* %7
%30 = load i32, i32* @stdout
%31 = call i32 @fwrite(i8* %28, i32 1, i64 %29, i32 %30)
br label %22
32: ; preds = %22
%33 = load i32*, i32** %4
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* @EXIT_SUCCESS
store i32 %35, i32* %2
br label %36
36: ; preds = %32, %16
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Could not open maps file: %s\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @print_map_listing(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i64 @fread(i8* %5, i32 1, i32 256, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %21, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %23
15: ; preds = %9, %15
%16 = phi i64 [ %19, %15 ], [ %10, %9 ]
%17 = load i32, i32* @stdout
%18 = call i32 @fwrite(i8* %5, i32 1, i64 %16, i32 %17)
%19 = call i64 @fread(i8* %5, i32 1, i32 256, i32* %7)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %15
21: ; preds = %15, %9
%22 = call i32 @fclose(i32* %7)
br label %23
23: ; preds = %21, %12
%24 = phi i32* [ @EXIT_FAILURE, %12 ], [ @EXIT_SUCCESS, %21 ]
%25 = load i32, i32* %24
ret i32 %25
}