#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 */
int /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ close(int);
int create_tmpfile_cloexec(char *);
int /*<<< orphan*/ 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[] = "/wayland-cursor-shared-XXXXXX";
  const char *path;
  char *name;
  int fd;
  int ret;
  path = getenv("XDG_RUNTIME_DIR");
  if (!path) {
    errno = ENOENT;
    return -1;
  }
  name = malloc(strlen(path) + sizeof(template));
  if (!name)
    return -1;
  strcpy(name, path);
  strcat(name, template);
  fd = create_tmpfile_cloexec(name);
  free(name);
  if (fd < 0)
    return -1;
  ret = ftruncate(fd, size);
  if (ret < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<os_create_anonymous_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe7c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d4 <os_create_anonymous_file+0x44>
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <os_create_anonymous_file+0xee>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1e,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1204 <os_create_anonymous_file+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <os_create_anonymous_file+0xee>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xde4(%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    124c <os_create_anonymous_file+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <os_create_anonymous_file+0xee>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1070 <ftruncate@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1278 <os_create_anonymous_file+0xe8>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <os_create_anonymous_file+0xee>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_create_anonymous_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1210 <os_create_anonymous_file+0x80>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x1e(%rax),%rdi
call   10a0 <malloc@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1231 <os_create_anonymous_file+0xa1>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
lea    0xe21(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
call   1090 <create_tmpfile_cloexec@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <free@plt>
test   %ebp,%ebp
js     1231 <os_create_anonymous_file+0xa1>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <ftruncate@plt>
test   %eax,%eax
js     122a <os_create_anonymous_file+0x9a>
mov    %ebp,%r15d
jmp    1231 <os_create_anonymous_file+0xa1>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    0x2dc0(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
jmp    1231 <os_create_anonymous_file+0xa1>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@os_create_anonymous_file.template = internal constant [30 x i8] c"/wayland-cursor-shared-XXXXXX\00"
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@ENOENT = global i32 0
@errno = global i32 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 i32, i32* @ENOENT
store i32 %12, i32* @errno
store i32 -1, i32* %2
br label %45
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = add i64 %15, 30
%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 %45
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 ([30 x i8], [30 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 %45
34: ; preds = %21
%35 = load i32, i32* %6
%36 = load i32, i32* %3
%37 = call i32 @ftruncate(i32 %35, i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %2
br label %45
43: ; preds = %34
%44 = load i32, i32* %6
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %40, %33, %20, %11
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@os_create_anonymous_file.template = internal constant [30 x i8] c"/wayland-cursor-shared-XXXXXX\00"
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@ENOENT = global i32 0
@errno = global i32 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 i32, i32* @ENOENT
store i32 %5, i32* @errno
br label %22
6: ; preds = %1
%7 = tail call i64 @strlen(i8* %2)
%8 = add i64 %7, 30
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %22, label %11
11: ; preds = %6
%12 = tail call i32 @strcpy(i8* %9, i8* %2)
%13 = tail call i32 @strcat(i8* %9, i8* getelementptr inbounds ([30 x i8], [30 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 %22, label %17
17: ; preds = %11
%18 = tail call i32 @ftruncate(i32 %14, i32 %0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @close(i32 %14)
br label %22
22: ; preds = %17, %11, %6, %20, %4
%23 = phi i32 [ -1, %20 ], [ -1, %4 ], [ -1, %6 ], [ -1, %11 ], [ %14, %17 ]
ret i32 %23
}