#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 */

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int mkdir(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
char *strrchr(char *, char);
int cfg_mkdir(char *filename) {
  char *h;
  int rc;
  h = strrchr(filename, '/');
  if (!h || h == filename)
    return -1;
  *h = '\0';
  rc = mkdir(filename, 0777);
  if (-1 == rc && ENOENT == errno) {
    cfg_mkdir(filename);
    rc = mkdir(filename, 0777);
  }
  if (-1 == rc)
    fprintf(stderr, "mkdir(%s): %s\n", filename, strerror(errno));
  *h = '/';
  return rc;
}


x86 assembly (clang, -O0):
<cfg_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1187 <cfg_mkdir+0x37>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1193 <cfg_mkdir+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <cfg_mkdir+0xf1>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11ed <cfg_mkdir+0x9d>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <cfg_mkdir+0x9d>
mov    -0x10(%rbp),%rdi
call   1040 <cfg_mkdir@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    1234 <cfg_mkdir+0xe4>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd1(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cfg_mkdir>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     11e7 <cfg_mkdir+0x97>
cmp    %r14,%rbx
je     11e7 <cfg_mkdir+0x97>
movb   $0x0,(%rbx)
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    11e4 <cfg_mkdir+0x94>
mov    0x2e39(%rip),%rax
mov    0x2e42(%rip),%rbp
mov    0x0(%rbp),%rdi
cmp    %rdi,(%rax)
jne    11bd <cfg_mkdir+0x6d>
mov    %r14,%rdi
call   1040 <cfg_mkdir@plt>
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    11e4 <cfg_mkdir+0x94>
mov    0x0(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebp
call   1070 <strerror@plt>
lea    0xe2e(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
movb   $0x2f,(%rbx)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@ENOENT = global i64 0
@errno = global i64 0
@stderr = global i32 0
@.str = [15 x i8] c"mkdir(%s): %s\0A\00"
define i32 @cfg_mkdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @strrchr(i8* %6, i8 signext 47)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10, %1
store i32 -1, i32* %2
br label %42
15: ; preds = %10
%16 = load i8*, i8** %4
store i8 0, i8* %16
%17 = load i8*, i8** %3
%18 = call i32 @mkdir(i8* %17, i32 511)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %30
21: ; preds = %15
%22 = load i64, i64* @ENOENT
%23 = load i64, i64* @errno
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = call i32 @cfg_mkdir(i8* %26)
%28 = load i8*, i8** %3
%29 = call i32 @mkdir(i8* %28, i32 511)
store i32 %29, i32* %5
br label %30
30: ; preds = %25, %21, %15
%31 = load i32, i32* %5
%32 = icmp eq i32 -1, %31
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %3
%36 = load i64, i64* @errno
%37 = call i8* @strerror(i64 %36)
%38 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %37)
br label %39
39: ; preds = %33, %30
%40 = load i8*, i8** %4
store i8 47, i8* %40
%41 = load i32, i32* %5
store i32 %41, i32* %2
br label %42
42: ; preds = %39, %14
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@ENOENT = global i64 0
@errno = global i64 0
@stderr = global i32 0
@.str = [15 x i8] c"mkdir(%s): %s\0A\00"
define i32 @cfg_mkdir(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
%4 = icmp eq i8* %2, %0
%5 = or i1 %3, %4
br i1 %5, label %26, label %6
6: ; preds = %1
store i8 0, i8* %2
%7 = tail call i32 @mkdir(i8* %0, i32 511)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %24
9: ; preds = %6
%10 = load i64, i64* @ENOENT
%11 = load i64, i64* @errno
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = tail call i32 @cfg_mkdir(i8* %0)
%15 = tail call i32 @mkdir(i8* %0, i32 511)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = load i64, i64* @errno
br label %19
19: ; preds = %17, %9
%20 = phi i64 [ %18, %17 ], [ %11, %9 ]
%21 = load i32, i32* @stderr
%22 = tail call i8* @strerror(i64 %20)
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %22)
br label %24
24: ; preds = %6, %19, %13
%25 = phi i32 [ -1, %19 ], [ %15, %13 ], [ %7, %6 ]
store i8 47, i8* %2
br label %26
26: ; preds = %1, %24
%27 = phi i32 [ %25, %24 ], [ -1, %1 ]
ret i32 %27
}