#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 */
scalar_t__ EEXIST;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
char *strchr(char *, char);
FILE *openFile(char *path) {
  char *cur, *prev;
  FILE *ans;
  prev = path;
  while (NULL != (cur = strchr(prev + 1, '/'))) {
    *cur = '\0';
    if (0 > mkdir(&path[0], S_IRWXU)) {
      if (errno != EEXIST) {
        perror("Unable to create directory");
        exit(EXIT_FAILURE);
      }
    }
    *cur = '/';
    prev = cur;
  }
  if (NULL == (ans = fopen(&path[0], "w"))) {
    perror("Unable to open file");
    exit(EXIT_FAILURE);
  }
  return ans;
}


x86 assembly (clang, -O0):
<openFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     11fc <openFile+0xac>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11e8 <openFile+0x98>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
je     11e3 <openFile+0x93>
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11e8 <openFile+0x98>
mov    -0x10(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1164 <openFile+0x14>
mov    -0x8(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1238 <openFile+0xe8>
lea    0xdf8(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
add    $0x1,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11e3 <openFile+0x93>
mov    %rax,%rbx
mov    0x2e6f(%rip),%r15
mov    0x2e50(%rip),%r12
mov    0x2e51(%rip),%r13
jmp    11ac <openFile+0x5c>
nopl   0x0(%rax)
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e3 <openFile+0x93>
movb   $0x0,(%rbx)
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <mkdir@plt>
test   %rax,%rax
jns    1190 <openFile+0x40>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1190 <openFile+0x40>
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe31(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1201 <openFile+0xb1>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe15(%rip),%rdi
jmp    11d0 <openFile+0x80>

LLVM IR (-O0):
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str = [27 x i8] c"Unable to create directory\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [20 x i8] c"Unable to open file\00"
define i32* @openFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %28, %1
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = call i8* @strchr(i8* %9, i8 signext 47)
store i8* %10, i8** %3
%11 = icmp ne i8* null, %10
br i1 %11, label %12, label %31
12: ; preds = %7
%13 = load i8*, i8** %3
store i8 0, i8* %13
%14 = load i8*, i8** %2
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i32, i32* @S_IRWXU
%17 = call i64 @mkdir(i8* %15, i32 %16)
%18 = icmp sgt i64 0, %17
br i1 %18, label %19, label %28
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EEXIST
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
br label %28
28: ; preds = %27, %12
%29 = load i8*, i8** %3
store i8 47, i8* %29
%30 = load i8*, i8** %3
store i8* %30, i8** %4
br label %7
31: ; preds = %7
%32 = load i8*, i8** %2
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %5
%35 = icmp eq i32* null, %34
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %31
%41 = load i32*, i32** %5
ret i32* %41
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str = [27 x i8] c"Unable to create directory\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [20 x i8] c"Unable to open file\00"
define noalias i32* @openFile(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 1
%3 = tail call i8* @strchr(i8* %2, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %22, label %5
5: ; preds = %1, %18
%6 = phi i8* [ %20, %18 ], [ %3, %1 ]
store i8 0, i8* %6
%7 = load i32, i32* @S_IRWXU
%8 = tail call i64 @mkdir(i8* %0, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EEXIST
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %10, %5
store i8 47, i8* %6
%19 = getelementptr inbounds i8, i8* %6, i64 1
%20 = tail call i8* @strchr(i8* %19, i8 signext 47)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %5
22: ; preds = %18, %1
%23 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %22
ret i32* %23
}