#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_uid;
  int st_mode;
};
struct passwd {
  int /*<<< orphan*/ pw_name;
};
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int getpwuid_r(int, struct passwd *, char *, int, struct passwd **);
int getuid();
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
unsigned long strtol(char *, int /*<<< orphan*/ *, int);
const char *get_conndir() {
  static char conndir[64];
  static pthread_mutex_t mutex;
  static int inited;
  char username[32 + 1];
  struct passwd pw;
  struct passwd *pwres;
  char buf[256];
  int uid;
  int res;
  struct stat stbuf;
  pthread_mutex_lock(&mutex);
  if (!inited) {
    char *variant;
    char varbuf[32];
    inited = 1;
    uid = getuid();
    res = getpwuid_r(uid, &pw, buf, 256, &pwres);
    if (res == 0) {
      strncpy(username, pwres->pw_name, 32);
      username[32] = '\0';
    } else
      sprintf(username, "%i", uid);
    varbuf[0] = '\0';
    variant = getenv("AVFS_VARIANT");
    if (variant != NULL) {
      unsigned long i = strtol(variant, NULL, 10);
      sprintf(varbuf, "-%lu", i);
    }
    sprintf(conndir, "/tmp/.avfsconn-%s%s", username, varbuf);
    res = stat(conndir, &stbuf);
    if (res == -1 && errno == ENOENT) {
      mkdir(conndir, 0700);
      res = stat(conndir, &stbuf);
    }
    if (res == -1 || stbuf.st_uid != uid || (stbuf.st_mode & 0077) != 0) {
      fprintf(stderr, "AVFS: please check permissions of <%s>\n", conndir);
      conndir[0] = '\0';
    }
  }
  pthread_mutex_unlock(&mutex);
  if (conndir[0] != '\0')
    return conndir;
  else
    return NULL;
}


