#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 ssize_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int copyfile(int, int);
int mkstemp(char *);
int open(char *, int /*<<< orphan*/);
int umask(int);
ssize_t copytemp(char *inpath, char *outtemplate) {
  int infd = open(inpath, O_RDONLY);
  int outfd;
  mode_t oldmask;
  ssize_t rv;
  if (infd < 0)
    return -1;
  oldmask = umask(077);
  outfd = mkstemp(outtemplate);
  umask(oldmask);
  if (outfd < 0) {
    close(infd);
    return -1;
  }
  rv = copyfile(infd, outfd);
  close(infd);
  close(outfd);
  return rv;
}


x86 assembly (clang, -O0):
<copytemp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118b <copytemp+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <copytemp+0x9e>
mov    $0x3f,%edi
call   1040 <umask@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <mkstemp@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <umask@plt>
cmpl   $0x0,-0x20(%rbp)
jge    11ca <copytemp+0x7a>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <copytemp+0x9e>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <copyfile@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copytemp>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11a5 <copytemp+0x55>
mov    %eax,%ebx
mov    $0x3f,%edi
call   1040 <umask@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1050 <mkstemp@plt>
mov    %eax,%ebp
mov    %r14d,%edi
call   1040 <umask@plt>
test   %ebp,%ebp
js     11ad <copytemp+0x5d>
mov    %ebx,%edi
mov    %ebp,%esi
call   1070 <copyfile@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%ebx
jmp    11b3 <copytemp+0x63>
mov    $0xffffffff,%r14d
jmp    11ba <copytemp+0x6a>
mov    $0xffffffff,%r14d
mov    %ebx,%edi
call   1030 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @copytemp(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %36
16: ; preds = %2
%17 = call i32 @umask(i32 63)
store i32 %17, i32* %8
%18 = load i8*, i8** %5
%19 = call i32 @mkstemp(i8* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %8
%21 = call i32 @umask(i32 %20)
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %3
br label %36
27: ; preds = %16
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i32 @copyfile(i32 %28, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %9
store i32 %35, i32* %3
br label %36
36: ; preds = %27, %24, %15
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @copytemp(i8* nocapture %0, i8* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %2
%7 = tail call i32 @umask(i32 63)
%8 = tail call i32 @mkstemp(i8* %1)
%9 = tail call i32 @umask(i32 %7)
%10 = icmp slt i32 %8, 0
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = tail call i32 @copyfile(i32 %4, i32 %8)
%13 = tail call i32 @close(i32 %4)
br label %14
14: ; preds = %6, %11
%15 = phi i32 [ %8, %11 ], [ %4, %6 ]
%16 = phi i32 [ %12, %11 ], [ -1, %6 ]
%17 = tail call i32 @close(i32 %15)
br label %18
18: ; preds = %14, %2
%19 = phi i32 [ -1, %2 ], [ %16, %14 ]
ret i32 %19
}