#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*/ suffix;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
size_t strlen(char const *);
int umask(int);
int unlink(char *);
int create_unlinked_temp_file(const char *tmpdir) {
  mode_t old_mask = umask(0177);
  const char suffix[] = "/silence_XXXXXX";
  size_t n = strlen(tmpdir);
  char s[n + sizeof(suffix)];
  memcpy(s, tmpdir, n);
  memcpy(s + n, suffix, sizeof(suffix));
  int fd = mkstemp(s);
  do {
    if (fd == -1) {
      perror("creating temp file");
      exit(1);
    }
  } while (0);
  umask(old_mask);
  int r = unlink(s);
  do {
    if (r == -1) {
      perror("unlinking temp file");
      exit(1);
    }
  } while (0);
  return fd;
}


x86 assembly (clang, -O0):
<create_unlinked_temp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x7f,%edi
call   1050 <umask@plt>
mov    %eax,-0xc(%rbp)
movaps 0xe70(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    %rcx,%rax
add    $0x10,%rax
mov    %rsp,%rdx
mov    %rdx,-0x30(%rbp)
add    $0x1f,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rdi
add    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rdi
call   1070 <mkstemp@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    121d <create_unlinked_temp_file+0xad>
lea    0xe02(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1222 <create_unlinked_temp_file+0xb2>
jmp    1227 <create_unlinked_temp_file+0xb7>
mov    -0xc(%rbp),%edi
call   1050 <umask@plt>
mov    -0x48(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    125b <create_unlinked_temp_file+0xeb>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1260 <create_unlinked_temp_file+0xf0>
jmp    1265 <create_unlinked_temp_file+0xf5>
mov    -0x3c(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_unlinked_temp_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    $0x7f,%edi
call   1050 <umask@plt>
mov    %eax,%r14d
movaps 0xe68(%rip),%xmm0
movaps %xmm0,-0x40(%rbp)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rsp,%r13
mov    %rsp,%rbx
add    $0x1f,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
add    %rbx,%r12
lea    -0x40(%rbp),%rsi
mov    %r12,%rdi
mov    $0x10,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
call   1070 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1217 <create_unlinked_temp_file+0xa7>
mov    %eax,%r15d
mov    %r14d,%edi
call   1050 <umask@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     1220 <create_unlinked_temp_file+0xb0>
mov    %r13,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf2(%rip),%rdi
jmp    1227 <create_unlinked_temp_file+0xb7>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.create_unlinked_temp_file.suffix = [16 x i8] c"/silence_XXXXXX\00"
@.str = [19 x i8] c"creating temp file\00"
@.str.1 = [20 x i8] c"unlinking temp file\00"
define i32 @create_unlinked_temp_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %2
%10 = call i32 @umask(i32 127)
store i32 %10, i32* %3
%11 = bitcast [16 x i8]* %4 to i8*
%12 = load i8*, i8** %2
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %5
%14 = load i64, i64* %5
%15 = add i64 %14, 16
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i8*, i8** %2
%19 = load i64, i64* %5
%20 = trunc i64 %19 to i32
%21 = call i32 @memcpy(i8* %17, i8* %18, i32 %20)
%22 = load i64, i64* %5
%23 = getelementptr inbounds i8, i8* %17, i64 %22
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%25 = call i32 @memcpy(i8* %23, i8* %24, i32 16)
%26 = call i32 @mkstemp(i8* %17)
store i32 %26, i32* %8
br label %27
27: ; preds = %1
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
%36 = load i32, i32* %3
%37 = call i32 @umask(i32 %36)
%38 = call i32 @unlink(i8* %17)
store i32 %38, i32* %9
br label %39
39: ; preds = %35
%40 = load i32, i32* %9
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %39
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46
%48 = load i32, i32* %8
%49 = load i8*, i8** %6
ret i32 %48
}

LLVM IR (-O2):
@__const.create_unlinked_temp_file.suffix = [16 x i8] c"/silence_XXXXXX\00"
@.str = [19 x i8] c"creating temp file\00"
@.str.1 = [20 x i8] c"unlinking temp file\00"
define i32 @create_unlinked_temp_file(i8* %0) {
%2 = alloca [16 x i8]
%3 = tail call i32 @umask(i32 127)
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add i64 %5, 16
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = trunc i64 %5 to i32
%10 = call i32 @memcpy(i8* %8, i8* %0, i32 %9)
%11 = getelementptr inbounds i8, i8* %8, i64 %5
%12 = call i32 @memcpy(i8* %11, i8* %4, i32 16)
%13 = call i32 @mkstemp(i8* %8)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = call i32 @umask(i32 %3)
%20 = call i32 @unlink(i8* %8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
ret i32 %13
}