#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 *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ *stdout;
int strlen(char *);
int getOutStream(char *outputPath, FILE **outStream) {
  if (outputPath != NULL) {
    for (int i = strlen(outputPath) - 1; i >= 0; i--) {
      if (outputPath[i] == '/') {
        char directoryPath[i];
        memcpy(directoryPath, outputPath, i);
        directoryPath[i] = '\0';
        mkdir(directoryPath, 0700);
        break;
      }
    }
    *outStream = fopen(outputPath, "w");
    if (outStream < 0) {
      printf("Failed to open output file %s\n", outputPath);
      return -1;
    }
  } else {
    *outStream = stdout;
  }
  return 0;
}


x86 assembly (clang, -O0):
<getOutStream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1246 <getOutStream+0xf6>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1200 <getOutStream+0xb0>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11ed <getOutStream+0x9d>
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rdi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
jmp    1200 <getOutStream+0xb0>
jmp    11f2 <getOutStream+0xa2>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    117a <getOutStream+0x2a>
mov    -0x10(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x18(%rbp)
jae    1241 <getOutStream+0xf1>
mov    -0x10(%rbp),%rsi
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <getOutStream+0x10e>
jmp    1257 <getOutStream+0x107>
mov    0x2d93(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getOutStream>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     11bb <getOutStream+0x7b>
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%eax
test   %eax,%eax
jle    11aa <getOutStream+0x6a>
lea    -0x1(%rax),%rdx
cmpb   $0x2f,-0x1(%rbx,%rax,1)
mov    %rdx,%rax
jne    1160 <getOutStream+0x20>
mov    %rsp,%r12
mov    %edx,%r13d
mov    %rsp,%r15
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <memcpy@plt>
movb   $0x0,0x0(%r13,%r15,1)
mov    %r15,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    %r12,%rsp
lea    0xe4f(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
jmp    11c5 <getOutStream+0x85>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [31 x i8] c"Failed to open output file %s\0A\00"
@stdout = global i32* null
define i32 @getOutStream(i8* %0, i32** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i32** %1, i32*** %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %53
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %6
br label %15
15: ; preds = %40, %11
%16 = load i32, i32* %6
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 47
br i1 %25, label %26, label %39
26: ; preds = %18
%27 = load i32, i32* %6
%28 = zext i32 %27 to i64
%29 = call i8* @llvm.stacksave()
store i8* %29, i8** %7
%30 = alloca i8, i64 %28
store i64 %28, i64* %8
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = call i32 @memcpy(i8* %30, i8* %31, i32 %32)
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %30, i64 %35
store i8 0, i8* %36
%37 = call i32 @mkdir(i8* %30, i32 448)
%38 = load i8*, i8** %7
br label %43
39: ; preds = %18
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, -1
store i32 %42, i32* %6
br label %15
43: ; preds = %26, %15
%44 = load i8*, i8** %4
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%46 = load i32**, i32*** %5
store i32* %45, i32** %46
%47 = load i32**, i32*** %5
%48 = icmp ult i32** %47, null
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load i8*, i8** %4
%51 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %50)
store i32 -1, i32* %3
br label %57
52: ; preds = %43
br label %56
53: ; preds = %2
%54 = load i32*, i32** @stdout
%55 = load i32**, i32*** %5
store i32* %54, i32** %55
br label %56
56: ; preds = %53, %52
store i32 0, i32* %3
br label %57
57: ; preds = %56, %49
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stdout = global i32* null
define i32 @getOutStream(i8* %0, i32** nocapture writeonly %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %26, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %0)
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %12, %4
%8 = phi i64 [ %9, %12 ], [ %6, %4 ]
%9 = add nsw i64 %8, -1
%10 = trunc i64 %8 to i32
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %0, i64 %9
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
br i1 %15, label %16, label %7
16: ; preds = %12
%17 = trunc i64 %9 to i32
%18 = and i64 %9, 4294967295
%19 = tail call i8* @llvm.stacksave()
%20 = alloca i8, i64 %18
%21 = call i32 @memcpy(i8* %20, i8* %0, i32 %17)
%22 = getelementptr inbounds i8, i8* %20, i64 %18
store i8 0, i8* %22
%23 = call i32 @mkdir(i8* %20, i32 448)
br label %24
24: ; preds = %7, %16
%25 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %28
26: ; preds = %2
%27 = load i32*, i32** @stdout
br label %28
28: ; preds = %24, %26
%29 = phi i32* [ %27, %26 ], [ %25, %24 ]
store i32* %29, i32** %1
ret i32 0
}