#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 */

/* Variables and functions */
int mkstemp(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int unlink(char *);
int memfd_create(char *name) {
  char buffer[256];
  int fd;
  snprintf(buffer, 256, "/tmp/%s-XXXXXX", name);
  fd = mkstemp(buffer);
  if (fd == -1)
    return -1;
  return unlink(buffer) > -1 ? fd : -1;
}


x86 assembly (clang, -O0):
<memfd_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xeaa(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1050 <mkstemp@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    1186 <memfd_create+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <memfd_create+0x95>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jle    11ac <memfd_create+0x7c>
mov    -0x114(%rbp),%eax
mov    %eax,-0x118(%rbp)
jmp    11bc <memfd_create+0x8c>
mov    $0xffffffff,%eax
mov    %eax,-0x118(%rbp)
jmp    11bc <memfd_create+0x8c>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memfd_create>:
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rcx
lea    0xebe(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1175 <memfd_create+0x45>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1030 <unlink@plt>
test   %eax,%eax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    117a <memfd_create+0x4a>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/tmp/%s-XXXXXX\00"
define i32 @memfd_create(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i32
store i8* %0, i8** %3
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i8*, i8** %3
%8 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = call i32 @mkstemp(i8* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %23
14: ; preds = %1
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = call i32 @unlink(i8* %15)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %5
br label %21
20: ; preds = %14
br label %21
21: ; preds = %20, %18
%22 = phi i32 [ %19, %18 ], [ -1, %20 ]
store i32 %22, i32* %2
br label %23
23: ; preds = %21, %13
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@.str = [15 x i8] c"/tmp/%s-XXXXXX\00"
define i32 @memfd_create(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32 @mkstemp(i8* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = call i32 @unlink(i8* %3)
%9 = icmp sgt i32 %8, -1
%10 = select i1 %9, i32 %5, i32 -1
br label %11
11: ; preds = %1, %7
%12 = phi i32 [ %10, %7 ], [ -1, %1 ]
ret i32 %12
}