x86 assembly (clang, -O0):
<get_conndir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
lea    0x2f0e(%rip),%rdi
call   10d0 <pthread_mutex_lock@plt>
cmpl   $0x0,0x2f06(%rip)
jne    1395 <get_conndir+0x1e5>
movl   $0x1,0x2ef6(%rip)
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,-0x144(%rbp)
mov    -0x144(%rbp),%edi
lea    -0x140(%rbp),%rdx
lea    -0x38(%rbp),%rsi
mov    $0x100,%ecx
lea    -0x40(%rbp),%r8
call   1060 <getpwuid_r@plt>
mov    %eax,-0x148(%rbp)
cmpl   $0x0,-0x148(%rbp)
jne    123a <get_conndir+0x8a>
lea    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
mov    $0x20,%edx
call   1040 <strncpy@plt>
movb   $0x0,-0x10(%rbp)
jmp    1252 <get_conndir+0xa2>
lea    -0x30(%rbp),%rdi
mov    -0x144(%rbp),%edx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
movb   $0x0,-0x180(%rbp)
lea    0xda3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
je     12b2 <get_conndir+0x102>
mov    -0x158(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %rax,-0x188(%rbp)
lea    -0x180(%rbp),%rdi
mov    -0x188(%rbp),%rdx
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
lea    -0x30(%rbp),%rdx
lea    -0x180(%rbp),%rcx
lea    0x2dcc(%rip),%rdi
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
lea    0x2db7(%rip),%rdi
lea    -0x150(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x148(%rbp)
cmpl   $0xffffffff,-0x148(%rbp)
jne    133c <get_conndir+0x18c>
mov    0x2cd9(%rip),%rax
mov    (%rax),%rax
mov    0x2cbf(%rip),%rcx
cmp    (%rcx),%rax
jne    133c <get_conndir+0x18c>
lea    0x2d77(%rip),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
lea    0x2d66(%rip),%rdi
lea    -0x150(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x148(%rbp)
cmpl   $0xffffffff,-0x148(%rbp)
je     136d <get_conndir+0x1bd>
mov    -0x150(%rbp),%eax
cmp    -0x144(%rbp),%eax
jne    136d <get_conndir+0x1bd>
mov    -0x14c(%rbp),%eax
and    $0x3f,%eax
cmp    $0x0,%eax
je     1390 <get_conndir+0x1e0>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
lea    0xcac(%rip),%rsi
lea    0x2d0c(%rip),%rdx
call   1080 <fprintf@plt>
movb   $0x0,0x2d00(%rip)
jmp    1395 <get_conndir+0x1e5>
lea    0x2d34(%rip),%rdi
call   10b0 <pthread_mutex_unlock@plt>
movsbl 0x2ce8(%rip),%eax
cmp    $0x0,%eax
je     13c1 <get_conndir+0x211>
lea    0x2cd8(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c9 <get_conndir+0x219>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_conndir>:
push   %rbp
push   %rbx
sub    $0x178,%rsp
lea    0x2f10(%rip),%rdi
call   10d0 <pthread_mutex_lock@plt>
cmpb   $0x0,0x2f08(%rip)
jne    1315 <get_conndir+0x165>
movb   $0x1,0x2efb(%rip)
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%ebp
lea    0x18(%rsp),%rsi
lea    0x70(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %eax,%edi
mov    $0x100,%ecx
call   1060 <getpwuid_r@plt>
test   %eax,%eax
je     1218 <get_conndir+0x68>
lea    0xdf8(%rip),%rsi
lea    0x20(%rsp),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   10c0 <sprintf@plt>
jmp    1233 <get_conndir+0x83>
mov    0x10(%rsp),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdi
mov    $0x20,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x40(%rsp)
movb   $0x0,0x50(%rsp)
lea    0xdc4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     126e <get_conndir+0xbe>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
lea    0xdb1(%rip),%rsi
lea    0x50(%rsp),%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   10c0 <sprintf@plt>
lea    0x2e1b(%rip),%rbx
lea    0xd99(%rip),%rsi
lea    0x20(%rsp),%rdx
lea    0x50(%rsp),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
call   10c0 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     12b6 <get_conndir+0x106>
cmp    $0xffffffff,%eax
je     12f2 <get_conndir+0x142>
cmp    %ebp,0x8(%rsp)
jne    12f2 <get_conndir+0x142>
testb  $0x3f,0xc(%rsp)
jne    12f2 <get_conndir+0x142>
jmp    1315 <get_conndir+0x165>
mov    0x2d1b(%rip),%rax
mov    (%rax),%rax
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%rax
jne    12f2 <get_conndir+0x142>
lea    0x2dbd(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    12a7 <get_conndir+0xf7>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
lea    0x2d87(%rip),%rdx
call   1080 <fprintf@plt>
movb   $0x0,0x2d7b(%rip)
lea    0x2db4(%rip),%rdi
call   10b0 <pthread_mutex_unlock@plt>
lea    0x2d68(%rip),%rcx
xor    %eax,%eax
cmpb   $0x0,0x2d5f(%rip)
cmovne %rcx,%rax
add    $0x178,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.stat = type { i32, i32 }
@get_conndir.conndir = internal global [64 x i8] zeroinitializer
@get_conndir.mutex = internal global i32 0
@get_conndir.inited = internal global i32 0
@.str = [3 x i8] c"%i\00"
@.str.1 = [13 x i8] c"AVFS_VARIANT\00"
@.str.2 = [5 x i8] c"-%lu\00"
@.str.3 = [20 x i8] c"/tmp/.avfsconn-%s%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.4 = [40 x i8] c"AVFS: please check permissions of <%s>\0A\00"
define i8* @get_conndir() {
%1 = alloca i8*
%2 = alloca [33 x i8]
%3 = alloca %struct.passwd
%4 = alloca %struct.passwd*
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca [32 x i8]
%11 = alloca i64
%12 = call i32 @pthread_mutex_lock(i32* @get_conndir.mutex)
%13 = load i32, i32* @get_conndir.inited
%14 = icmp ne i32 %13, 0
br i1 %14, label %75, label %15
15: ; preds = %0
store i32 1, i32* @get_conndir.inited
%16 = call i32 (...) @getuid()
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = call i32 @getpwuid_r(i32 %17, %struct.passwd* %3, i8* %18, i32 256, %struct.passwd** %4)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %15
%23 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 0
%24 = load %struct.passwd*, %struct.passwd** %4
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @strncpy(i8* %23, i32 %26, i32 32)
%28 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 32
store i8 0, i8* %28
br label %33
29: ; preds = %15
%30 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 0
%31 = load i32, i32* %6
%32 = call i32 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %29, %22
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
store i8 0, i8* %34
%35 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i8* %35, i8** %9
%36 = load i8*, i8** %9
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i8*, i8** %9
%40 = call i64 @strtol(i8* %39, i32* null, i32 10)
store i64 %40, i64* %11
%41 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%42 = load i64, i64* %11
%43 = call i32 (i8*, i8*, ...) @sprintf(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %42)
br label %44
44: ; preds = %38, %33
%45 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 0
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%47 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %45, i8* %46)
%48 = call i32 @stat(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), %struct.stat* %8)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %58
51: ; preds = %44
%52 = load i64, i64* @errno
%53 = load i64, i64* @ENOENT
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = call i32 @mkdir(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), i32 448)
%57 = call i32 @stat(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), %struct.stat* %8)
store i32 %57, i32* %7
br label %58
58: ; preds = %55, %51, %44
%59 = load i32, i32* %7
%60 = icmp eq i32 %59, -1
br i1 %60, label %71, label %61
61: ; preds = %58
%62 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = load i32, i32* %6
%65 = icmp ne i32 %63, %64
br i1 %65, label %71, label %66
66: ; preds = %61
%67 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%68 = load i32, i32* %67
%69 = and i32 %68, 63
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %66, %61, %58
%72 = load i32, i32* @stderr
%73 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0)
br label %74
74: ; preds = %71, %66
br label %75
75: ; preds = %74, %0
%76 = call i32 @pthread_mutex_unlock(i32* @get_conndir.mutex)
%77 = load i8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0)
%78 = sext i8 %77 to i32
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %75
store i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), i8** %1
br label %82
81: ; preds = %75
store i8* null, i8** %1
br label %82
82: ; preds = %81, %80
%83 = load i8*, i8** %1
ret i8* %83
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.stat = type { i32, i32 }
@get_conndir.conndir = internal global [64 x i8] zeroinitializer
@get_conndir.mutex = internal global i32 0
@get_conndir.inited = internal unnamed_addr global i1 false
@.str = [3 x i8] c"%i\00"
@.str.1 = [13 x i8] c"AVFS_VARIANT\00"
@.str.2 = [5 x i8] c"-%lu\00"
@.str.3 = [20 x i8] c"/tmp/.avfsconn-%s%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.4 = [40 x i8] c"AVFS: please check permissions of <%s>\0A\00"
define i8* @get_conndir() {
%1 = alloca [33 x i8]
%2 = alloca %struct.passwd
%3 = alloca %struct.passwd*
%4 = alloca [256 x i8]
%5 = alloca %struct.stat
%6 = alloca [32 x i8]
%7 = getelementptr inbounds [33 x i8], [33 x i8]* %1, i64 0, i64 0
%8 = bitcast %struct.passwd* %2 to i8*
%9 = bitcast %struct.passwd** %3 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%11 = bitcast %struct.stat* %5 to i8*
%12 = tail call i32 @pthread_mutex_lock(i32* @get_conndir.mutex)
%13 = load i1, i1* @get_conndir.inited
br i1 %13, label %60, label %14
14: ; preds = %0
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
store i1 true, i1* @get_conndir.inited
%16 = tail call i32 (...) @getuid()
%17 = call i32 @getpwuid_r(i32 %16, %struct.passwd* %2, i8* %10, i32 256, %struct.passwd** %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load %struct.passwd*, %struct.passwd** %3
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strncpy(i8* %7, i32 %22, i32 32)
%24 = getelementptr inbounds [33 x i8], [33 x i8]* %1, i64 0, i64 32
store i8 0, i8* %24
br label %27
25: ; preds = %14
%26 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %27
27: ; preds = %25, %19
store i8 0, i8* %15
%28 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i64 @strtol(i8* nocapture %28, i32* null, i32 10)
%32 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %31)
br label %33
33: ; preds = %30, %27
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %7, i8* %15)
%35 = call i32 @stat(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), %struct.stat* %5)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i64, i64* @errno
%39 = load i64, i64* @ENOENT
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %56
41: ; preds = %37
%42 = call i32 @mkdir(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), i32 448)
%43 = call i32 @stat(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0), %struct.stat* %5)
br label %44
44: ; preds = %41, %33
%45 = phi i32 [ %43, %41 ], [ %35, %33 ]
%46 = icmp ne i32 %45, -1
%47 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%48 = load i32, i32* %47
%49 = icmp eq i32 %48, %16
%50 = select i1 %46, i1 %49, i1 false
br i1 %50, label %51, label %56
51: ; preds = %44
%52 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%53 = load i32, i32* %52
%54 = and i32 %53, 63
%55 = icmp eq i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %37, %51, %44
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0)
br label %59
59: ; preds = %56, %51
br label %60
60: ; preds = %59, %0
%61 = call i32 @pthread_mutex_unlock(i32* @get_conndir.mutex)
%62 = load i8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0)
%63 = icmp eq i8 %62, 0
%64 = select i1 %63, i8* null, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_conndir.conndir, i64 0, i64 0)
ret i8* %64
}