#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;
  scalar_t__ st_gid;
};
typedef int /*<<< orphan*/ mode_t;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ S_ISDIR(int);
int S_ISVTX;
scalar_t__ errno;
int /*<<< orphan*/ getgid();
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setgid(int /*<<< orphan*/);
int stat(char *, struct stat *);
char *strerror(scalar_t__);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int check_fs() {
  struct stat st;
  int r;
  r = stat("/tmp/spr16", &st);
  if (r == -1 && errno != ENOENT) {
    printf("stat(%s): %s\n", "/tmp/spr16", strerror(errno));
    return -1;
  } else if (r == -1 && errno == ENOENT) {
    mode_t um = umask(0);
    gid_t gid = getgid();
    if (setgid(0))
      goto err_gid;
    if (mkdir("/tmp/spr16", 01707)) {
      printf("mkdir(%s, 01707): %s\n", "/tmp/spr16", strerror(errno));
      return -1;
    }
    if (setgid(gid))
      goto err_gid;
    umask(um);
  } else {
    if (!S_ISDIR(st.st_mode)) {
      printf(" %s is not a directory\n", "/tmp/spr16");
      return -1;
    }
    if (!(S_ISVTX & st.st_mode)) {
      printf("sticky bit missing from %s\n", "/tmp/spr16");
      printf("chmod 01707 is one way to set this.\n");
      return -1;
    }
    if (st.st_uid != 0 || st.st_gid != 0) {
      printf("%s does not belong to root\n", "/tmp/spr16");
      return -1;
    }
  }
  return 0;
err_gid:
  printf("%s\n", strerror(errno));
  printf("cannot set group for creating %s\n", "/tmp/spr16");
  return -1;
}


