#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 S_IROTH;
int S_IRWXG;
int S_IRWXU;
int S_IXOTH;
char *dirname(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int mkdirp(char *, int);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strdup(char const *);
int /*<<< orphan*/ t_error(char *);
FILE *t_fopen(const char *name, const char *mode) {
  FILE *fp = NULL;
  char *basedir = dirname(strdup(name));
  int ret = 0;
  ret = mkdirp(basedir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  if (-1 == ret) {
    fprintf(stderr, "ERROR: Unable to create parent directory `%s`. Exiting.\n",
            basedir);
    exit(1);
  }
  if ((fp = fopen(name, mode)) == NULL) {
    printf("%s\n", name);
    t_error(" The specified file could not be opened ");
    return NULL;
  } else {
    return fp;
  }
}


x86 assembly (clang, -O0):
<t_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %eax,%edi
call   1040 <dirname@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
call   1050 <mkdirp@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    1214 <t_fopen+0x94>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1258 <t_fopen+0xd8>
mov    -0x10(%rbp),%rsi
lea    0xdff(%rip),%rdi
call   1030 <printf@plt>
lea    0xdf7(%rip),%rdi
call   1080 <t_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1260 <t_fopen+0xe0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<t_fopen>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   10a0 <strdup@plt>
mov    %eax,%edi
call   1040 <dirname@plt>
mov    0x2e47(%rip),%rcx
mov    0x2e30(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e0d(%rip),%rcx
or     (%rcx),%esi
mov    %rax,%r15
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
call   1050 <mkdirp@plt>
cmp    $0xffffffff,%eax
je     120b <t_fopen+0x8b>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11fc <t_fopen+0x7c>
lea    0xe51(%rip),%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
lea    0xe46(%rip),%rdi
call   1080 <t_error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@stderr = global i32 0
@.str = [57 x i8] c"ERROR: Unable to create parent directory `%s`. Exiting.\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [41 x i8] c" The specified file could not be opened \00"
define i32* @t_fopen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %6
%9 = load i8*, i8** %4
%10 = call i32 @strdup(i8* %9)
%11 = call i8* @dirname(i32 %10)
store i8* %11, i8** %7
store i32 0, i32* %8
%12 = load i8*, i8** %7
%13 = load i32, i32* @S_IRWXU
%14 = load i32, i32* @S_IRWXG
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IXOTH
%19 = or i32 %17, %18
%20 = call i32 @mkdirp(i8* %12, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 -1, %21
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %7
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load i8*, i8** %4
%30 = load i8*, i8** %5
%31 = call i32* @fopen(i8* %29, i8* %30)
store i32* %31, i32** %6
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i8*, i8** %4
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = call i32 @t_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32* null, i32** %3
br label %39
37: ; preds = %28
%38 = load i32*, i32** %6
store i32* %38, i32** %3
br label %39
39: ; preds = %37, %33
%40 = load i32*, i32** %3
ret i32* %40
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@stderr = global i32 0
@.str = [57 x i8] c"ERROR: Unable to create parent directory `%s`. Exiting.\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [41 x i8] c" The specified file could not be opened \00"
define noalias i32* @t_fopen(i8* %0, i8* nocapture %1) {
%3 = tail call i32 @strdup(i8* %0)
%4 = tail call i8* @dirname(i32 %3)
%5 = load i32, i32* @S_IRWXU
%6 = load i32, i32* @S_IRWXG
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IROTH
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IXOTH
%11 = or i32 %9, %10
%12 = tail call i32 @mkdirp(i8* %4, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %4)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = tail call i32* @fopen(i8* %0, i8* %1)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%23 = tail call i32 @t_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %18, %21
ret i32* %19
}