#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 */
struct stat {
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ mkdir(char const *, int);
int stat(char const *, struct stat *);
char *strdup(char const *);
char *strrchr(char *, char);
int create_folder(const char *folder) {
  struct stat st = {0};
  char *buff = strdup(folder);
  char *name = strrchr(buff, '/');
  if (name) {
    *name = '\0';
    name++;
    create_folder(buff);
  } else
    name = buff;
  if (stat(folder, &st) == -1) {
    mkdir(folder, 0777);
    if (stat(folder, &st) == -1) {
      free(buff);
      return 0;
    }
  }
  free(buff);
  return 1;
}


x86 assembly (clang, -O0):
<create_folder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d7 <create_folder+0x67>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <create_folder@plt>
jmp    11df <create_folder+0x6f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1233 <create_folder+0xc3>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    122e <create_folder+0xbe>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1243 <create_folder+0xd3>
jmp    1233 <create_folder+0xc3>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_folder>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movl   $0x0,(%rsp)
call   1080 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1193 <create_folder+0x33>
movb   $0x0,(%rax)
mov    %r14,%rdi
call   1030 <create_folder@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11c0 <create_folder+0x60>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     11d7 <create_folder+0x77>
mov    $0x1,%ebx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
jmp    11c5 <create_folder+0x65>

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @create_folder(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = bitcast %struct.stat* %4 to i8*
%8 = load i8*, i8** %3
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i8* @strrchr(i8* %10, i8 signext 47)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = load i8*, i8** %6
store i8 0, i8* %15
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %6
%18 = load i8*, i8** %5
%19 = call i32 @create_folder(i8* %18)
br label %22
20: ; preds = %1
%21 = load i8*, i8** %5
store i8* %21, i8** %6
br label %22
22: ; preds = %20, %14
%23 = load i8*, i8** %3
%24 = call i32 @stat(i8* %23, %struct.stat* %4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i8*, i8** %3
%28 = call i32 @mkdir(i8* %27, i32 511)
%29 = load i8*, i8** %3
%30 = call i32 @stat(i8* %29, %struct.stat* %4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i8*, i8** %5
%34 = call i32 @free(i8* %33)
store i32 0, i32* %2
br label %39
35: ; preds = %26
br label %36
36: ; preds = %35, %22
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
store i32 1, i32* %2
br label %39
39: ; preds = %36, %32
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @create_folder(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
store i32 0, i32* %4
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %5, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %1
store i8 0, i8* %6
%9 = tail call i32 @create_folder(i8* %5)
br label %10
10: ; preds = %1, %8
%11 = call i32 @stat(i8* %0, %struct.stat* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @mkdir(i8* %0, i32 511)
%15 = call i32 @stat(i8* %0, %struct.stat* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %18, label %17
17: ; preds = %13, %10
br label %18
18: ; preds = %13, %17
%19 = phi i32 [ 1, %17 ], [ 0, %13 ]
%20 = tail call i32 @free(i8* %5)
ret i32 %19
}