x86 assembly (clang, -O0):
<check_fs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f2 <check_fs+0x72>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
je     11f2 <check_fs+0x72>
mov    0x2e12(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %rax,%rdx
lea    0xe33(%rip),%rdi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b1 <check_fs+0x231>
cmpl   $0xffffffff,-0x24(%rbp)
jne    12b2 <check_fs+0x132>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    12b2 <check_fs+0x132>
xor    %edi,%edi
call   1060 <umask@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,-0x2c(%rbp)
xor    %edi,%edi
call   1090 <setgid@plt>
cmp    $0x0,%rax
je     1240 <check_fs+0xc0>
jmp    1375 <check_fs+0x1f5>
lea    0xdb9(%rip),%rdi
mov    $0x3c7,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     128e <check_fs+0x10e>
mov    0x2d76(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %rax,%rdx
lea    0xda5(%rip),%rdi
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b1 <check_fs+0x231>
mov    -0x2c(%rbp),%edi
call   1090 <setgid@plt>
cmp    $0x0,%rax
je     12a5 <check_fs+0x125>
jmp    1375 <check_fs+0x1f5>
mov    -0x28(%rbp),%edi
call   1060 <umask@plt>
jmp    1364 <check_fs+0x1e4>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12e4 <check_fs+0x164>
lea    0xd65(%rip),%rdi
lea    0xd2f(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b1 <check_fs+0x231>
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
and    -0x20(%rbp),%eax
cmp    $0x0,%eax
jne    1328 <check_fs+0x1a8>
lea    0xd47(%rip),%rdi
lea    0xcf9(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b1 <check_fs+0x231>
cmpq   $0x0,-0x18(%rbp)
jne    133e <check_fs+0x1be>
cmpq   $0x0,-0x10(%rbp)
je     135f <check_fs+0x1df>
lea    0xd43(%rip),%rdi
lea    0xcb4(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b1 <check_fs+0x231>
jmp    1364 <check_fs+0x1e4>
jmp    1369 <check_fs+0x1e9>
movl   $0x0,-0x4(%rbp)
jmp    13b1 <check_fs+0x231>
mov    0x2c5c(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xc9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd08(%rip),%rdi
lea    0xc5d(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_fs>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xe61(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     11e7 <check_fs+0x57>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     1235 <check_fs+0xa5>
mov    0x2e24(%rip),%rax
test   %ebx,(%rax)
je     126b <check_fs+0xdb>
mov    0x10(%rsp),%rcx
xor    %eax,%eax
or     0x18(%rsp),%rcx
je     1291 <check_fs+0x101>
lea    0xe7e(%rip),%rdi
jmp    123c <check_fs+0xac>
mov    0x2dea(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2dd0(%rip),%rax
cmp    (%rax),%rdi
jne    124c <check_fs+0xbc>
xor    %edi,%edi
call   1070 <umask@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1090 <getgid@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   10a0 <setgid@plt>
test   %rax,%rax
je     129a <check_fs+0x10a>
mov    (%rbx),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
lea    0xe4c(%rip),%rdi
jmp    123c <check_fs+0xac>
lea    0xdf3(%rip),%rdi
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    128c <check_fs+0xfc>
call   10b0 <strerror@plt>
lea    0xdb3(%rip),%rdi
lea    0xda1(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    128c <check_fs+0xfc>
lea    0xdd5(%rip),%rdi
lea    0xd87(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe1a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd5f(%rip),%rdi
mov    $0x3c7,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     12c1 <check_fs+0x131>
mov    (%rbx),%rdi
call   10b0 <strerror@plt>
lea    0xd5a(%rip),%rdi
jmp    1258 <check_fs+0xc8>
mov    %ebp,%edi
call   10a0 <setgid@plt>
test   %rax,%rax
jne    121c <check_fs+0x8c>
mov    %r14d,%edi
call   1070 <umask@plt>
xor    %eax,%eax
jmp    1291 <check_fs+0x101>

LLVM IR (-O0):
%struct.stat = type { i32, i64, i64 }
@.str = [11 x i8] c"/tmp/spr16\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [14 x i8] c"stat(%s): %s\0A\00"
@.str.2 = [22 x i8] c"mkdir(%s, 01707): %s\0A\00"
@.str.3 = [24 x i8] c" %s is not a directory\0A\00"
@S_ISVTX = global i32 0
@.str.4 = [28 x i8] c"sticky bit missing from %s\0A\00"
@.str.5 = [37 x i8] c"chmod 01707 is one way to set this.\0A\00"
@.str.6 = [28 x i8] c"%s does not belong to root\0A\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@.str.8 = [34 x i8] c"cannot set group for creating %s\0A\00"
define i32 @check_fs() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %17
9: ; preds = %0
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = call i8* @strerror(i64 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %1
br label %79
17: ; preds = %9, %0
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %45
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %45
24: ; preds = %20
%25 = call i32 @umask(i32 0)
store i32 %25, i32* %4
%26 = call i32 (...) @getgid()
store i32 %26, i32* %5
%27 = call i64 @setgid(i32 0)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %74
30: ; preds = %24
%31 = call i64 @mkdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 967)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = call i8* @strerror(i64 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %35)
store i32 -1, i32* %1
br label %79
37: ; preds = %30
%38 = load i32, i32* %5
%39 = call i64 @setgid(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
br label %74
42: ; preds = %37
%43 = load i32, i32* %4
%44 = call i32 @umask(i32 %43)
br label %72
45: ; preds = %20, %17
%46 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @S_ISDIR(i32 %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %45
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %79
52: ; preds = %45
%53 = load i32, i32* @S_ISVTX
%54 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%55 = load i32, i32* %54
%56 = and i32 %53, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %52
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %79
61: ; preds = %52
%62 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%63 = load i64, i64* %62
%64 = icmp ne i64 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 2
%67 = load i64, i64* %66
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65, %61
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %79
71: ; preds = %65
br label %72
72: ; preds = %71, %42
br label %73
73: ; preds = %72
store i32 0, i32* %1
br label %79
74: ; preds = %41, %29
%75 = load i64, i64* @errno
%76 = call i8* @strerror(i64 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %79
79: ; preds = %74, %73, %69, %58, %50, %33, %13
%80 = load i32, i32* %1
ret i32 %80
}

LLVM IR (-O2):
%struct.stat = type { i32, i64, i64 }
@.str = [11 x i8] c"/tmp/spr16\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [14 x i8] c"stat(%s): %s\0A\00"
@.str.2 = [22 x i8] c"mkdir(%s, 01707): %s\0A\00"
@.str.3 = [24 x i8] c" %s is not a directory\0A\00"
@S_ISVTX = global i32 0
@.str.4 = [28 x i8] c"sticky bit missing from %s\0A\00"
@.str.6 = [28 x i8] c"%s does not belong to root\0A\00"
@.str.8 = [34 x i8] c"cannot set group for creating %s\0A\00"
@str = [36 x i8] c"chmod 01707 is one way to set this.\00"
define i32 @check_fs() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %29
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = tail call i8* @strerror(i64 %6)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %58
12: ; preds = %5
%13 = tail call i32 @umask(i32 0)
%14 = tail call i32 (...) @getgid()
%15 = tail call i64 @setgid(i32 0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %53
17: ; preds = %12
%18 = tail call i64 @mkdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 967)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = tail call i8* @strerror(i64 %21)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %58
24: ; preds = %17
%25 = tail call i64 @setgid(i32 %14)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %53
27: ; preds = %24
%28 = tail call i32 @umask(i32 %13)
br label %58
29: ; preds = %0
%30 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i32 @S_ISDIR(i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %58
36: ; preds = %29
%37 = load i32, i32* @S_ISVTX
%38 = and i32 %37, %31
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %58
43: ; preds = %36
%44 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%45 = load i64, i64* %44
%46 = icmp ne i64 %45, 0
%47 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 2
%48 = load i64, i64* %47
%49 = icmp ne i64 %48, 0
%50 = select i1 %46, i1 true, i1 %49
br i1 %50, label %51, label %58
51: ; preds = %43
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %58
53: ; preds = %12, %24
%54 = load i64, i64* @errno
%55 = tail call i8* @strerror(i64 %54)
%56 = tail call i32 @puts(i8* dereferenceable(1) %55)
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %58
58: ; preds = %43, %27, %20, %53, %51, %40, %34, %9
%59 = phi i32 [ -1, %9 ], [ -1, %53 ], [ -1, %51 ], [ -1, %40 ], [ -1, %34 ], [ -1, %20 ], [ 0, %27 ], [ 0, %43 ]
ret i32 %59
}