#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;
  scalar_t__ st_uid;
};

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ S_ISDIR(int);
int /*<<< orphan*/ abortmsg(char *, char const *);
int /*<<< orphan*/ abortmsgerrno(char *, char const *);
scalar_t__ errno;
scalar_t__ geteuid();
int lstat(char const *, struct stat *);
int mkdir(char const *, int);
void preparesockdir(const char *sockdir) {
  int r;
  r = mkdir(sockdir, 0700);
  if (r < 0 && errno != EEXIST)
    abortmsgerrno("cannot create sockdir %s", sockdir);
  struct stat st;
  r = lstat(sockdir, &st);
  if (r < 0)
    abortmsgerrno("cannot stat %s", sockdir);
  if (!S_ISDIR(st.st_mode))
    abortmsg("cannot create sockdir %s (file exists)", sockdir);
  if (st.st_uid != geteuid() || st.st_mode & 0077)
    abortmsg("insecure sockdir %s", sockdir);
}


x86 assembly (clang, -O0):
<preparesockdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b1 <preparesockdir+0x51>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
je     11b1 <preparesockdir+0x51>
mov    -0x8(%rbp),%rsi
lea    0xe54(%rip),%rdi
call   1070 <abortmsgerrno@plt>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <lstat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11db <preparesockdir+0x7b>
mov    -0x8(%rbp),%rsi
lea    0xe43(%rip),%rdi
call   1070 <abortmsgerrno@plt>
mov    -0x20(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11fc <preparesockdir+0x9c>
mov    -0x8(%rbp),%rsi
lea    0xe31(%rip),%rdi
call   1080 <abortmsg@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jne    122a <preparesockdir+0xca>
mov    -0x20(%rbp),%eax
and    $0x3f,%eax
cmp    $0x0,%eax
je     123a <preparesockdir+0xda>
mov    -0x8(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1080 <abortmsg@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<preparesockdir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %eax,%eax
jns    119e <preparesockdir+0x3e>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
je     119e <preparesockdir+0x3e>
lea    0xe6a(%rip),%rdi
mov    %r14,%rsi
call   1070 <abortmsgerrno@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1060 <lstat@plt>
test   %eax,%eax
jns    11bc <preparesockdir+0x5c>
lea    0xe65(%rip),%rdi
mov    %r14,%rsi
call   1070 <abortmsgerrno@plt>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
jne    11d9 <preparesockdir+0x79>
lea    0xe57(%rip),%rdi
mov    %r14,%rsi
call   1080 <abortmsg@plt>
mov    0x8(%rsp),%rbx
xor    %eax,%eax
call   1050 <geteuid@plt>
test   $0x3f,%bpl
jne    11f9 <preparesockdir+0x99>
cmp    %rax,%rbx
jne    11f9 <preparesockdir+0x99>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <abortmsg@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@EEXIST = global i64 0
@.str = [25 x i8] c"cannot create sockdir %s\00"
@.str.1 = [15 x i8] c"cannot stat %s\00"
@.str.2 = [39 x i8] c"cannot create sockdir %s (file exists)\00"
@.str.3 = [20 x i8] c"insecure sockdir %s\00"
define void @preparesockdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @mkdir(i8* %5, i32 448)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EEXIST
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i8*, i8** %2
%15 = call i32 @abortmsgerrno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %9, %1
%17 = load i8*, i8** %2
%18 = call i32 @lstat(i8* %17, %struct.stat* %4)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8*, i8** %2
%23 = call i32 @abortmsgerrno(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %16
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @S_ISDIR(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = load i8*, i8** %2
%31 = call i32 @abortmsg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %24
%33 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%34 = load i64, i64* %33
%35 = call i64 (...) @geteuid()
%36 = icmp ne i64 %34, %35
br i1 %36, label %42, label %37
37: ; preds = %32
%38 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%39 = load i32, i32* %38
%40 = and i32 %39, 63
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37, %32
%43 = load i8*, i8** %2
%44 = call i32 @abortmsg(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %37
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@EEXIST = global i64 0
@.str = [25 x i8] c"cannot create sockdir %s\00"
@.str.1 = [15 x i8] c"cannot stat %s\00"
@.str.2 = [39 x i8] c"cannot create sockdir %s (file exists)\00"
@.str.3 = [20 x i8] c"insecure sockdir %s\00"
define void @preparesockdir(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32 @mkdir(i8* %0, i32 448)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %1
%6 = load i64, i64* @errno
%7 = load i64, i64* @EEXIST
%8 = icmp eq i64 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @abortmsgerrno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %5, %1
%12 = bitcast %struct.stat* %2 to i8*
%13 = call i32 @lstat(i8* %0, %struct.stat* %2)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @abortmsgerrno(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %15, %11
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @S_ISDIR(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @abortmsg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %24
24: ; preds = %22, %17
%25 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%26 = load i64, i64* %25
%27 = tail call i64 (...) @geteuid()
%28 = icmp eq i64 %26, %27
%29 = and i32 %19, 63
%30 = icmp eq i32 %29, 0
%31 = and i1 %30, %28
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = tail call i32 @abortmsg(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %34
34: ; preds = %24, %32
ret void
}