#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *);
char *dirname(char *);
int /*<<< orphan*/ *fopen(char *, char const *);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
FILE *cache_open(const char *path, const char *mode) {
  FILE *cache;
  char *cache_path;
  char *path_copy = strdup(path);
  char *dir = dirname(path_copy);
  asprintf(&cache_path, "%s/cache", dir);
  cache = fopen(cache_path, mode);
  free(cache_path);
  free(path_copy);
  return cache;
}


x86 assembly (clang, -O0):
<cache_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <dirname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1050 <asprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_open>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <dirname@plt>
lea    0xe92(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
call   1050 <asprintf@plt>
mov    (%rsp),%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%s/cache\00"
define i32* @cache_open(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = call i8* @dirname(i8* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %6
%16 = load i8*, i8** %4
%17 = call i32* @fopen(i8* %15, i8* %16)
store i32* %17, i32** %5
%18 = load i8*, i8** %6
%19 = call i32 @free(i8* %18)
%20 = load i8*, i8** %7
%21 = call i32 @free(i8* %20)
%22 = load i32*, i32** %5
ret i32* %22
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s/cache\00"
define noalias i32* @cache_open(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i8* @dirname(i8* %5)
%7 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* %1)
%10 = load i8*, i8** %3
%11 = call i32 @free(i8* %10)
%12 = call i32 @free(i8* %5)
ret i32* %9
}