#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_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ cio_seek(int);
int /*<<< orphan*/ cio_skip(int);
int cio_tell();
int /*<<< orphan*/ cio_write(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int jp2_write_jp2c(char *J2K_file) {
  int len, lenp, totlen, i;
  FILE *src;
  char *j2kfile;
  lenp = cio_tell();
  cio_skip(4);
  cio_write(0x6a703263, 4);
  src = fopen(J2K_file, "rb");
  fseek(src, 0, SEEK_END);
  totlen = ftell(src);
  fseek(src, 0, SEEK_SET);
  j2kfile = (char *)malloc(totlen);
  fread(j2kfile, 1, totlen, src);
  fclose(src);
  for (i = 0; i < totlen; i++)
    cio_write(j2kfile[i], 1);
  len = cio_tell() - lenp;
  cio_seek(lenp);
  cio_write(len, 4);
  cio_seek(lenp + len);
  return lenp;
}


x86 assembly (clang, -O0):
<jp2_write_jp2c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10b0 <cio_tell@plt>
mov    %eax,-0x10(%rbp)
mov    $0x4,%edi
call   1030 <cio_skip@plt>
mov    $0x6a703263,%edi
mov    $0x4,%esi
call   10a0 <cio_write@plt>
mov    -0x8(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1278 <jp2_write_jp2c+0xd8>
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
mov    $0x1,%esi
call   10a0 <cio_write@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1248 <jp2_write_jp2c+0xa8>
mov    $0x0,%al
call   10b0 <cio_tell@plt>
sub    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <cio_seek@plt>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   10a0 <cio_write@plt>
mov    -0x10(%rbp),%edi
add    -0xc(%rbp),%edi
call   1060 <cio_seek@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jp2_write_jp2c>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   10b0 <cio_tell@plt>
mov    %eax,%r14d
mov    $0x4,%edi
call   1030 <cio_skip@plt>
mov    $0x6a703263,%edi
mov    $0x4,%esi
call   10a0 <cio_write@plt>
lea    0xe2b(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbp
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbp,%rdi
call   1070 <ftell@plt>
mov    %eax,%r15d
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15d,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
test   %r15d,%r15d
jle    1268 <jp2_write_jp2c+0xc8>
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%r12,%rbx,1),%edi
mov    $0x1,%esi
call   10a0 <cio_write@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1250 <jp2_write_jp2c+0xb0>
xor    %eax,%eax
call   10b0 <cio_tell@plt>
mov    %eax,%ebx
mov    %eax,%ebp
sub    %r14d,%ebp
mov    %r14d,%edi
call   1060 <cio_seek@plt>
mov    %ebp,%edi
mov    $0x4,%esi
call   10a0 <cio_write@plt>
mov    %ebx,%edi
call   1060 <cio_seek@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @jp2_write_jp2c(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %2
%9 = call i32 (...) @cio_tell()
store i32 %9, i32* %4
%10 = call i32 @cio_skip(i32 4)
%11 = call i32 @cio_write(i32 1785737827, i32 4)
%12 = load i8*, i8** %2
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = load i32*, i32** %7
%18 = call i32 @ftell(i32* %17)
store i32 %18, i32* %5
%19 = load i32*, i32** %7
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32, i32* %5
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = load i32, i32* %5
%27 = load i32*, i32** %7
%28 = call i32 @fread(i8* %25, i32 1, i32 %26, i32* %27)
%29 = load i32*, i32** %7
%30 = call i32 @fclose(i32* %29)
store i32 0, i32* %6
br label %31
31: ; preds = %43, %1
%32 = load i32, i32* %6
%33 = load i32, i32* %5
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %46
35: ; preds = %31
%36 = load i8*, i8** %8
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = call i32 @cio_write(i32 %41, i32 1)
br label %43
43: ; preds = %35
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %31
46: ; preds = %31
%47 = call i32 (...) @cio_tell()
%48 = load i32, i32* %4
%49 = sub nsw i32 %47, %48
store i32 %49, i32* %3
%50 = load i32, i32* %4
%51 = call i32 @cio_seek(i32 %50)
%52 = load i32, i32* %3
%53 = call i32 @cio_write(i32 %52, i32 4)
%54 = load i32, i32* %4
%55 = load i32, i32* %3
%56 = add nsw i32 %54, %55
%57 = call i32 @cio_seek(i32 %56)
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @jp2_write_jp2c(i8* nocapture %0) {
%2 = tail call i32 (...) @cio_tell()
%3 = tail call i32 @cio_skip(i32 4)
%4 = tail call i32 @cio_write(i32 1785737827, i32 4)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %5, i32 0, i32 %6)
%8 = tail call i32 @ftell(i32* %5)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %5, i32 0, i32 %9)
%11 = tail call i64 @malloc(i32 %8)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @fread(i8* %12, i32 1, i32 %8, i32* %5)
%14 = tail call i32 @fclose(i32* %5)
%15 = icmp sgt i32 %8, 0
br i1 %15, label %16, label %26
16: ; preds = %1
%17 = zext i32 %8 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %24, %18 ]
%20 = getelementptr inbounds i8, i8* %12, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = tail call i32 @cio_write(i32 %22, i32 1)
%24 = add nuw nsw i64 %19, 1
%25 = icmp eq i64 %24, %17
br i1 %25, label %26, label %18
26: ; preds = %18, %1
%27 = tail call i32 (...) @cio_tell()
%28 = sub nsw i32 %27, %2
%29 = tail call i32 @cio_seek(i32 %2)
%30 = tail call i32 @cio_write(i32 %28, i32 4)
%31 = tail call i32 @cio_seek(i32 %27)
ret i32 %2
}