#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 st_mode;
};

/* Variables and functions */
scalar_t__ EEXIST;
int S_IFDIR;
int S_IFMT;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int create_user_config_dir(char *path) {
  int mkdir_err;
  mkdir_err = mkdir(path, 0700);
  struct stat buf;
  if (mkdir_err && (errno != EEXIST || stat(path, &buf) ||
                    !(((buf.st_mode) & S_IFMT) == S_IFDIR))) {
    return -1;
  }
  char *fullpath = malloc(strlen(path) + strlen("/tox/") + 1);
  strcpy(fullpath, path);
  strcat(fullpath, "/tox/");
  mkdir_err = mkdir(fullpath, 0700);
  if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) ||
                    !(((buf.st_mode) & S_IFMT) == S_IFDIR))) {
    free(fullpath);
    return -1;
  }
  free(fullpath);
  return 0;
}


x86 assembly (clang, -O0):
<create_user_config_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ef <create_user_config_dir+0x7f>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <create_user_config_dir+0x73>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11e3 <create_user_config_dir+0x73>
mov    -0x18(%rbp),%eax
mov    0x2e0e(%rip),%rcx
and    (%rcx),%eax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%eax
je     11ef <create_user_config_dir+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <create_user_config_dir+0x155>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x28(%rbp)
lea    0xdfd(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12b5 <create_user_config_dir+0x145>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    12a0 <create_user_config_dir+0x130>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    12a0 <create_user_config_dir+0x130>
mov    -0x18(%rbp),%eax
mov    0x2d51(%rip),%rcx
and    (%rcx),%eax
mov    0x2d28(%rip),%rcx
cmp    (%rcx),%eax
je     12b5 <create_user_config_dir+0x145>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <create_user_config_dir+0x155>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_user_config_dir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
test   %eax,%eax
je     11db <create_user_config_dir+0x6b>
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
jne    126b <create_user_config_dir+0xfb>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    126b <create_user_config_dir+0xfb>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%eax
jne    126b <create_user_config_dir+0xfb>
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x6(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
lea    0xdff(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
test   %eax,%eax
je     1260 <create_user_config_dir+0xf0>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2dad(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
jne    1263 <create_user_config_dir+0xf3>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    1263 <create_user_config_dir+0xf3>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%eax
jne    1263 <create_user_config_dir+0xf3>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str = [6 x i8] c"/tox/\00"
define i32 @create_user_config_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @mkdir(i8* %7, i32 448)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EEXIST
%14 = icmp ne i64 %12, %13
br i1 %14, label %26, label %15
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = call i64 @stat(i8* %16, %struct.stat* %5)
%18 = icmp ne i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IFMT
%23 = and i32 %21, %22
%24 = load i32, i32* @S_IFDIR
%25 = icmp eq i32 %23, %24
br i1 %25, label %27, label %26
26: ; preds = %19, %15, %11
store i32 -1, i32* %2
br label %64
27: ; preds = %19, %1
%28 = load i8*, i8** %3
%29 = call i64 @strlen(i8* %28)
%30 = call i64 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%31 = add nsw i64 %29, %30
%32 = add nsw i64 %31, 1
%33 = call i8* @malloc(i64 %32)
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = load i8*, i8** %3
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = load i8*, i8** %6
%38 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%39 = load i8*, i8** %6
%40 = call i32 @mkdir(i8* %39, i32 448)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %61
43: ; preds = %27
%44 = load i64, i64* @errno
%45 = load i64, i64* @EEXIST
%46 = icmp ne i64 %44, %45
br i1 %46, label %58, label %47
47: ; preds = %43
%48 = load i8*, i8** %6
%49 = call i64 @stat(i8* %48, %struct.stat* %5)
%50 = icmp ne i64 %49, 0
br i1 %50, label %58, label %51
51: ; preds = %47
%52 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* @S_IFMT
%55 = and i32 %53, %54
%56 = load i32, i32* @S_IFDIR
%57 = icmp eq i32 %55, %56
br i1 %57, label %61, label %58
58: ; preds = %51, %47, %43
%59 = load i8*, i8** %6
%60 = call i32 @free(i8* %59)
store i32 -1, i32* %2
br label %64
61: ; preds = %51, %27
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
store i32 0, i32* %2
br label %64
64: ; preds = %61, %58, %26
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str = [6 x i8] c"/tox/\00"
define i32 @create_user_config_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32 @mkdir(i8* %0, i32 448)
%4 = bitcast %struct.stat* %2 to i8*
%5 = icmp eq i32 %3, 0
br i1 %5, label %20, label %6
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EEXIST
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %46
10: ; preds = %6
%11 = call i64 @stat(i8* %0, %struct.stat* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %46
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @S_IFMT
%17 = and i32 %16, %15
%18 = load i32, i32* @S_IFDIR
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %46
20: ; preds = %13, %1
%21 = call i64 @strlen(i8* dereferenceable(1) %0)
%22 = add nsw i64 %21, 6
%23 = call i8* @malloc(i64 %22)
%24 = call i32 @strcpy(i8* %23, i8* %0)
%25 = call i32 @strcat(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @mkdir(i8* %23, i32 448)
%27 = icmp eq i32 %26, 0
br i1 %27, label %42, label %28
28: ; preds = %20
%29 = load i64, i64* @errno
%30 = load i64, i64* @EEXIST
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %43
32: ; preds = %28
%33 = call i64 @stat(i8* %23, %struct.stat* %2)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @S_IFMT
%39 = and i32 %38, %37
%40 = load i32, i32* @S_IFDIR
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %35, %20
br label %43
43: ; preds = %28, %32, %35, %42
%44 = phi i32 [ 0, %42 ], [ -1, %35 ], [ -1, %32 ], [ -1, %28 ]
%45 = call i32 @free(i8* %23)
br label %46
46: ; preds = %43, %6, %10, %13
%47 = phi i32 [ -1, %13 ], [ -1, %10 ], [ -1, %6 ], [ %44, %43 ]
ret i32 %47
}