#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*/ *fopen(char *, char const *);
int /*<<< orphan*/ recursive_mkdir(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
FILE *dir_fopen(const char *dir, const char *path, const char *mode) {
  char outfn[512];
  char *p;
  int len;
  strcpy(outfn, "./");
  strcat(outfn, dir);
  len = strlen(outfn);
  if (outfn[len - 1] != '/' && path[0] != '/')
    strcat(outfn, "/");
  strcat(outfn, path);
  p = strrchr(outfn, '/');
  if (*p) {
    *p = '\0';
    recursive_mkdir(outfn);
    *p = '/';
  }
  return fopen(outfn, mode);
}


x86 assembly (clang, -O0):
<dir_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x22c(%rbp)
mov    -0x22c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
je     11eb <dir_fopen+0x8b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11eb <dir_fopen+0x8b>
lea    -0x220(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x220(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x228(%rbp)
mov    -0x228(%rbp),%rax
cmpb   $0x0,(%rax)
je     1243 <dir_fopen+0xe3>
mov    -0x228(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x220(%rbp),%rdi
call   1040 <recursive_mkdir@plt>
mov    -0x228(%rbp),%rax
movb   $0x2f,(%rax)
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_fopen>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe82(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     11ba <dir_fopen+0x5a>
cmpb   $0x2f,(%r15)
je     11ba <dir_fopen+0x5a>
lea    0xe4f(%rip),%rsi
mov    %rsp,%rdi
call   1080 <strcat@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmpb   $0x0,(%rax)
je     11eb <dir_fopen+0x8b>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %rsp,%rdi
call   1040 <recursive_mkdir@plt>
movb   $0x2f,(%rbx)
mov    %rsp,%rdi
mov    %r14,%rsi
call   1070 <fopen@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c"/\00"
define i32* @dir_fopen(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [512 x i8]
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%13 = load i8*, i8** %4
%14 = call i32 @strcat(i8* %12, i8* %13)
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 47
br i1 %23, label %24, label %33
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 47
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%32 = call i32 @strcat(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %30, %24, %3
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%35 = load i8*, i8** %5
%36 = call i32 @strcat(i8* %34, i8* %35)
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%38 = call i8* @strrchr(i8* %37, i8 signext 47)
store i8* %38, i8** %8
%39 = load i8*, i8** %8
%40 = load i8, i8* %39
%41 = icmp ne i8 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %33
%43 = load i8*, i8** %8
store i8 0, i8* %43
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%45 = call i32 @recursive_mkdir(i8* %44)
%46 = load i8*, i8** %8
store i8 47, i8* %46
br label %47
47: ; preds = %42, %33
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%49 = load i8*, i8** %6
%50 = call i32* @fopen(i8* %48, i8* %49)
ret i32* %50
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c"/\00"
define noalias i32* @dir_fopen(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @strcat(i8* %5, i8* %0)
%8 = call i32 @strlen(i8* %5)
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 47
br i1 %13, label %19, label %14
14: ; preds = %3
%15 = load i8, i8* %1
%16 = icmp eq i8 %15, 47
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %14, %3
%20 = call i32 @strcat(i8* %5, i8* %1)
%21 = call i8* @strrchr(i8* %5, i8 signext 47)
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
store i8 0, i8* %21
%25 = call i32 @recursive_mkdir(i8* %5)
store i8 47, i8* %21
br label %26
26: ; preds = %24, %19
%27 = call i32* @fopen(i8* %5, i8* %2)
ret i32* %27
}