#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 EINTR;
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 create_anonymous_file(off_t size) {
  static const char template[] = "/wayland-test-client-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;
  do
    ret = posix_fallocate(fd, 0, size);
  while (ret == EINTR);
  if (ret != 0) {
    close(fd);
    errno = ret;
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_anonymous_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe81(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d4 <create_anonymous_file+0x44>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <create_anonymous_file+0x113>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x23,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1204 <create_anonymous_file+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <create_anonymous_file+0x113>
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 <create_anonymous_file+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <create_anonymous_file+0x113>
jmp    1251 <create_anonymous_file+0xc1>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%edx
xor    %esi,%esi
call   1090 <posix_fallocate@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%eax
je     1251 <create_anonymous_file+0xc1>
cmpl   $0x0,-0x20(%rbp)
je     129d <create_anonymous_file+0x10d>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2d51(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <create_anonymous_file+0x113>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_anonymous_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r12d
lea    0xe81(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1230 <create_anonymous_file+0xa0>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x23(%rax),%rdi
call   10a0 <malloc@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     1248 <create_anonymous_file+0xb8>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
lea    0xe1c(%rip),%rsi
mov    %r15,%rdi
call   10b0 <strcat@plt>
mov    %r15,%rdi
call   1080 <create_tmpfile_cloexec@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <free@plt>
test   %ebp,%ebp
js     1248 <create_anonymous_file+0xb8>
mov    0x2dcf(%rip),%r14
nopl   0x0(%rax)
mov    %ebp,%edi
xor    %esi,%esi
mov    %r12d,%edx
call   1090 <posix_fallocate@plt>
cmp    (%r14),%eax
je     1210 <create_anonymous_file+0x80>
mov    %eax,%ebx
test   %eax,%eax
je     1254 <create_anonymous_file+0xc4>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1239 <create_anonymous_file+0xa9>
mov    0x2d91(%rip),%rax
mov    (%rax),%ebx
mov    0x2da0(%rip),%rax
mov    %ebx,(%rax)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%r14d
jmp    1248 <create_anonymous_file+0xb8>

LLVM IR (-O0):
@create_anonymous_file.template = internal constant [35 x i8] c"/wayland-test-client-shared-XXXXXX\00"
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@ENOENT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @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 %52
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = add i64 %15, 35
%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 %52
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 ([35 x i8], [35 x i8]* @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 %52
34: ; preds = %21
br label %35
35: ; preds = %39, %34
%36 = load i32, i32* %6
%37 = load i32, i32* %3
%38 = call i32 @posix_fallocate(i32 %36, i32 0, i32 %37)
store i32 %38, i32* %7
br label %39
39: ; preds = %35
%40 = load i32, i32* %7
%41 = load i32, i32* @EINTR
%42 = icmp eq i32 %40, %41
br i1 %42, label %35, label %43
43: ; preds = %39
%44 = load i32, i32* %7
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %7
store i32 %49, i32* @errno
store i32 -1, i32* %2
br label %52
50: ; preds = %43
%51 = load i32, i32* %6
store i32 %51, i32* %2
br label %52
52: ; preds = %50, %46, %33, %20, %11
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@create_anonymous_file.template = internal constant [35 x i8] c"/wayland-test-client-shared-XXXXXX\00"
@.str = [16 x i8] c"XDG_RUNTIME_DIR\00"
@ENOENT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @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 %25
6: ; preds = %1
%7 = tail call i64 @strlen(i8* %2)
%8 = add i64 %7, 35
%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 ([35 x i8], [35 x i8]* @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 @posix_fallocate(i32 %14, i32 0, i32 %0)
%19 = load i32, i32* @EINTR
%20 = icmp eq i32 %18, %19
br i1 %20, label %17, label %21
21: ; preds = %17
%22 = icmp eq i32 %18, 0
br i1 %22, label %27, label %23
23: ; preds = %21
%24 = tail call i32 @close(i32 %14)
br label %25
25: ; preds = %4, %23
%26 = phi i32 [ %18, %23 ], [ %5, %4 ]
store i32 %26, i32* @errno
br label %27
27: ; preds = %25, %21, %11, %6
%28 = phi i32 [ -1, %6 ], [ -1, %11 ], [ %14, %21 ], [ -1, %25 ]
ret i32 %28
}