#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int mp3edit_isvalid_index(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
char *mpgedit_index_build_filename(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
FILE *mpgedit_indexfile_init(FILE *fp, char *mpeg_filename,
                             char **rindx_filename) {
  FILE *indxfp = NULL;
  int sts;
  char *indx_filename;
  if (!fp || !mpeg_filename) {
    return NULL;
  }
  indx_filename = mpgedit_index_build_filename(mpeg_filename);
  if (!indx_filename) {
    return NULL;
  }
  indxfp = fopen(indx_filename, "r+b");
  if (!indxfp) {
    goto clean_exit;
  }
  sts = mp3edit_isvalid_index(fp, indxfp);
  if (sts != 1) {
    fclose(indxfp);
    indxfp = NULL;
    remove(indx_filename);
    goto clean_exit;
  }
  rewind(indxfp);
clean_exit:
  if (rindx_filename) {
    *rindx_filename = indx_filename;
  } else {
    free(indx_filename);
  }
  return indxfp;
}


x86 assembly (clang, -O0):
<mpgedit_indexfile_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a2 <mpgedit_indexfile_init+0x32>
cmpq   $0x0,-0x18(%rbp)
jne    11af <mpgedit_indexfile_init+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    1266 <mpgedit_indexfile_init+0xf6>
mov    -0x18(%rbp),%rdi
call   1040 <mpgedit_index_build_filename@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d4 <mpgedit_indexfile_init+0x64>
movq   $0x0,-0x8(%rbp)
jmp    1266 <mpgedit_indexfile_init+0xf6>
mov    -0x38(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <mpgedit_indexfile_init+0x88>
jmp    123a <mpgedit_indexfile_init+0xca>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <mp3edit_isvalid_index@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
je     1231 <mpgedit_indexfile_init+0xc1>
mov    -0x28(%rbp),%rdi
call   1070 <fclose@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <remove@plt>
jmp    123a <mpgedit_indexfile_init+0xca>
mov    -0x28(%rbp),%rdi
call   1080 <rewind@plt>
cmpq   $0x0,-0x20(%rbp)
je     1255 <mpgedit_indexfile_init+0xe5>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    125e <mpgedit_indexfile_init+0xee>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpgedit_indexfile_init>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     11f6 <mpgedit_indexfile_init+0x86>
test   %rsi,%rsi
je     11f6 <mpgedit_indexfile_init+0x86>
mov    %rdx,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <mpgedit_index_build_filename@plt>
test   %rax,%rax
je     11d8 <mpgedit_indexfile_init+0x68>
mov    %rax,%r15
lea    0xe5f(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ec <mpgedit_indexfile_init+0x7c>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <mp3edit_isvalid_index@plt>
cmp    $0x1,%eax
jne    11dc <mpgedit_indexfile_init+0x6c>
mov    %rbx,%rdi
call   1080 <rewind@plt>
test   %r14,%r14
jne    11f3 <mpgedit_indexfile_init+0x83>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11f6 <mpgedit_indexfile_init+0x86>
xor    %ebx,%ebx
jmp    11f6 <mpgedit_indexfile_init+0x86>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    %r15,%rdi
call   1060 <remove@plt>
xor    %ebx,%ebx
test   %r14,%r14
je     11ce <mpgedit_indexfile_init+0x5e>
mov    %r15,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
define i32* @mpgedit_indexfile_init(i32* %0, i8* %1, i8** %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i32* null, i32** %8
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %13, %3
store i32* null, i32** %4
br label %54
17: ; preds = %13
%18 = load i8*, i8** %6
%19 = call i8* @mpgedit_index_build_filename(i8* %18)
store i8* %19, i8** %10
%20 = load i8*, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %17
store i32* null, i32** %4
br label %54
23: ; preds = %17
%24 = load i8*, i8** %10
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp ne i32* %26, null
br i1 %27, label %29, label %28
28: ; preds = %23
br label %43
29: ; preds = %23
%30 = load i32*, i32** %5
%31 = load i32*, i32** %8
%32 = call i32 @mp3edit_isvalid_index(i32* %30, i32* %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32*, i32** %8
%37 = call i32 @fclose(i32* %36)
store i32* null, i32** %8
%38 = load i8*, i8** %10
%39 = call i32 @remove(i8* %38)
br label %43
40: ; preds = %29
%41 = load i32*, i32** %8
%42 = call i32 @rewind(i32* %41)
br label %43
43: ; preds = %40, %35, %28
%44 = load i8**, i8*** %7
%45 = icmp ne i8** %44, null
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i8*, i8** %10
%48 = load i8**, i8*** %7
store i8* %47, i8** %48
br label %52
49: ; preds = %43
%50 = load i8*, i8** %10
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %49, %46
%53 = load i32*, i32** %8
store i32* %53, i32** %4
br label %54
54: ; preds = %52, %22, %16
%55 = load i32*, i32** %4
ret i32* %55
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
define i32* @mpgedit_indexfile_init(i32* %0, i8* %1, i8** writeonly %2) {
%4 = icmp ne i32* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
br i1 %6, label %7, label %27
7: ; preds = %3
%8 = tail call i8* @mpgedit_index_build_filename(i8* %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %27, label %10
10: ; preds = %7
%11 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = tail call i32 @mp3edit_isvalid_index(i32* %0, i32* %11)
%15 = icmp eq i32 %14, 1
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @fclose(i32* %11)
%18 = tail call i32 @remove(i8* %8)
br label %21
19: ; preds = %13
%20 = tail call i32 @rewind(i32* %11)
br label %21
21: ; preds = %10, %19, %16
%22 = phi i32* [ null, %16 ], [ %11, %19 ], [ null, %10 ]
%23 = icmp eq i8** %2, null
br i1 %23, label %25, label %24
24: ; preds = %21
store i8* %8, i8** %2
br label %27
25: ; preds = %21
%26 = tail call i32 @free(i8* %8)
br label %27
27: ; preds = %24, %25, %7, %3
%28 = phi i32* [ null, %3 ], [ null, %7 ], [ %22, %25 ], [ %22, %24 ]
ret i32* %28
}