#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char *, char *, int);
FILE *open_jp2file(const char filename[]) {
  FILE *fp;
  char *data;
  if (!(fp = fopen(filename, "a+b"))) {
    fprintf(stderr, "Original JP2 %s not found\n", filename);
    return NULL;
  }
  if (fseek(fp, 0, SEEK_SET) == -1) {
    fclose(fp);
    fprintf(stderr, "Original JP2 %s broken (fseek error)\n", filename);
    return NULL;
  }
  data = (char *)malloc(12);
  if (fread(data, 12, 1, fp) != 1) {
    free(data);
    fclose(fp);
    fprintf(stderr, "Original JP2 %s broken (read error)\n", filename);
    return NULL;
  }
  if (*data || *(data + 1) || *(data + 2) || *(data + 3) != 12 ||
      strncmp(data + 4, "jP  \r\n\x87\n", 8)) {
    free(data);
    fclose(fp);
    fprintf(stderr, "No JPEG 2000 Signature box in target %s\n", filename);
    return NULL;
  }
  free(data);
  return fp;
}


x86 assembly (clang, -O0):
<open_jp2file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d0 <open_jp2file+0x50>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe46(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1331 <open_jp2file+0x1b1>
mov    -0x18(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
jne    121c <open_jp2file+0x9c>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe15(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1331 <open_jp2file+0x1b1>
mov    $0xc,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0xc,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     1282 <open_jp2file+0x102>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd5(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1331 <open_jp2file+0x1b1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12e8 <open_jp2file+0x168>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    12e8 <open_jp2file+0x168>
mov    -0x20(%rbp),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    12e8 <open_jp2file+0x168>
mov    -0x20(%rbp),%rax
movsbl 0x3(%rax),%eax
cmp    $0xc,%eax
jne    12e8 <open_jp2file+0x168>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
lea    0xd96(%rip),%rsi
mov    $0x8,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     1320 <open_jp2file+0x1a0>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd65(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1331 <open_jp2file+0x1b1>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_jp2file>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe71(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1245 <open_jp2file+0xc5>
mov    %rax,%rbx
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
je     1257 <open_jp2file+0xd7>
mov    $0xc,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    1271 <open_jp2file+0xf1>
cmpb   $0x0,(%r15)
jne    1223 <open_jp2file+0xa3>
cmpb   $0x0,0x1(%r15)
jne    1223 <open_jp2file+0xa3>
cmpb   $0x0,0x2(%r15)
jne    1223 <open_jp2file+0xa3>
cmpb   $0xc,0x3(%r15)
jne    1223 <open_jp2file+0xa3>
mov    %r15,%rdi
add    $0x4,%rdi
lea    0xe5a(%rip),%rsi
mov    $0x8,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     12a4 <open_jp2file+0x124>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
jmp    1291 <open_jp2file+0x111>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
jmp    1291 <open_jp2file+0x111>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
jmp    1291 <open_jp2file+0x111>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1030 <free@plt>
jmp    129b <open_jp2file+0x11b>

LLVM IR (-O0):
@.str = [4 x i8] c"a+b\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Original JP2 %s not found\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [38 x i8] c"Original JP2 %s broken (fseek error)\0A\00"
@.str.3 = [37 x i8] c"Original JP2 %s broken (read error)\0A\00"
@.str.4 = [9 x i8] c"jP \0D\0A\87\0A\00"
@.str.5 = [41 x i8] c"No JPEG 2000 Signature box in target %s\0A\00"
define i32* @open_jp2file(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %3
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32* null, i32** %2
br label %79
13: ; preds = %1
%14 = load i32*, i32** %4
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %3
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %22)
store i32* null, i32** %2
br label %79
24: ; preds = %13
%25 = call i64 @malloc(i32 12)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = load i32*, i32** %4
%29 = call i32 @fread(i8* %27, i32 12, i32 1, i32* %28)
%30 = icmp ne i32 %29, 1
br i1 %30, label %31, label %39
31: ; preds = %24
%32 = load i8*, i8** %5
%33 = call i32 @free(i8* %32)
%34 = load i32*, i32** %4
%35 = call i32 @fclose(i32* %34)
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %3
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %37)
store i32* null, i32** %2
br label %79
39: ; preds = %24
%40 = load i8*, i8** %5
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 0
br i1 %43, label %67, label %44
44: ; preds = %39
%45 = load i8*, i8** %5
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 0
br i1 %49, label %67, label %50
50: ; preds = %44
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i64 2
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 0
br i1 %55, label %67, label %56
56: ; preds = %50
%57 = load i8*, i8** %5
%58 = getelementptr inbounds i8, i8* %57, i64 3
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp ne i32 %60, 12
br i1 %61, label %67, label %62
62: ; preds = %56
%63 = load i8*, i8** %5
%64 = getelementptr inbounds i8, i8* %63, i64 4
%65 = call i64 @strncmp(i8* %64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %62, %56, %50, %44, %39
%68 = load i8*, i8** %5
%69 = call i32 @free(i8* %68)
%70 = load i32*, i32** %4
%71 = call i32 @fclose(i32* %70)
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %3
%74 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %73)
store i32* null, i32** %2
br label %79
75: ; preds = %62
%76 = load i8*, i8** %5
%77 = call i32 @free(i8* %76)
%78 = load i32*, i32** %4
store i32* %78, i32** %2
br label %79
79: ; preds = %75, %67, %31, %18, %9
%80 = load i32*, i32** %2
ret i32* %80
}

LLVM IR (-O2):
@.str = [4 x i8] c"a+b\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Original JP2 %s not found\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [38 x i8] c"Original JP2 %s broken (fseek error)\0A\00"
@.str.3 = [37 x i8] c"Original JP2 %s broken (read error)\0A\00"
@.str.4 = [9 x i8] c"jP \0D\0A\87\0A\00"
@.str.5 = [41 x i8] c"No JPEG 2000 Signature box in target %s\0A\00"
define noalias i32* @open_jp2file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %51
7: ; preds = %1
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = tail call i32 @fclose(i32* %2)
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %51
15: ; preds = %7
%16 = tail call i64 @malloc(i32 12)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @fread(i8* %17, i32 12, i32 1, i32* %2)
%19 = icmp eq i32 %18, 1
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = tail call i32 @free(i8* %17)
%22 = tail call i32 @fclose(i32* %2)
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %51
25: ; preds = %15
%26 = load i8, i8* %17
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %25
%29 = getelementptr inbounds i8, i8* %17, i64 1
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %28
%33 = getelementptr inbounds i8, i8* %17, i64 2
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = getelementptr inbounds i8, i8* %17, i64 3
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 12
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = getelementptr inbounds i8, i8* %17, i64 4
%42 = tail call i64 @strncmp(i8* %41, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%43 = icmp eq i64 %42, 0
br i1 %43, label %49, label %44
44: ; preds = %40, %36, %32, %28, %25
%45 = tail call i32 @free(i8* %17)
%46 = tail call i32 @fclose(i32* %2)
%47 = load i32, i32* @stderr
%48 = tail call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %51
49: ; preds = %40
%50 = tail call i32 @free(i8* %17)
br label %51
51: ; preds = %49, %44, %20, %11, %4
%52 = phi i32* [ null, %11 ], [ null, %20 ], [ null, %44 ], [ %2, %49 ], [ null, %4 ]
ret i32* %52
}