#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*/ copyfile(int /*<<< orphan*/ *, int /*<<< orphan*/ *, long);
int /*<<< orphan*/ copyfilepos(int /*<<< orphan*/ *, int /*<<< orphan*/ *, long,
                               long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ savetempbak(char *, char *);
int /*<<< orphan*/ setoutdir(char *);
char *tempfile(int);
int write_error(int /*<<< orphan*/ *);
int merge_key_to_ringfile(char *keyfile, char *ringfile, long fp,
                          int packetlength, long keylen) {
  FILE *f, *g, *h;
  char *tempf;
  int rc;
  setoutdir(ringfile);
  tempf = tempfile(1);
  if ((f = fopen(ringfile, "rb")) == NULL)
    return -1;
  if ((g = fopen(tempf, "wb")) == NULL) {
    fclose(f);
    return -1;
  }
  if ((h = fopen(keyfile, "rb")) == NULL) {
    fclose(f);
    fclose(g);
    return -1;
  }
  copyfile(f, g, fp);
  copyfile(h, g, keylen);
  copyfilepos(f, g, -1L, fp + packetlength);
  fclose(f);
  rc = write_error(g);
  fclose(g);
  fclose(h);
  if (!rc)
    savetempbak(tempf, ringfile);
  return rc ? -1 : 0;
}


x86 assembly (clang, -O0):
<merge_key_to_ringfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <setoutdir@plt>
mov    $0x1,%edi
call   1030 <tempfile@plt>
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11dc <merge_key_to_ringfile+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dd <merge_key_to_ringfile+0x15d>
mov    -0x50(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    120f <merge_key_to_ringfile+0x8f>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dd <merge_key_to_ringfile+0x15d>
mov    -0x10(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    124b <merge_key_to_ringfile+0xcb>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dd <merge_key_to_ringfile+0x15d>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <copyfile@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10a0 <copyfile@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rax
add    %rax,%rcx
mov    $0xffffffffffffffff,%rdx
call   1040 <copyfilepos@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1060 <write_error@plt>
mov    %eax,-0x54(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x54(%rbp)
jne    12ca <merge_key_to_ringfile+0x14a>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <savetempbak@plt>
mov    -0x54(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_key_to_ringfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,0x20(%rsp)
mov    %ecx,0xc(%rsp)
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1090 <setoutdir@plt>
mov    $0x1,%edi
call   1030 <tempfile@plt>
mov    %rax,%rbx
lea    0xe44(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
je     129e <merge_key_to_ringfile+0x11e>
mov    %rax,%rbp
mov    %r12,0x18(%rsp)
lea    0xe21(%rip),%rsi
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1289 <merge_key_to_ringfile+0x109>
mov    %rax,%r12
lea    0xdfe(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     128e <merge_key_to_ringfile+0x10e>
mov    %rax,%r14
mov    %rbp,%rdi
mov    %r12,%rsi
mov    0x18(%rsp),%rbx
mov    %rbx,%rdx
call   10a0 <copyfile@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    0x20(%rsp),%rdx
call   10a0 <copyfile@plt>
movslq 0xc(%rsp),%rcx
add    %rbx,%rcx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0xffffffffffffffff,%rdx
call   1040 <copyfilepos@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1060 <write_error@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %ebp,%ebp
jne    1282 <merge_key_to_ringfile+0x102>
mov    0x10(%rsp),%rdi
mov    %r15,%rsi
call   1080 <savetempbak@plt>
neg    %ebp
sbb    %r13d,%r13d
jmp    129e <merge_key_to_ringfile+0x11e>
mov    %rbp,%rdi
jmp    1299 <merge_key_to_ringfile+0x119>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @merge_key_to_ringfile(i8* %0, i8* %1, i64 %2, i32 %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i8*
%16 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i64 %4, i64* %11
%17 = load i8*, i8** %8
%18 = call i32 @setoutdir(i8* %17)
%19 = call i8* @tempfile(i32 1)
store i8* %19, i8** %15
%20 = load i8*, i8** %8
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %12
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %5
store i32 -1, i32* %6
br label %75
24: ; preds = %5
%25 = load i8*, i8** %15
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %13
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32*, i32** %12
%30 = call i32 @fclose(i32* %29)
store i32 -1, i32* %6
br label %75
31: ; preds = %24
%32 = load i8*, i8** %7
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %33, i32** %14
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i32*, i32** %12
%37 = call i32 @fclose(i32* %36)
%38 = load i32*, i32** %13
%39 = call i32 @fclose(i32* %38)
store i32 -1, i32* %6
br label %75
40: ; preds = %31
%41 = load i32*, i32** %12
%42 = load i32*, i32** %13
%43 = load i64, i64* %9
%44 = call i32 @copyfile(i32* %41, i32* %42, i64 %43)
%45 = load i32*, i32** %14
%46 = load i32*, i32** %13
%47 = load i64, i64* %11
%48 = call i32 @copyfile(i32* %45, i32* %46, i64 %47)
%49 = load i32*, i32** %12
%50 = load i32*, i32** %13
%51 = load i64, i64* %9
%52 = load i32, i32* %10
%53 = sext i32 %52 to i64
%54 = add nsw i64 %51, %53
%55 = call i32 @copyfilepos(i32* %49, i32* %50, i64 -1, i64 %54)
%56 = load i32*, i32** %12
%57 = call i32 @fclose(i32* %56)
%58 = load i32*, i32** %13
%59 = call i32 @write_error(i32* %58)
store i32 %59, i32* %16
%60 = load i32*, i32** %13
%61 = call i32 @fclose(i32* %60)
%62 = load i32*, i32** %14
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %16
%65 = icmp ne i32 %64, 0
br i1 %65, label %70, label %66
66: ; preds = %40
%67 = load i8*, i8** %15
%68 = load i8*, i8** %8
%69 = call i32 @savetempbak(i8* %67, i8* %68)
br label %70
70: ; preds = %66, %40
%71 = load i32, i32* %16
%72 = icmp ne i32 %71, 0
%73 = zext i1 %72 to i64
%74 = select i1 %72, i32 -1, i32 0
store i32 %74, i32* %6
br label %75
75: ; preds = %70, %35, %28, %23
%76 = load i32, i32* %6
ret i32 %76
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @merge_key_to_ringfile(i8* nocapture %0, i8* %1, i64 %2, i32 %3, i64 %4) {
%6 = tail call i32 @setoutdir(i8* %1)
%7 = tail call i8* @tempfile(i32 1)
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %37, label %10
10: ; preds = %5
%11 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @fclose(i32* %8)
br label %37
15: ; preds = %10
%16 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @fclose(i32* %8)
%20 = tail call i32 @fclose(i32* %11)
br label %37
21: ; preds = %15
%22 = tail call i32 @copyfile(i32* %8, i32* %11, i64 %2)
%23 = tail call i32 @copyfile(i32* %16, i32* %11, i64 %4)
%24 = sext i32 %3 to i64
%25 = add nsw i64 %24, %2
%26 = tail call i32 @copyfilepos(i32* %8, i32* %11, i64 -1, i64 %25)
%27 = tail call i32 @fclose(i32* %8)
%28 = tail call i32 @write_error(i32* %11)
%29 = tail call i32 @fclose(i32* %11)
%30 = tail call i32 @fclose(i32* %16)
%31 = icmp eq i32 %28, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = tail call i32 @savetempbak(i8* %7, i8* %1)
br label %34
34: ; preds = %32, %21
%35 = xor i1 %31, true
%36 = sext i1 %35 to i32
br label %37
37: ; preds = %5, %34, %18, %13
%38 = phi i32 [ -1, %13 ], [ -1, %18 ], [ %36, %34 ], [ -1, %5 ]
ret i32 %38
}