#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 /*<<< orphan*/ SEEK_SET;
int _copyfile(char *, int);
scalar_t__ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ isp_dbgfail(char *, char *);
scalar_t__ lseek(int, long, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int util_mktmp(int *, char **);
int util_mktmp_copy(char *opath, int *fdp, char **pathp) {
  char *path;
  int fd;
  int res = 0;
  res = util_mktmp(&fd, &path);
  if (res != 0)
    goto done;
  res = _copyfile(opath, fd);
  if (res != 0) {
    (void)close(fd);
    (void)unlink(path);
    free(path);
    goto done;
  }
  if (fdp) {
    if (lseek(fd, 0L, SEEK_SET) < 0) {
      isp_dbgfail("util_mktmp_copy: lseek %s: %m", path);
      (void)close(fd);
      (void)unlink(path);
      free(path);
      goto done;
    }
    *fdp = fd;
  } else if (close(fd) < 0) {
    isp_dbgfail("util_mktmp_copy: close %s: %m", path);
    (void)unlink(path);
    free(path);
    goto done;
  }
  if (pathp)
    *pathp = path;
done:
  return res;
}


x86 assembly (clang, -O0):
<util_mktmp_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x24(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <util_mktmp@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11aa <util_mktmp_copy+0x3a>
jmp    12a2 <util_mktmp_copy+0x132>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <_copyfile@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11e2 <util_mktmp_copy+0x72>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12a2 <util_mktmp_copy+0x132>
cmpq   $0x0,-0x10(%rbp)
je     1249 <util_mktmp_copy+0xd9>
mov    -0x24(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    123b <util_mktmp_copy+0xcb>
mov    -0x20(%rbp),%rsi
lea    0xde9(%rip),%rdi
call   1080 <isp_dbgfail@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12a2 <util_mktmp_copy+0x132>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1287 <util_mktmp_copy+0x117>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1282 <util_mktmp_copy+0x112>
mov    -0x20(%rbp),%rsi
lea    0xdb8(%rip),%rdi
call   1080 <isp_dbgfail@plt>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12a2 <util_mktmp_copy+0x132>
jmp    1287 <util_mktmp_copy+0x117>
cmpq   $0x0,-0x18(%rbp)
je     129d <util_mktmp_copy+0x12d>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    12a2 <util_mktmp_copy+0x132>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_mktmp_copy>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1090 <util_mktmp@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11c8 <util_mktmp_copy+0x58>
mov    0xc(%rsp),%esi
mov    %r12,%rdi
call   1070 <_copyfile@plt>
test   %eax,%eax
je     11d6 <util_mktmp_copy+0x66>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%rdi
call   1040 <unlink@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0xc(%rsp),%edi
test   %r15,%r15
je     11ff <util_mktmp_copy+0x8f>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %esi,%esi
call   1050 <lseek@plt>
test   %rax,%rax
js     121a <util_mktmp_copy+0xaa>
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
jmp    1209 <util_mktmp_copy+0x99>
call   1060 <close@plt>
test   %rax,%rax
js     1230 <util_mktmp_copy+0xc0>
xor    %ebx,%ebx
test   %r14,%r14
je     11c8 <util_mktmp_copy+0x58>
mov    0x10(%rsp),%rax
mov    %rax,(%r14)
jmp    11c8 <util_mktmp_copy+0x58>
mov    0x10(%rsp),%rsi
lea    0xdda(%rip),%rdi
call   1080 <isp_dbgfail@plt>
jmp    11ab <util_mktmp_copy+0x3b>
mov    0x10(%rsp),%rsi
lea    0xde2(%rip),%rdi
call   1080 <isp_dbgfail@plt>
mov    0x10(%rsp),%rdi
call   1040 <unlink@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    11c8 <util_mktmp_copy+0x58>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [30 x i8] c"util_mktmp_copy: lseek %s: %m\00"
@.str.1 = [30 x i8] c"util_mktmp_copy: close %s: %m\00"
define i32 @util_mktmp_copy(i8* %0, i32* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8** %2, i8*** %6
store i32 0, i32* %9
%10 = call i32 @util_mktmp(i32* %8, i8** %7)
store i32 %10, i32* %9
%11 = load i32, i32* %9
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
br label %66
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i32, i32* %8
%17 = call i32 @_copyfile(i8* %15, i32 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %14
%21 = load i32, i32* %8
%22 = call i64 @close(i32 %21)
%23 = load i8*, i8** %7
%24 = call i32 @unlink(i8* %23)
%25 = load i8*, i8** %7
%26 = call i32 @free(i8* %25)
br label %66
27: ; preds = %14
%28 = load i32*, i32** %5
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %47
30: ; preds = %27
%31 = load i32, i32* %8
%32 = load i32, i32* @SEEK_SET
%33 = call i64 @lseek(i32 %31, i64 0, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = call i32 @isp_dbgfail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %8
%39 = call i64 @close(i32 %38)
%40 = load i8*, i8** %7
%41 = call i32 @unlink(i8* %40)
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
br label %66
44: ; preds = %30
%45 = load i32, i32* %8
%46 = load i32*, i32** %5
store i32 %45, i32* %46
br label %59
47: ; preds = %27
%48 = load i32, i32* %8
%49 = call i64 @close(i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i8*, i8** %7
%53 = call i32 @isp_dbgfail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %52)
%54 = load i8*, i8** %7
%55 = call i32 @unlink(i8* %54)
%56 = load i8*, i8** %7
%57 = call i32 @free(i8* %56)
br label %66
58: ; preds = %47
br label %59
59: ; preds = %58, %44
%60 = load i8**, i8*** %6
%61 = icmp ne i8** %60, null
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i8*, i8** %7
%64 = load i8**, i8*** %6
store i8* %63, i8** %64
br label %65
65: ; preds = %62, %59
br label %66
66: ; preds = %65, %51, %35, %20, %13
%67 = load i32, i32* %9
ret i32 %67
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [30 x i8] c"util_mktmp_copy: lseek %s: %m\00"
@.str.1 = [30 x i8] c"util_mktmp_copy: close %s: %m\00"
define i32 @util_mktmp_copy(i8* %0, i32* writeonly %1, i8** writeonly %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast i8** %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @util_mktmp(i32* %5, i8** %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %53
10: ; preds = %3
%11 = load i32, i32* %5
%12 = call i32 @_copyfile(i8* %0, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %10
%15 = load i32, i32* %5
%16 = call i64 @close(i32 %15)
%17 = load i8*, i8** %4
%18 = call i32 @unlink(i8* %17)
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
br label %53
21: ; preds = %10
%22 = icmp eq i32* %1, null
%23 = load i32, i32* %5
br i1 %22, label %39, label %24
24: ; preds = %21
%25 = load i32, i32* @SEEK_SET
%26 = call i64 @lseek(i32 %23, i64 0, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i8*, i8** %4
%30 = call i32 @isp_dbgfail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %5
%32 = call i64 @close(i32 %31)
%33 = load i8*, i8** %4
%34 = call i32 @unlink(i8* %33)
%35 = load i8*, i8** %4
%36 = call i32 @free(i8* %35)
br label %53
37: ; preds = %24
%38 = load i32, i32* %5
store i32 %38, i32* %1
br label %49
39: ; preds = %21
%40 = call i64 @close(i32 %23)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %39
%43 = load i8*, i8** %4
%44 = call i32 @isp_dbgfail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = load i8*, i8** %4
%46 = call i32 @unlink(i8* %45)
%47 = load i8*, i8** %4
%48 = call i32 @free(i8* %47)
br label %53
49: ; preds = %39, %37
%50 = icmp eq i8** %2, null
br i1 %50, label %53, label %51
51: ; preds = %49
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %53
53: ; preds = %49, %51, %3, %42, %28, %14
%54 = phi i32 [ %8, %3 ], [ %12, %14 ], [ 0, %28 ], [ 0, %51 ], [ 0, %49 ], [ 0, %42 ]
ret i32 %54
}