#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 */
typedef int /*<<< orphan*/ template;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ ENOENT;
int /*<<< orphan*/ close(int);
int create_tmpfile_cloexec(char *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int ftruncate(int, int /*<<< orphan*/);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int os_create_anonymous_file(off_t size) {
  static const char template[] = "/redshift-shared-XXXXXX";
  const char *path = getenv("XDG_RUNTIME_DIR");
  if (!path) {
    errno = ENOENT;
    return -1;
  }
  char *name = malloc(strlen(path) + sizeof(template));
  if (!name) {
    return -1;
  }
  strcpy(name, path);
  strcat(name, template);
  int fd = create_tmpfile_cloexec(name);
  free(name);
  if (fd < 0) {
    return -1;
  }
  int ret;
  do {
    ret = ftruncate(fd, size);
  } while (ret < 0 && errno == EINTR);
  if (ret < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<os_create_anonymous_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d6 <os_create_anonymous_file+0x46>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e19(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <os_create_anonymous_file+0x129>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x18,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1206 <os_create_anonymous_file+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <os_create_anonymous_file+0x129>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x18(%rbp),%rdi
call   1090 <create_tmpfile_cloexec@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    124e <os_create_anonymous_file+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <os_create_anonymous_file+0x129>
jmp    1253 <os_create_anonymous_file+0xc3>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1070 <ftruncate@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jge    128a <os_create_anonymous_file+0xfa>
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    0x2d57(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1253 <os_create_anonymous_file+0xc3>
cmpl   $0x0,-0x20(%rbp)
jge    12b3 <os_create_anonymous_file+0x123>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <os_create_anonymous_file+0x129>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_create_anonymous_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r12d
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     123f <os_create_anonymous_file+0xaf>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x18(%rax),%rdi
call   10a0 <malloc@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     1262 <os_create_anonymous_file+0xd2>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
lea    0xe18(%rip),%rsi
mov    %r15,%rdi
call   10b0 <strcat@plt>
mov    %r15,%rdi
call   1090 <create_tmpfile_cloexec@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <free@plt>
test   %ebp,%ebp
js     1262 <os_create_anonymous_file+0xd2>
mov    0x2dd3(%rip),%r15
mov    0x2dc4(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r12d,%esi
call   1070 <ftruncate@plt>
test   %eax,%eax
jns    1236 <os_create_anonymous_file+0xa6>
mov    (%rbx),%rcx
cmp    %rcx,(%r15)
je     1220 <os_create_anonymous_file+0x90>
test   %eax,%eax
js     125b <os_create_anonymous_file+0xcb>
mov    %ebp,%r14d
jmp    1262 <os_create_anonymous_file+0xd2>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    0x2d90(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%r14d
jmp    1262 <os_create_anonymous_file+0xd2>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@os_create_anonymous_file.template = internal constant [24 x i8] c"/redshift-shared-XXXXXX\00"
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@ENOENT = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @os_create_anonymous_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = load i64, i64* @ENOENT
store i64 %12, i64* @errno
store i32 -1, i32* %2
br label %56
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = add i64 %15, 24
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %13
store i32 -1, i32* %2
br label %56
21: ; preds = %13
%22 = load i8*, i8** %5
%23 = load i8*, i8** %4
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i8*, i8** %5
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @os_create_anonymous_file.template, i64 0, i64 0))
%27 = load i8*, i8** %5
%28 = call i32 @create_tmpfile_cloexec(i8* %27)
store i32 %28, i32* %6
%29 = load i8*, i8** %5
%30 = call i32 @free(i8* %29)
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %21
store i32 -1, i32* %2
br label %56
34: ; preds = %21
br label %35
35: ; preds = %46, %34
%36 = load i32, i32* %6
%37 = load i32, i32* %3
%38 = call i32 @ftruncate(i32 %36, i32 %37)
store i32 %38, i32* %7
br label %39
39: ; preds = %35
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br label %46
46: ; preds = %42, %39
%47 = phi i1 [ false, %39 ], [ %45, %42 ]
br i1 %47, label %35, label %48
48: ; preds = %46
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
store i32 -1, i32* %2
br label %56
54: ; preds = %48
%55 = load i32, i32* %6
store i32 %55, i32* %2
br label %56
56: ; preds = %54, %51, %33, %20, %11
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
@os_create_anonymous_file.template = internal constant [24 x i8] c"/redshift-shared-XXXXXX\00"
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@ENOENT = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @os_create_anonymous_file(i32 %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = load i64, i64* @ENOENT
store i64 %5, i64* @errno
br label %27
6: ; preds = %1
%7 = tail call i64 @strlen(i8* %2)
%8 = add i64 %7, 24
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %27, label %11
11: ; preds = %6
%12 = tail call i32 @strcpy(i8* %9, i8* %2)
%13 = tail call i32 @strcat(i8* %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @os_create_anonymous_file.template, i64 0, i64 0))
%14 = tail call i32 @create_tmpfile_cloexec(i8* %9)
%15 = tail call i32 @free(i8* %9)
%16 = icmp slt i32 %14, 0
br i1 %16, label %27, label %17
17: ; preds = %11, %17
%18 = tail call i32 @ftruncate(i32 %14, i32 %0)
%19 = icmp slt i32 %18, 0
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %17, label %24
24: ; preds = %17
br i1 %19, label %25, label %27
25: ; preds = %24
%26 = tail call i32 @close(i32 %14)
br label %27
27: ; preds = %6, %25, %24, %11, %4
%28 = phi i32 [ -1, %4 ], [ -1, %6 ], [ -1, %11 ], [ -1, %25 ], [ %14, %24 ]
ret i32 %28
}