#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*/ st_mode;
};
typedef int /*<<< orphan*/ path;

/* Variables and functions */
scalar_t__ EEXIST;
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, char const *, char *);
scalar_t__ stat(char *, struct stat *);
int create_avatar_diretory(const char *base_dir) {
  char path[PATH_MAX];
  int n = snprintf(path, sizeof(path), "%s/%s", base_dir, "avatars");
  path[sizeof(path) - 1] = '\0';
  if (n >= sizeof(path))
    return -1;
  if (mkdir(path, 0755) == 0) {
    return 0;
  } else if (errno == EEXIST) {
    struct stat st;
    if (stat(path, &st) != 0) {
      perror("stat()ing avatar directory");
      return -1;
    }
    if (S_ISDIR(st.st_mode))
      return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<create_avatar_diretory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%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,-0x20(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe67(%rip),%rdx
lea    0xe66(%rip),%r8
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%rcx
mov    %eax,%edx
mov    -0x38(%rbp),%rax
mov    %edx,-0x24(%rbp)
mov    %rcx,%rdx
sub    $0x1,%rdx
movb   $0x0,(%rax,%rdx,1)
movslq -0x24(%rbp),%rax
cmp    %rcx,%rax
jb     11dd <create_avatar_diretory+0x8d>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1295 <create_avatar_diretory+0x145>
mov    -0x38(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jne    1208 <create_avatar_diretory+0xb8>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1295 <create_avatar_diretory+0x145>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    1282 <create_avatar_diretory+0x132>
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1258 <create_avatar_diretory+0x108>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1295 <create_avatar_diretory+0x145>
mov    -0x30(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     127d <create_avatar_diretory+0x12d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1295 <create_avatar_diretory+0x145>
jmp    1282 <create_avatar_diretory+0x132>
jmp    1287 <create_avatar_diretory+0x137>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_avatar_diretory>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rcx
mov    0x2e8c(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe81(%rip),%rdx
lea    0xe80(%rip),%r8
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <snprintf@plt>
movb   $0x0,-0x1(%r15,%rbx,1)
cltq
mov    $0xffffffff,%r14d
cmp    %r15,%rax
jae    11f3 <create_avatar_diretory+0xa3>
mov    %rbx,%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11f3 <create_avatar_diretory+0xa3>
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <create_avatar_diretory+0x9d>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    1201 <create_avatar_diretory+0xb1>
mov    -0x20(%rbp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
jne    11f3 <create_avatar_diretory+0xa3>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
jmp    11ed <create_avatar_diretory+0x9d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [8 x i8] c"avatars\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.2 = [27 x i8] c"stat()ing avatar directory\00"
define i32 @create_avatar_diretory(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = trunc i64 %10 to i32
%14 = load i8*, i8** %3
%15 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %15, i32* %6
%16 = sub i64 %10, 1
%17 = getelementptr inbounds i8, i8* %12, i64 %16
store i8 0, i8* %17
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = icmp uge i64 %19, %10
br i1 %20, label %21, label %22
21: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %7
br label %44
22: ; preds = %1
%23 = call i64 @mkdir(i8* %12, i32 493)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 0, i32* %2
store i32 1, i32* %7
br label %44
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @EEXIST
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %42
30: ; preds = %26
%31 = call i64 @stat(i8* %12, %struct.stat* %8)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %7
br label %44
35: ; preds = %30
%36 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @S_ISDIR(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 0, i32* %2
store i32 1, i32* %7
br label %44
41: ; preds = %35
br label %42
42: ; preds = %41, %26
br label %43
43: ; preds = %42
store i32 -1, i32* %2
store i32 1, i32* %7
br label %44
44: ; preds = %43, %40, %33, %25, %21
%45 = load i8*, i8** %4
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [8 x i8] c"avatars\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.2 = [27 x i8] c"stat()ing avatar directory\00"
define i32 @create_avatar_diretory(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%7 = add nsw i64 %4, -1
%8 = getelementptr inbounds i8, i8* %5, i64 %7
store i8 0, i8* %8
%9 = sext i32 %6 to i64
%10 = icmp ult i64 %9, %4
br i1 %10, label %11, label %30
11: ; preds = %1
%12 = call i64 @mkdir(i8* %5, i32 493)
%13 = icmp eq i64 %12, 0
br i1 %13, label %30, label %14
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EEXIST
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = bitcast %struct.stat* %2 to i8*
%20 = call i64 @stat(i8* %5, %struct.stat* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %30
24: ; preds = %18
%25 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @S_ISDIR(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %14, %24
br label %30
30: ; preds = %22, %11, %1, %24, %29
%31 = phi i32 [ -1, %29 ], [ 0, %24 ], [ -1, %1 ], [ 0, %11 ], [ -1, %22 ]
ret i32 %31
}