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

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, long);
FILE *proc_maps_open(pid_t pid) {
  if (pid) {
    char filename[32];
    sprintf(filename, "/proc/%ld/maps", (long)pid);
    return fopen(filename, "r");
  }
  return fopen("/proc/self/maps", "r");
}


x86 assembly (clang, -O0):
<proc_maps_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1164 <proc_maps_open+0x44>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xeba(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1030 <fopen@plt>
mov    %rax,-0x8(%rbp)
jmp    117b <proc_maps_open+0x5b>
lea    0xea6(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1030 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_maps_open>:
test   %rdi,%rdi
je     1154 <proc_maps_open+0x34>
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rdx
lea    0xecc(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <sprintf@plt>
lea    0xec9(%rip),%rsi
mov    %rbx,%rdi
call   1030 <fopen@plt>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xeb6(%rip),%rdi
lea    0xead(%rip),%rsi
jmp    1030 <fopen@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%ld/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"/proc/self/maps\00"
define i32* @proc_maps_open(i64 %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca [32 x i8]
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%9 = load i64, i64* %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %9)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %2
br label %15
13: ; preds = %1
%14 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %2
br label %15
15: ; preds = %13, %7
%16 = load i32*, i32** %2
ret i32* %16
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%ld/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"/proc/self/maps\00"
define noalias i32* @proc_maps_open(i64 %0) {
%2 = alloca [32 x i8]
%3 = icmp eq i64 %0, 0
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %0)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %10
8: ; preds = %1
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %4
%11 = phi i32* [ %7, %4 ], [ %9, %8 ]
ret i32* %11
}