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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                         char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
FILE *perf_map_open(pid_t pid) {
  char filename[500];
  snprintf(filename, sizeof(filename), "/tmp/perf-%d.map", pid);
  FILE *res = fopen(filename, "w");
  if (!res)
    error(0, errno, "Couldn't open %s.", filename);
  return res;
}


x86 assembly (clang, -O0):
<perf_map_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x200(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x1f4,%esi
lea    0xeac(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x200(%rbp),%rdi
lea    0xeaa(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x208(%rbp)
cmpq   $0x0,-0x208(%rbp)
jne    119f <perf_map_open+0x6f>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    -0x200(%rbp),%rcx
xor    %edi,%edi
lea    0xe79(%rip),%rdx
call   1040 <error@plt>
mov    -0x208(%rbp),%rax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perf_map_open>:
push   %rbx
sub    $0x200,%rsp
mov    %edi,%ecx
lea    0xebf(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1f4,%esi
call   1030 <snprintf@plt>
lea    0xeb9(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1182 <perf_map_open+0x52>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdx
mov    %rsp,%rcx
xor    %edi,%edi
call   1040 <error@plt>
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/tmp/perf-%d.map\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"Couldn't open %s.\00"
define i32* @perf_map_open(i32 %0) {
%2 = alloca i32
%3 = alloca [500 x i8]
%4 = alloca i32*
store i32 %0, i32* %2
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* %2
%7 = call i32 @snprintf(i8* %5, i32 500, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%15 = call i32 @error(i32 0, i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %12, %1
%17 = load i32*, i32** %4
ret i32* %17
}

LLVM IR (-O2):
@.str = [17 x i8] c"/tmp/perf-%d.map\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"Couldn't open %s.\00"
define noalias i32* @perf_map_open(i32 %0) {
%2 = alloca [500 x i8]
%3 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 500, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = call i32 @error(i32 0, i32 %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %10
10: ; preds = %7, %1
ret i32* %5
}