#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 */
struct stat {
  size_t st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EEXIST;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int ffindex_index_open(char *data_filename, char *index_filename, char *mode,
                       FILE **out_data_file, FILE **out_index_file,
                       size_t *out_offset) {
  if (mode[0] == 'a') {
    *out_data_file = fopen(data_filename, "a");
    if (*out_data_file == NULL) {
      perror(data_filename);
      return EXIT_FAILURE;
    }
    *out_index_file = fopen(index_filename, "a+");
    if (*out_index_file == NULL) {
      perror(index_filename);
      return EXIT_FAILURE;
    }
    struct stat sb;
    fstat(fileno(*out_index_file), &sb);
    fseek(*out_index_file, sb.st_size, SEEK_SET);
    fstat(fileno(*out_data_file), &sb);
    fseek(*out_data_file, sb.st_size, SEEK_SET);
    *out_offset = sb.st_size;
  } else {
    struct stat st;
    if (stat(data_filename, &st) == 0) {
      errno = EEXIST;
      perror(data_filename);
      return EXIT_FAILURE;
    }
    *out_data_file = fopen(data_filename, "w");
    if (*out_data_file == NULL) {
      perror(data_filename);
      return EXIT_FAILURE;
    }
    if (stat(index_filename, &st) == 0) {
      errno = EEXIST;
      perror(index_filename);
      return EXIT_FAILURE;
    }
    *out_index_file = fopen(index_filename, "w+");
    if (*out_index_file == NULL) {
      perror(index_filename);
      return EXIT_FAILURE;
    }
    *out_offset = 0;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<ffindex_index_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
jne    1284 <ffindex_index_open+0x124>
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d2 <ffindex_index_open+0x72>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <ffindex_index_open+0x245>
mov    -0x18(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1214 <ffindex_index_open+0xb4>
mov    -0x18(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <ffindex_index_open+0x245>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x40(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x40(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
jmp    1399 <ffindex_index_open+0x239>
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
jne    12c7 <ffindex_index_open+0x167>
mov    0x2d3e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d2d(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <ffindex_index_open+0x245>
mov    -0x10(%rbp),%rdi
lea    0xd33(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1309 <ffindex_index_open+0x1a9>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <ffindex_index_open+0x245>
mov    -0x18(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
jne    134c <ffindex_index_open+0x1ec>
mov    0x2cb9(%rip),%rax
mov    (%rax),%ecx
mov    0x2ca8(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2c86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <ffindex_index_open+0x245>
mov    -0x18(%rbp),%rdi
lea    0xcb0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    138e <ffindex_index_open+0x22e>
mov    -0x18(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2c44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <ffindex_index_open+0x245>
mov    -0x38(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2c20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffindex_index_open>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
cmpb   $0x61,(%rdx)
jne    1216 <ffindex_index_open+0xb6>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     12a4 <ffindex_index_open+0x144>
lea    0xe5a(%rip),%rsi
mov    %r13,%rdi
call   1060 <fopen@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     128a <ffindex_index_open+0x12a>
mov    %rax,%rdi
call   1040 <fileno@plt>
lea    0x8(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
call   1080 <fstat@plt>
mov    (%r15),%rdi
mov    0x8(%rsp),%rsi
mov    0x2dee(%rip),%r15
mov    (%r15),%edx
call   1050 <fseek@plt>
mov    (%r12),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1080 <fstat@plt>
mov    (%r12),%rdi
mov    0x8(%rsp),%rbx
mov    (%r15),%edx
mov    %rbx,%rsi
call   1050 <fseek@plt>
mov    %rbx,(%r14)
jmp    1273 <ffindex_index_open+0x113>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <stat@plt>
test   %rax,%rax
je     1292 <ffindex_index_open+0x132>
lea    0xdd6(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     12a4 <ffindex_index_open+0x144>
lea    0x8(%rsp),%rsi
mov    %r13,%rdi
call   1030 <stat@plt>
test   %rax,%rax
je     128f <ffindex_index_open+0x12f>
lea    0xdae(%rip),%rsi
mov    %r13,%rdi
call   1060 <fopen@plt>
mov    %rax,(%r15)
mov    %r13,%rbx
test   %rax,%rax
je     12a4 <ffindex_index_open+0x144>
movq   $0x0,(%r14)
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r13,%rdi
jmp    12a7 <ffindex_index_open+0x147>
mov    %r13,%rbx
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    0x2d36(%rip),%rcx
mov    %eax,(%rcx)
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    0x2d15(%rip),%rax
jmp    127a <ffindex_index_open+0x11a>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [2 x i8] c"a\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"a+\00"
@SEEK_SET = global i32 0
@EEXIST = global i32 0
@errno = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"w+\00"
@EXIT_SUCCESS = global i32 0
define i32 @ffindex_index_open(i8* %0, i8* %1, i8* %2, i32** %3, i32** %4, i64* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32**
%12 = alloca i32**
%13 = alloca i64*
%14 = alloca %struct.stat
%15 = alloca %struct.stat
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32** %3, i32*** %11
store i32** %4, i32*** %12
store i64* %5, i64** %13
%16 = load i8*, i8** %10
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 97
br i1 %20, label %21, label %67
21: ; preds = %6
%22 = load i8*, i8** %8
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = load i32**, i32*** %11
store i32* %23, i32** %24
%25 = load i32**, i32*** %11
%26 = load i32*, i32** %25
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = load i8*, i8** %8
%30 = call i32 @perror(i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
store i32 %31, i32* %7
br label %111
32: ; preds = %21
%33 = load i8*, i8** %9
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32**, i32*** %12
store i32* %34, i32** %35
%36 = load i32**, i32*** %12
%37 = load i32*, i32** %36
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = load i8*, i8** %9
%41 = call i32 @perror(i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
store i32 %42, i32* %7
br label %111
43: ; preds = %32
%44 = load i32**, i32*** %12
%45 = load i32*, i32** %44
%46 = call i32 @fileno(i32* %45)
%47 = call i32 @fstat(i32 %46, %struct.stat* %14)
%48 = load i32**, i32*** %12
%49 = load i32*, i32** %48
%50 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%51 = load i64, i64* %50
%52 = load i32, i32* @SEEK_SET
%53 = call i32 @fseek(i32* %49, i64 %51, i32 %52)
%54 = load i32**, i32*** %11
%55 = load i32*, i32** %54
%56 = call i32 @fileno(i32* %55)
%57 = call i32 @fstat(i32 %56, %struct.stat* %14)
%58 = load i32**, i32*** %11
%59 = load i32*, i32** %58
%60 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%61 = load i64, i64* %60
%62 = load i32, i32* @SEEK_SET
%63 = call i32 @fseek(i32* %59, i64 %61, i32 %62)
%64 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%65 = load i64, i64* %64
%66 = load i64*, i64** %13
store i64 %65, i64* %66
br label %109
67: ; preds = %6
%68 = load i8*, i8** %8
%69 = call i64 @stat(i8* %68, %struct.stat* %15)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %67
%72 = load i32, i32* @EEXIST
store i32 %72, i32* @errno
%73 = load i8*, i8** %8
%74 = call i32 @perror(i8* %73)
%75 = load i32, i32* @EXIT_FAILURE
store i32 %75, i32* %7
br label %111
76: ; preds = %67
%77 = load i8*, i8** %8
%78 = call i32* @fopen(i8* %77, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%79 = load i32**, i32*** %11
store i32* %78, i32** %79
%80 = load i32**, i32*** %11
%81 = load i32*, i32** %80
%82 = icmp eq i32* %81, null
br i1 %82, label %83, label %87
83: ; preds = %76
%84 = load i8*, i8** %8
%85 = call i32 @perror(i8* %84)
%86 = load i32, i32* @EXIT_FAILURE
store i32 %86, i32* %7
br label %111
87: ; preds = %76
%88 = load i8*, i8** %9
%89 = call i64 @stat(i8* %88, %struct.stat* %15)
%90 = icmp eq i64 %89, 0
br i1 %90, label %91, label %96
91: ; preds = %87
%92 = load i32, i32* @EEXIST
store i32 %92, i32* @errno
%93 = load i8*, i8** %9
%94 = call i32 @perror(i8* %93)
%95 = load i32, i32* @EXIT_FAILURE
store i32 %95, i32* %7
br label %111
96: ; preds = %87
%97 = load i8*, i8** %9
%98 = call i32* @fopen(i8* %97, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%99 = load i32**, i32*** %12
store i32* %98, i32** %99
%100 = load i32**, i32*** %12
%101 = load i32*, i32** %100
%102 = icmp eq i32* %101, null
br i1 %102, label %103, label %107
103: ; preds = %96
%104 = load i8*, i8** %9
%105 = call i32 @perror(i8* %104)
%106 = load i32, i32* @EXIT_FAILURE
store i32 %106, i32* %7
br label %111
107: ; preds = %96
%108 = load i64*, i64** %13
store i64 0, i64* %108
br label %109
109: ; preds = %107, %43
%110 = load i32, i32* @EXIT_SUCCESS
store i32 %110, i32* %7
br label %111
111: ; preds = %109, %103, %91, %83, %71, %39, %28
%112 = load i32, i32* %7
ret i32 %112
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [2 x i8] c"a\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"a+\00"
@SEEK_SET = global i32 0
@EEXIST = global i32 0
@errno = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"w+\00"
@EXIT_SUCCESS = global i32 0
define i32 @ffindex_index_open(i8* %0, i8* %1, i8* nocapture %2, i32** nocapture %3, i32** nocapture %4, i64* nocapture writeonly %5) {
%7 = alloca %struct.stat
%8 = alloca %struct.stat
%9 = load i8, i8* %2
%10 = icmp eq i8 %9, 97
br i1 %10, label %11, label %39
11: ; preds = %6
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %3
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @perror(i8* %0)
%16 = load i32, i32* @EXIT_FAILURE
br label %62
17: ; preds = %11
%18 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %4
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @perror(i8* %1)
%22 = load i32, i32* @EXIT_FAILURE
br label %62
23: ; preds = %17
%24 = bitcast %struct.stat* %7 to i8*
%25 = tail call i32 @fileno(i32* %18)
%26 = call i32 @fstat(i32 %25, %struct.stat* %7)
%27 = load i32*, i32** %4
%28 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
%29 = load i64, i64* %28
%30 = load i32, i32* @SEEK_SET
%31 = tail call i32 @fseek(i32* %27, i64 %29, i32 %30)
%32 = load i32*, i32** %3
%33 = tail call i32 @fileno(i32* %32)
%34 = call i32 @fstat(i32 %33, %struct.stat* %7)
%35 = load i32*, i32** %3
%36 = load i64, i64* %28
%37 = load i32, i32* @SEEK_SET
%38 = tail call i32 @fseek(i32* %35, i64 %36, i32 %37)
store i64 %36, i64* %5
br label %60
39: ; preds = %6
%40 = bitcast %struct.stat* %8 to i8*
%41 = call i64 @stat(i8* %0, %struct.stat* %8)
%42 = icmp eq i64 %41, 0
br i1 %42, label %52, label %43
43: ; preds = %39
%44 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %44, i32** %3
%45 = icmp eq i32* %44, null
br i1 %45, label %55, label %46
46: ; preds = %43
%47 = call i64 @stat(i8* %1, %struct.stat* %8)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %46
%50 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %50, i32** %4
%51 = icmp eq i32* %50, null
br i1 %51, label %55, label %59
52: ; preds = %46, %39
%53 = phi i8* [ %0, %39 ], [ %1, %46 ]
%54 = load i32, i32* @EEXIST
store i32 %54, i32* @errno
br label %55
55: ; preds = %52, %49, %43
%56 = phi i8* [ %0, %43 ], [ %1, %49 ], [ %53, %52 ]
%57 = call i32 @perror(i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
br label %62
59: ; preds = %49
store i64 0, i64* %5
br label %60
60: ; preds = %59, %23
%61 = load i32, i32* @EXIT_SUCCESS
br label %62
62: ; preds = %55, %60, %20, %14
%63 = phi i32 [ %16, %14 ], [ %22, %20 ], [ %61, %60 ], [ %58, %55 ]
ret i32 %63
}