#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputc(long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_jp2file(char *);
char *read_xmlfile(char *, long *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "USAGE: ./addXMLinJP2 modifing.jp2 adding.xml\n");
    return -1;
  }
  FILE *fp;
  char *xmldata, type[] = "xml ";
  long fsize, boxsize;
  fp = open_jp2file(argv[1]);
  if (!fp)
    return -1;
  xmldata = read_xmlfile(argv[2], &fsize);
  boxsize = fsize + 8;
  fputc((boxsize >> 24) & 0xff, fp);
  fputc((boxsize >> 16) & 0xff, fp);
  fputc((boxsize >> 8) & 0xff, fp);
  fputc(boxsize & 0xff, fp);
  fwrite(type, 4, 1, fp);
  fwrite(xmldata, fsize, 1, fp);
  free(xmldata);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11b1 <main+0x41>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <main+0x13e>
mov    0xe77(%rip),%eax
mov    %eax,-0x25(%rbp)
mov    0xe72(%rip),%al
mov    %al,-0x21(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <open_jp2file@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11eb <main+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <main+0x13e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1030 <read_xmlfile@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
sar    $0x18,%rdi
and    $0xff,%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x38(%rbp),%rdi
sar    $0x10,%rdi
and    $0xff,%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x38(%rbp),%rdi
sar    $0x8,%rdi
and    $0xff,%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x38(%rbp),%rdi
and    $0xff,%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputc@plt>
lea    -0x25(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jg     1198 <main+0x28>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1247 <main+0xd7>
mov    %rsi,%rbx
movb   $0x0,0x4(%rsp)
movl   $0x206c6d78,(%rsp)
mov    0x8(%rsi),%rdi
call   1080 <open_jp2file@plt>
test   %rax,%rax
je     1247 <main+0xd7>
mov    %rax,%r15
mov    0x10(%rbx),%rdi
lea    0x8(%rsp),%rsi
call   1030 <read_xmlfile@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rbx
add    $0x8,%rbx
mov    %ebx,%edi
shr    $0x18,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
movzbl %bh,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
movzbl %bl,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
mov    %rsp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    124c <main+0xdc>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [46 x i8] c"USAGE: ./addXMLinJP2 modifing.jp2 adding.xml\0A\00"
@__const.main.type = [5 x i8] c"xml \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [5 x i8]
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
16: ; preds = %2
%17 = bitcast [5 x i8]* %8 to i8*
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @open_jp2file(i8* %20)
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %16
store i32 -1, i32* %3
br label %62
25: ; preds = %16
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i8* @read_xmlfile(i8* %28, i64* %9)
store i8* %29, i8** %7
%30 = load i64, i64* %9
%31 = add nsw i64 %30, 8
store i64 %31, i64* %10
%32 = load i64, i64* %10
%33 = ashr i64 %32, 24
%34 = and i64 %33, 255
%35 = load i32*, i32** %6
%36 = call i32 @fputc(i64 %34, i32* %35)
%37 = load i64, i64* %10
%38 = ashr i64 %37, 16
%39 = and i64 %38, 255
%40 = load i32*, i32** %6
%41 = call i32 @fputc(i64 %39, i32* %40)
%42 = load i64, i64* %10
%43 = ashr i64 %42, 8
%44 = and i64 %43, 255
%45 = load i32*, i32** %6
%46 = call i32 @fputc(i64 %44, i32* %45)
%47 = load i64, i64* %10
%48 = and i64 %47, 255
%49 = load i32*, i32** %6
%50 = call i32 @fputc(i64 %48, i32* %49)
%51 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%52 = load i32*, i32** %6
%53 = call i32 @fwrite(i8* %51, i64 4, i32 1, i32* %52)
%54 = load i8*, i8** %7
%55 = load i64, i64* %9
%56 = load i32*, i32** %6
%57 = call i32 @fwrite(i8* %54, i64 %55, i32 1, i32* %56)
%58 = load i8*, i8** %7
%59 = call i32 @free(i8* %58)
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 0, i32* %3
br label %62
62: ; preds = %25, %24, %13
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"USAGE: ./addXMLinJP2 modifing.jp2 adding.xml\0A\00"
@__const.main.type = [5 x i8] c"xml \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [5 x i8]
%4 = alloca i64
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %40
9: ; preds = %2
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%11 = bitcast i64* %4 to i8*
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @open_jp2file(i8* %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %38, label %16
16: ; preds = %9
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = call i8* @read_xmlfile(i8* %18, i64* %4)
%20 = load i64, i64* %4
%21 = add nsw i64 %20, 8
%22 = lshr i64 %21, 24
%23 = and i64 %22, 255
%24 = call i32 @fputc(i64 %23, i32* %14)
%25 = lshr i64 %21, 16
%26 = and i64 %25, 255
%27 = call i32 @fputc(i64 %26, i32* %14)
%28 = lshr i64 %21, 8
%29 = and i64 %28, 255
%30 = call i32 @fputc(i64 %29, i32* %14)
%31 = and i64 %21, 255
%32 = call i32 @fputc(i64 %31, i32* %14)
%33 = call i32 @fwrite(i8* %10, i64 4, i32 1, i32* %14)
%34 = load i64, i64* %4
%35 = call i32 @fwrite(i8* %19, i64 %34, i32 1, i32* %14)
%36 = call i32 @free(i8* %19)
%37 = call i32 @fclose(i32* %14)
br label %38
38: ; preds = %9, %16
%39 = phi i32 [ 0, %16 ], [ -1, %9 ]
br label %40
40: ; preds = %38, %6
%41 = phi i32 [ -1, %6 ], [ %39, %38 ]
ret i32 %41
}