#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 {
  scalar_t__ st_uid;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int getuid();
int lstat(char const *, struct stat *);
int mkdir(char const *, int);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int check_tmp_dir(const char *tmp_dir) {
  int result;
  struct stat stat_buf;
  result = lstat(tmp_dir, &stat_buf);
  if ((result == -1) && (errno == ENOENT)) {
    result = mkdir(tmp_dir, 0700);
    if (result == -1) {
      fprintf(stderr, "Error: Can not create directory \"%s\".\n", tmp_dir);
      return 1;
    }
    result = stat(tmp_dir, &stat_buf);
  }
  if ((result == -1) || (!S_ISDIR(stat_buf.st_mode))) {
    fprintf(stderr, "Error: \"%s\" is not a directory.\n", tmp_dir);
    return 1;
  }
  if (stat_buf.st_uid != getuid()) {
    fprintf(stderr, "Error: \"%s\" is owned by uid %d instead of uid %d.\n",
            tmp_dir, stat_buf.st_uid, getuid());
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_tmp_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f2 <check_tmp_dir+0x92>
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
jne    11f2 <check_tmp_dir+0x92>
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e2 <check_tmp_dir+0x82>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a7 <check_tmp_dir+0x147>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     120d <check_tmp_dir+0xad>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1234 <check_tmp_dir+0xd4>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a7 <check_tmp_dir+0x147>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
je     12a0 <check_tmp_dir+0x140>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    %eax,%r8d
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a7 <check_tmp_dir+0x147>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_tmp_dir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <lstat@plt>
cmp    $0xffffffff,%eax
je     11c8 <check_tmp_dir+0x68>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1204 <check_tmp_dir+0xa4>
mov    (%rsp),%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
cltq
cmp    %rax,%r14
je     1223 <check_tmp_dir+0xc3>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
lea    0xe94(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    121e <check_tmp_dir+0xbe>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <check_tmp_dir+0xa4>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
je     122e <check_tmp_dir+0xce>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1178 <check_tmp_dir+0x18>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
jmp    1214 <check_tmp_dir+0xb4>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [39 x i8] c"Error: Can not create directory \22%s\22.\0A\00"
@.str.1 = [33 x i8] c"Error: \22%s\22 is not a directory.\0A\00"
@.str.2 = [51 x i8] c"Error: \22%s\22 is owned by uid %d instead of uid %d.\0A\00"
define i32 @check_tmp_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @lstat(i8* %6, %struct.stat* %5)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %26
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = call i32 @mkdir(i8* %15, i32 448)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %3
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %2
br label %52
23: ; preds = %14
%24 = load i8*, i8** %3
%25 = call i32 @stat(i8* %24, %struct.stat* %5)
store i32 %25, i32* %4
br label %26
26: ; preds = %23, %10, %1
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, -1
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @S_ISDIR(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %29, %26
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %3
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %36)
store i32 1, i32* %2
br label %52
38: ; preds = %29
%39 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%40 = load i64, i64* %39
%41 = call i32 (...) @getuid()
%42 = sext i32 %41 to i64
%43 = icmp ne i64 %40, %42
br i1 %43, label %44, label %51
44: ; preds = %38
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %3
%47 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%48 = load i64, i64* %47
%49 = call i32 (...) @getuid()
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %46, i64 %48, i32 %49)
store i32 1, i32* %2
br label %52
51: ; preds = %38
store i32 0, i32* %2
br label %52
52: ; preds = %51, %44, %34, %19
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [39 x i8] c"Error: Can not create directory \22%s\22.\0A\00"
@.str.1 = [33 x i8] c"Error: \22%s\22 is not a directory.\0A\00"
@.str.2 = [51 x i8] c"Error: \22%s\22 is owned by uid %d instead of uid %d.\0A\00"
define i32 @check_tmp_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %19
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %24
10: ; preds = %6
%11 = tail call i32 @mkdir(i8* %0, i32 448)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %37
16: ; preds = %10
%17 = call i32 @stat(i8* %0, %struct.stat* %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %24, label %19
19: ; preds = %1, %16
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%21 = load i32, i32* %20
%22 = tail call i32 @S_ISDIR(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %6, %19, %16
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %37
27: ; preds = %19
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%29 = load i64, i64* %28
%30 = tail call i32 (...) @getuid()
%31 = sext i32 %30 to i64
%32 = icmp eq i64 %29, %31
br i1 %32, label %37, label %33
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = tail call i32 (...) @getuid()
%36 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %29, i32 %35)
br label %37
37: ; preds = %27, %33, %24, %13
%38 = phi i32 [ 1, %13 ], [ 1, %24 ], [ 1, %33 ], [ 0, %27 ]
ret i32 %38
}