#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*/ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
FILE *open_output(char *filename) {
  unsigned int temp;
  FILE *file;
  if (!(file = fopen(filename, "wb"))) {
    for (temp = 0; filename[temp]; temp++) {
      if (filename[temp] == '/') {
        filename[temp] = 0;
        mkdir(filename, 511);
        filename[temp] = '/';
      }
    }
    if (!(file = fopen(filename, "wb"))) {
      perror("FOpen");
    }
  }
  return (file);
}


x86 assembly (clang, -O0):
<open_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ec <open_output+0xbc>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
cmpb   $0x0,(%rax,%rcx,1)
je     11bd <open_output+0x8d>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11aa <open_output+0x7a>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x2f,(%rax,%rcx,1)
jmp    11af <open_output+0x7f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1161 <open_output+0x31>
mov    -0x8(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e7 <open_output+0xb7>
lea    0xe21(%rip),%rdi
call   1050 <perror@plt>
jmp    11ec <open_output+0xbc>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_output>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xec2(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
jne    118d <open_output+0x5d>
xor    %ebx,%ebx
jmp    116a <open_output+0x3a>
nopl   0x0(%rax)
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
movb   $0x2f,(%r14,%rbx,1)
add    $0x1,%ebx
mov    %ebx,%ebx
movzbl (%r14,%rbx,1),%eax
cmp    $0x2f,%al
je     1150 <open_output+0x20>
test   %al,%al
jne    1167 <open_output+0x37>
lea    0xe80(%rip),%rsi
mov    %r14,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     1195 <open_output+0x65>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    118d <open_output+0x5d>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [6 x i8] c"FOpen\00"
define i32* @open_output(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %46, label %8
8: ; preds = %1
store i32 0, i32* %3
br label %9
9: ; preds = %36, %8
%10 = load i8*, i8** %2
%11 = load i32, i32* %3
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %39
16: ; preds = %9
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 47
br i1 %23, label %24, label %35
24: ; preds = %16
%25 = load i8*, i8** %2
%26 = load i32, i32* %3
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %2
%30 = call i32 @mkdir(i8* %29, i32 511)
%31 = load i8*, i8** %2
%32 = load i32, i32* %3
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 47, i8* %34
br label %35
35: ; preds = %24, %16
br label %36
36: ; preds = %35
%37 = load i32, i32* %3
%38 = add i32 %37, 1
store i32 %38, i32* %3
br label %9
39: ; preds = %9
%40 = load i8*, i8** %2
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %41, i32** %4
%42 = icmp ne i32* %41, null
br i1 %42, label %45, label %43
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
br label %46
46: ; preds = %45, %1
%47 = load i32*, i32** %4
ret i32* %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [6 x i8] c"FOpen\00"
define noalias i32* @open_output(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %18
4: ; preds = %1, %11
%5 = phi i32 [ %12, %11 ], [ 0, %1 ]
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
switch i8 %8, label %11 [
i8 0, label %13
i8 47, label %9
]
9: ; preds = %4
store i8 0, i8* %7
%10 = tail call i32 @mkdir(i8* %0, i32 511)
store i8 47, i8* %7
br label %11
11: ; preds = %4, %9
%12 = add i32 %5, 1
br label %4
13: ; preds = %4
%14 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %13, %16, %1
%19 = phi i32* [ %2, %1 ], [ %14, %13 ], [ null, %16 ]
ret i32* %19
}