#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 */

/* Variables and functions */
int /*<<< orphan*/ abortmsg(char *, int);
scalar_t__ checkruntimedir(char const *);
char *getenv(char *);
int geteuid();
int snprintf(char *, size_t, char *, char const *, ...);
void getdefaultsockdir(char sockdir[], size_t size) {
  const char *runtimedir = getenv("XDG_RUNTIME_DIR");
  int r;
  if (runtimedir && checkruntimedir(runtimedir)) {
    r = snprintf(sockdir, size, "%s/chg", runtimedir);
  } else {
    const char *tmpdir = getenv("TMPDIR");
    if (!tmpdir)
      tmpdir = "/tmp";
    r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid());
  }
  if (r < 0 || (size_t)r >= size)
    abortmsg("too long TMPDIR (r = %d)", r);
}


x86 assembly (clang, -O0):
<getdefaultsockdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe99(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b0 <getdefaultsockdir+0x60>
mov    -0x18(%rbp),%rdi
call   1070 <checkruntimedir@plt>
cmp    $0x0,%rax
je     11b0 <getdefaultsockdir+0x60>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
lea    0xe6f(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    1215 <getdefaultsockdir+0xc5>
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d6 <getdefaultsockdir+0x86>
lea    0xe4c(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    %eax,%r8d
lea    0xe18(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     122d <getdefaultsockdir+0xdd>
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     123c <getdefaultsockdir+0xec>
mov    -0x1c(%rbp),%esi
lea    0xdf5(%rip),%rdi
call   1060 <abortmsg@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdefaultsockdir>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe9e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1199 <getdefaultsockdir+0x49>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <checkruntimedir@plt>
test   %rax,%rax
je     1199 <getdefaultsockdir+0x49>
lea    0xe8d(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
test   %eax,%eax
jns    11d8 <getdefaultsockdir+0x88>
jmp    11e6 <getdefaultsockdir+0x96>
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe6f(%rip),%rbx
cmovne %rax,%rbx
xor    %eax,%eax
call   1050 <geteuid@plt>
lea    0xe62(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
test   %eax,%eax
js     11e6 <getdefaultsockdir+0x96>
movslq %eax,%rcx
cmp    %r14,%rcx
jae    11e6 <getdefaultsockdir+0x96>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe3f(%rip),%rdi
mov    %eax,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <abortmsg@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@.str.1 = [7 x i8] c"%s/chg\00"
@.str.2 = [7 x i8] c"TMPDIR\00"
@.str.3 = [5 x i8] c"/tmp\00"
@.str.4 = [9 x i8] c"%s/chg%d\00"
@.str.5 = [25 x i8] c"too long TMPDIR (r = %d)\00"
define void @getdefaultsockdir(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %20
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = call i64 @checkruntimedir(i8* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = load i64, i64* %4
%18 = load i8*, i8** %5
%19 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %16, i64 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 %19, i32* %6
br label %31
20: ; preds = %11, %2
%21 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %20
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %25
25: ; preds = %24, %20
%26 = load i8*, i8** %3
%27 = load i64, i64* %4
%28 = load i8*, i8** %7
%29 = call i32 (...) @geteuid()
%30 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %28, i32 %29)
store i32 %30, i32* %6
br label %31
31: ; preds = %25, %15
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %31
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = load i64, i64* %4
%38 = icmp uge i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %34, %31
%40 = load i32, i32* %6
%41 = call i32 @abortmsg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %39, %34
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@.str.1 = [7 x i8] c"%s/chg\00"
@.str.2 = [7 x i8] c"TMPDIR\00"
@.str.3 = [5 x i8] c"/tmp\00"
@.str.4 = [9 x i8] c"%s/chg%d\00"
@.str.5 = [25 x i8] c"too long TMPDIR (r = %d)\00"
define void @getdefaultsockdir(i8* %0, i64 %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call i64 @checkruntimedir(i8* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %16
10: ; preds = %5, %2
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i8* %11, null
%13 = select i1 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %11
%14 = tail call i32 (...) @geteuid()
%15 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %13, i32 %14)
br label %16
16: ; preds = %10, %8
%17 = phi i32 [ %9, %8 ], [ %15, %10 ]
%18 = icmp sgt i32 %17, -1
%19 = sext i32 %17 to i64
%20 = icmp ult i64 %19, %1
%21 = and i1 %18, %20
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = tail call i32 @abortmsg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %17)
br label %24
24: ; preds = %16, %22
ret void
}