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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ copy_file(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char *,
                             size_t);
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
scalar_t__ lseek(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void copy_tmpfile(FILE *tmpfile, FILE *outfile, char *buf, size_t size) {
  if (lseek(fileno(tmpfile), 0, SEEK_SET)) {
    perror("could to seek to start of file");
    fclose(tmpfile);
    exit(1);
  }
  copy_file(tmpfile, outfile, buf, size);
  if (fclose(tmpfile) != 0) {
    perror("read temporary file");
    exit(1);
  }
  if (fclose(outfile) != 0) {
    perror("error writing buffer to output file");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<copy_tmpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0x0,%rax
je     11bc <copy_tmpfile+0x5c>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <copy_file@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     11fa <copy_tmpfile+0x9a>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1223 <copy_tmpfile+0xc3>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_tmpfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <fileno@plt>
mov    0x2e60(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jne    11c7 <copy_tmpfile+0x67>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1030 <copy_file@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    11e5 <copy_tmpfile+0x85>
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    11ee <copy_tmpfile+0x8e>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe32(%rip),%rdi
call   1070 <perror@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe33(%rip),%rdi
jmp    11f5 <copy_tmpfile+0x95>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [31 x i8] c"could to seek to start of file\00"
@.str.1 = [20 x i8] c"read temporary file\00"
@.str.2 = [36 x i8] c"error writing buffer to output file\00"
define void @copy_tmpfile(i32* %0, i32* %1, i8* %2, i64 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%9 = load i32*, i32** %5
%10 = call i32 @fileno(i32* %9)
%11 = load i32, i32* @SEEK_SET
%12 = call i64 @lseek(i32 %10, i32 0, i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%16 = load i32*, i32** %5
%17 = call i64 @fclose(i32* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %4
%20 = load i32*, i32** %5
%21 = load i32*, i32** %6
%22 = load i8*, i8** %7
%23 = load i64, i64* %8
%24 = call i32 @copy_file(i32* %20, i32* %21, i8* %22, i64 %23)
%25 = load i32*, i32** %5
%26 = call i64 @fclose(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = load i32*, i32** %6
%33 = call i64 @fclose(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [31 x i8] c"could to seek to start of file\00"
@.str.1 = [20 x i8] c"read temporary file\00"
@.str.2 = [36 x i8] c"error writing buffer to output file\00"
define void @copy_tmpfile(i32* %0, i32* %1, i8* %2, i64 %3) {
%5 = tail call i32 @fileno(i32* %0)
%6 = load i32, i32* @SEEK_SET
%7 = tail call i64 @lseek(i32 %5, i32 0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%11 = tail call i64 @fclose(i32* %0)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %4
%14 = tail call i32 @copy_file(i32* %0, i32* %1, i8* %2, i64 %3)
%15 = tail call i64 @fclose(i32* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = tail call i64 @fclose(i32* %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %20
ret void
}