#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 ENOENT;
int /*<<< orphan*/ close(int);
int create_tmpfile_cloexec(char *);
int errno;
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(scalar_t__);
int posix_fallocate(int, int /*<<< orphan*/, int /*<<< orphan*/);
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[] = "/weston-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 = posix_fallocate(fd, 0, size);
  if (ret != 0) {
    close(fd);
    errno = ret;
    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    0xe74(%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    128c <os_create_anonymous_file+0xfc>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x16,%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    128c <os_create_anonymous_file+0xfc>
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   1080 <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    128c <os_create_anonymous_file+0xfc>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%edx
xor    %esi,%esi
call   1090 <posix_fallocate@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1286 <os_create_anonymous_file+0xf6>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2d68(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <os_create_anonymous_file+0xfc>
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    0xe75(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1218 <os_create_anonymous_file+0x88>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x16(%rax),%rdi
call   10a0 <malloc@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1230 <os_create_anonymous_file+0xa0>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
lea    0xe21(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <strcat@plt>
mov    %rbp,%rdi
call   1080 <create_tmpfile_cloexec@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <free@plt>
test   %ebx,%ebx
js     1230 <os_create_anonymous_file+0xa0>
mov    %ebx,%edi
xor    %esi,%esi
mov    %r14d,%edx
call   1090 <posix_fallocate@plt>
test   %eax,%eax
je     123e <os_create_anonymous_file+0xae>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1221 <os_create_anonymous_file+0x91>
mov    0x2db1(%rip),%rax
mov    (%rax),%ebp
mov    0x2db8(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%r15d
jmp    1230 <os_create_anonymous_file+0xa0>

LLVM IR (-O0):
@os_create_anonymous_file.template = internal constant [22 x i8] c"/weston-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 %46
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = add i64 %15, 22
%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 %46
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 ([22 x i8], [22 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 %46
34: ; preds = %21
%35 = load i32, i32* %6
%36 = load i32, i32* %3
%37 = call i32 @posix_fallocate(i32 %35, i32 0, i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %7
store i32 %43, i32* @errno
store i32 -1, i32* %2
br label %46
44: ; preds = %34
%45 = load i32, i32* %6
store i32 %45, i32* %2
br label %46
46: ; preds = %44, %40, %33, %20, %11
%47 = load i32, i32* %2
ret i32 %47
}

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