#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 O_CREAT;
int O_WRONLY;
int _copyfile(char *, int);
scalar_t__ close(int);
int /*<<< orphan*/ isp_dbgfail(char *, char *);
int open(char *, int, int);
int util_mkcopy(char *path, char *npath) {
  int nfd;
  int res = 0;
  if ((nfd = open(npath, O_WRONLY | O_CREAT, 0600)) < 0) {
    isp_dbgfail("util_mkcopy: open O_WRONLY | O_CREAT %s: %m", npath);
    res = 21;
    goto done;
  }
  if ((res = _copyfile(path, nfd)) != 0)
    goto done;
done:
  if (nfd >= 0 && close(nfd) < 0) {
    isp_dbgfail("util_mkcopy: close %s: %m", npath);
    res = 21;
  }
  return res;
}


x86 assembly (clang, -O0):
<util_mkcopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e8d(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    119f <util_mkcopy+0x5f>
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1050 <isp_dbgfail@plt>
movl   $0x15,-0x18(%rbp)
jmp    11c1 <util_mkcopy+0x81>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <_copyfile@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     11bc <util_mkcopy+0x7c>
jmp    11c1 <util_mkcopy+0x81>
jmp    11c1 <util_mkcopy+0x81>
cmpl   $0x0,-0x14(%rbp)
jl     11f4 <util_mkcopy+0xb4>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    11f4 <util_mkcopy+0xb4>
mov    -0x10(%rbp),%rsi
lea    0xe44(%rip),%rdi
call   1050 <isp_dbgfail@plt>
movl   $0x15,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_mkcopy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e7c(%rip),%rax
mov    0x2e9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x180,%edx
call   1060 <open@plt>
test   %eax,%eax
js     1193 <util_mkcopy+0x53>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <_copyfile@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %rax,%rax
jns    11a7 <util_mkcopy+0x67>
lea    0xe9b(%rip),%rdi
jmp    119a <util_mkcopy+0x5a>
lea    0xe66(%rip),%rdi
mov    %r15,%rsi
call   1050 <isp_dbgfail@plt>
mov    $0x15,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [44 x i8] c"util_mkcopy: open O_WRONLY | O_CREAT %s: %m\00"
@.str.1 = [26 x i8] c"util_mkcopy: close %s: %m\00"
define i32 @util_mkcopy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%7 = load i8*, i8** %4
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10, i32 384)
store i32 %11, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @isp_dbgfail(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 21, i32* %6
br label %23
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = load i32, i32* %5
%19 = call i32 @_copyfile(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %23
22: ; preds = %16
br label %23
23: ; preds = %22, %21, %13
%24 = load i32, i32* %5
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i32, i32* %5
%28 = call i64 @close(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i8*, i8** %4
%32 = call i32 @isp_dbgfail(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %31)
store i32 21, i32* %6
br label %33
33: ; preds = %30, %26, %23
%34 = load i32, i32* %6
ret i32 %34
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [44 x i8] c"util_mkcopy: open O_WRONLY | O_CREAT %s: %m\00"
@.str.1 = [26 x i8] c"util_mkcopy: close %s: %m\00"
define i32 @util_mkcopy(i8* %0, i8* %1) {
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* %1, i32 %5, i32 384)
%7 = icmp slt i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = tail call i32 @_copyfile(i8* %0, i32 %6)
%10 = tail call i64 @close(i32 %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8, %2
%13 = phi i8* [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %8 ]
%14 = tail call i32 @isp_dbgfail(i8* %13, i8* %1)
br label %15
15: ; preds = %12, %8
%16 = phi i32 [ %9, %8 ], [ 21, %12 ]
ret i32 %16
}