#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 *);
scalar_t__ 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[] = "/weston-shared-XXXXXX";
  const char *path;
  char *name;
  int fd;
  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;
  if (ftruncate(fd, size) < 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    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    127b <os_create_anonymous_file+0xeb>
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    127b <os_create_anonymous_file+0xeb>
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    127b <os_create_anonymous_file+0xeb>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1070 <ftruncate@plt>
cmp    $0x0,%rax
jge    1275 <os_create_anonymous_file+0xe5>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <os_create_anonymous_file+0xeb>
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     1211 <os_create_anonymous_file+0x81>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x16(%rax),%rdi
call   10a0 <malloc@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1232 <os_create_anonymous_file+0xa2>
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     1232 <os_create_anonymous_file+0xa2>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <ftruncate@plt>
test   %rax,%rax
js     122b <os_create_anonymous_file+0x9b>
mov    %ebp,%r15d
jmp    1232 <os_create_anonymous_file+0xa2>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2dbf(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
jmp    1232 <os_create_anonymous_file+0xa2>
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 [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
store i32 %0, i32* %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = load i32, i32* @ENOENT
store i32 %11, i32* @errno
store i32 -1, i32* %2
br label %43
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
%15 = add i64 %14, 22
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %12
store i32 -1, i32* %2
br label %43
20: ; preds = %12
%21 = load i8*, i8** %5
%22 = load i8*, i8** %4
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** %5
%25 = call i32 @strcat(i8* %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @os_create_anonymous_file.template, i64 0, i64 0))
%26 = load i8*, i8** %5
%27 = call i32 @create_tmpfile_cloexec(i8* %26)
store i32 %27, i32* %6
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %20
store i32 -1, i32* %2
br label %43
33: ; preds = %20
%34 = load i32, i32* %6
%35 = load i32, i32* %3
%36 = call i64 @ftruncate(i32 %34, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %2
br label %43
41: ; preds = %33
%42 = load i32, i32* %6
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %38, %32, %19, %10
%44 = load i32, i32* %2
ret i32 %44
}

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
store i32 %5, i32* @errno
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 %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 ([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 %22, label %17
17: ; preds = %11
%18 = tail call i64 @ftruncate(i32 %14, i32 %0)
%19 = icmp slt i64 %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
}