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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int aac_drilltoatom(int /*<<< orphan*/ *, char *, int *);
int da_aac_insert_covr_atom(unsigned int, int, int /*<<< orphan*/ *, int, int,
                            int);
int da_aac_rewrite_stco_atom(unsigned int, int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ err_log(int, int, char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fcopyblock(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ *fdopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ r_close(int);
int /*<<< orphan*/ r_write(int, unsigned char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int scan_aac_findatom(int /*<<< orphan*/ *, int, char *, int *);
off_t da_aac_attach_image(int img_fd, int out_fd, int aac_fd, int offset) {
  off_t img_size;
  int atom_length;
  unsigned int extra_size;
  off_t file_size;
  unsigned char buffer[4];
  struct stat sb;
  FILE *aac_fp;
  off_t stco_atom_pos;
  off_t ilst_atom_pos;
  off_t last_pos;
  fstat(img_fd, &sb);
  img_size = sb.st_size;
  { err_log(5, 0x00000100, "Image size (in bytes): %ld.\n", img_size); };
  if (img_size < 1) {
    r_close(img_fd);
    return 0;
  }
  extra_size = img_size + 24;
  fstat(aac_fd, &sb);
  file_size = sb.st_size;
  aac_fp = fdopen(dup(aac_fd), "r");
  stco_atom_pos =
      aac_drilltoatom(aac_fp, "moov:trak:mdia:minf:stbl:stco", &atom_length);
  ilst_atom_pos = aac_drilltoatom(aac_fp, "moov:udta:meta:ilst", &atom_length);
  last_pos = aac_drilltoatom(aac_fp, "mdat", &atom_length);
  if (last_pos != -1) {
    if (offset >= last_pos) {
      return 0;
    }
  } else {
    { err_log(1, 0x00000100, "No 'mdat' atom.\n"); };
    return 0;
  }
  rewind(aac_fp);
  last_pos = scan_aac_findatom(aac_fp, file_size, "moov", &atom_length);
  if (last_pos != -1) {
    rewind(aac_fp);
    fcopyblock(aac_fp, out_fd, last_pos);
    atom_length += extra_size;
    buffer[3] = atom_length & 0xFF;
    buffer[2] = (atom_length >> 8) & 0xFF;
    buffer[1] = (atom_length >> 16) & 0xFF;
    buffer[0] = (atom_length >> 24) & 0xFF;
    r_write(out_fd, buffer, 4);
    last_pos += 4;
  } else {
    { err_log(1, 0x00000100, "Could not find 'moov' atom.\n"); };
    return 0;
  }
  if (stco_atom_pos < ilst_atom_pos) {
    last_pos = da_aac_rewrite_stco_atom(extra_size, out_fd, aac_fp, last_pos);
    last_pos = da_aac_insert_covr_atom(extra_size, out_fd, aac_fp, last_pos,
                                       file_size, img_fd);
  } else {
    last_pos = da_aac_insert_covr_atom(extra_size, out_fd, aac_fp, last_pos,
                                       file_size, img_fd);
    last_pos = da_aac_rewrite_stco_atom(extra_size, out_fd, aac_fp, last_pos);
  }
  lseek(aac_fd, last_pos, SEEK_SET);
  r_close(img_fd);
  fclose(aac_fp);
  return last_pos;
}


x86 assembly (clang, -O0):
<da_aac_attach_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1100 <fstat@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x5,%edi
mov    $0x100,%esi
lea    0xde6(%rip),%rdx
mov    $0x0,%al
call   1060 <err_log@plt>
cmpl   $0x1,-0x18(%rbp)
jge    123f <da_aac_attach_image+0x5f>
mov    -0x8(%rbp),%edi
call   1030 <r_close@plt>
movl   $0x0,-0x4(%rbp)
jmp    146a <da_aac_attach_image+0x28a>
mov    -0x18(%rbp),%eax
add    $0x18,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1100 <fstat@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%edi
call   1090 <dup@plt>
mov    %eax,%edi
lea    0xdb2(%rip),%rsi
call   10e0 <fdopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xda0(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   10f0 <aac_drilltoatom@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xda7(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   10f0 <aac_drilltoatom@plt>
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xda4(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   10f0 <aac_drilltoatom@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
je     12e0 <da_aac_attach_image+0x100>
mov    -0x14(%rbp),%eax
cmp    -0x44(%rbp),%eax
jl     12db <da_aac_attach_image+0xfb>
movl   $0x0,-0x4(%rbp)
jmp    146a <da_aac_attach_image+0x28a>
jmp    1304 <da_aac_attach_image+0x124>
mov    $0x1,%edi
mov    $0x100,%esi
lea    0xd65(%rip),%rdx
mov    $0x0,%al
call   1060 <err_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    146a <da_aac_attach_image+0x28a>
mov    -0x38(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x38(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    0xd4c(%rip),%rdx
lea    -0x1c(%rbp),%rcx
call   1050 <scan_aac_findatom@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
je     13a6 <da_aac_attach_image+0x1c6>
mov    -0x38(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x44(%rbp),%edx
call   10b0 <fcopyblock@plt>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x25(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x26(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x27(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <r_write@plt>
mov    -0x44(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x44(%rbp)
jmp    13ca <da_aac_attach_image+0x1ea>
mov    $0x1,%edi
mov    $0x100,%esi
lea    0xcb5(%rip),%rdx
mov    $0x0,%al
call   1060 <err_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    146a <da_aac_attach_image+0x28a>
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    140d <da_aac_attach_image+0x22d>
mov    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x44(%rbp),%ecx
call   10d0 <da_aac_rewrite_stco_atom@plt>
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x44(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x8(%rbp),%r9d
call   1080 <da_aac_insert_covr_atom@plt>
mov    %eax,-0x44(%rbp)
jmp    143f <da_aac_attach_image+0x25f>
mov    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x44(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x8(%rbp),%r9d
call   1080 <da_aac_insert_covr_atom@plt>
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x44(%rbp),%ecx
call   10d0 <da_aac_rewrite_stco_atom@plt>
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    0x2b94(%rip),%rax
mov    (%rax),%edx
call   10a0 <lseek@plt>
mov    -0x8(%rbp),%edi
call   1030 <r_close@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<da_aac_attach_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1100 <fstat@plt>
mov    0x8(%rsp),%r12d
lea    0xdf2(%rip),%rdx
xor    %r13d,%r13d
mov    $0x5,%edi
mov    $0x100,%esi
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <err_log@plt>
test   %r12d,%r12d
jle    1376 <da_aac_attach_image+0x196>
mov    %ebp,0x14(%rsp)
mov    %r15d,0x24(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1100 <fstat@plt>
mov    0x8(%rsp),%ebp
mov    %ebx,0x18(%rsp)
mov    %ebx,%edi
call   1090 <dup@plt>
lea    0xdc4(%rip),%rsi
mov    %eax,%edi
call   10e0 <fdopen@plt>
mov    %rax,%r15
lea    0xdb5(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rax,%rdi
mov    %rbx,%rdx
call   10f0 <aac_drilltoatom@plt>
mov    %eax,%r13d
lea    0xdb9(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   10f0 <aac_drilltoatom@plt>
mov    %eax,0x20(%rsp)
lea    0xdb7(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   10f0 <aac_drilltoatom@plt>
cmp    $0xffffffff,%eax
je     137f <da_aac_attach_image+0x19f>
mov    %r13d,0x1c(%rsp)
xor    %r13d,%r13d
cmp    %r14d,%eax
jle    13f5 <da_aac_attach_image+0x215>
mov    %r15,%rdi
call   1070 <rewind@plt>
lea    0xd99(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    %r15,%rdi
mov    %ebp,%r13d
mov    %ebp,%esi
call   1050 <scan_aac_findatom@plt>
cmp    $0xffffffff,%eax
je     1388 <da_aac_attach_image+0x1a8>
mov    %eax,%r14d
add    $0x18,%r12d
mov    %r15,%rdi
call   1070 <rewind@plt>
mov    %r15,%rdi
mov    0x24(%rsp),%ebx
mov    %ebx,%esi
mov    %r14d,%edx
call   10b0 <fcopyblock@plt>
mov    0x10(%rsp),%eax
add    %r12d,%eax
mov    %eax,0x10(%rsp)
mov    %al,0x7(%rsp)
mov    %ah,0x6(%rsp)
mov    %eax,%ecx
shr    $0x10,%ecx
mov    %cl,0x5(%rsp)
shr    $0x18,%eax
mov    %al,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   10c0 <r_write@plt>
add    $0x4,%r14d
mov    0x20(%rsp),%eax
cmp    %eax,0x1c(%rsp)
jge    13a5 <da_aac_attach_image+0x1c5>
mov    %r12d,%edi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   10d0 <da_aac_rewrite_stco_atom@plt>
mov    %r12d,%edi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %eax,%ecx
mov    %r13d,%r8d
mov    0x14(%rsp),%ebp
mov    %ebp,%r9d
call   1080 <da_aac_insert_covr_atom@plt>
jmp    13ce <da_aac_attach_image+0x1ee>
mov    %ebp,%edi
call   1030 <r_close@plt>
jmp    13f5 <da_aac_attach_image+0x215>
lea    0xcd0(%rip),%rdx
jmp    138f <da_aac_attach_image+0x1af>
lea    0xcdd(%rip),%rdx
xor    %r13d,%r13d
mov    $0x1,%edi
mov    $0x100,%esi
xor    %eax,%eax
call   1060 <err_log@plt>
jmp    13f5 <da_aac_attach_image+0x215>
mov    %r12d,%edi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
mov    %r13d,%r8d
mov    0x14(%rsp),%ebp
mov    %ebp,%r9d
call   1080 <da_aac_insert_covr_atom@plt>
mov    %r12d,%edi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %eax,%ecx
call   10d0 <da_aac_rewrite_stco_atom@plt>
mov    %eax,%r13d
mov    0x18(%rsp),%edi
mov    0x2c04(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%esi
call   10a0 <lseek@plt>
mov    %ebp,%edi
call   1030 <r_close@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [29 x i8] c"Image size (in bytes): %ld.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"moov:trak:mdia:minf:stbl:stco\00"
@.str.3 = [20 x i8] c"moov:udta:meta:ilst\00"
@.str.4 = [5 x i8] c"mdat\00"
@.str.5 = [17 x i8] c"No 'mdat' atom.\0A\00"
@.str.6 = [5 x i8] c"moov\00"
@.str.7 = [29 x i8] c"Could not find 'moov' atom.\0A\00"
@SEEK_SET = global i32 0
define i32 @da_aac_attach_image(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [4 x i8]
%15 = alloca %struct.stat
%16 = alloca i32*
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%20 = load i32, i32* %6
%21 = call i32 @fstat(i32 %20, %struct.stat* %15)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%23 = load i32, i32* %22
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = call i32 (i32, i32, i8*, ...) @err_log(i32 5, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %4
%29 = load i32, i32* %6
%30 = call i32 @r_close(i32 %29)
store i32 0, i32* %5
br label %141
31: ; preds = %4
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 24
store i32 %33, i32* %12
%34 = load i32, i32* %8
%35 = call i32 @fstat(i32 %34, %struct.stat* %15)
%36 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%37 = load i32, i32* %36
store i32 %37, i32* %13
%38 = load i32, i32* %8
%39 = call i32 @dup(i32 %38)
%40 = call i32* @fdopen(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %16
%41 = load i32*, i32** %16
%42 = call i32 @aac_drilltoatom(i32* %41, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32* %11)
store i32 %42, i32* %17
%43 = load i32*, i32** %16
%44 = call i32 @aac_drilltoatom(i32* %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32* %11)
store i32 %44, i32* %18
%45 = load i32*, i32** %16
%46 = call i32 @aac_drilltoatom(i32* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32* %11)
store i32 %46, i32* %19
%47 = load i32, i32* %19
%48 = icmp ne i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %31
%50 = load i32, i32* %9
%51 = load i32, i32* %19
%52 = icmp sge i32 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 0, i32* %5
br label %141
54: ; preds = %49
br label %57
55: ; preds = %31
%56 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %5
br label %141
57: ; preds = %54
%58 = load i32*, i32** %16
%59 = call i32 @rewind(i32* %58)
%60 = load i32*, i32** %16
%61 = load i32, i32* %13
%62 = call i32 @scan_aac_findatom(i32* %60, i32 %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32* %11)
store i32 %62, i32* %19
%63 = load i32, i32* %19
%64 = icmp ne i32 %63, -1
br i1 %64, label %65, label %99
65: ; preds = %57
%66 = load i32*, i32** %16
%67 = call i32 @rewind(i32* %66)
%68 = load i32*, i32** %16
%69 = load i32, i32* %7
%70 = load i32, i32* %19
%71 = call i32 @fcopyblock(i32* %68, i32 %69, i32 %70)
%72 = load i32, i32* %12
%73 = load i32, i32* %11
%74 = add i32 %73, %72
store i32 %74, i32* %11
%75 = load i32, i32* %11
%76 = and i32 %75, 255
%77 = trunc i32 %76 to i8
%78 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 3
store i8 %77, i8* %78
%79 = load i32, i32* %11
%80 = ashr i32 %79, 8
%81 = and i32 %80, 255
%82 = trunc i32 %81 to i8
%83 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 2
store i8 %82, i8* %83
%84 = load i32, i32* %11
%85 = ashr i32 %84, 16
%86 = and i32 %85, 255
%87 = trunc i32 %86 to i8
%88 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 1
store i8 %87, i8* %88
%89 = load i32, i32* %11
%90 = ashr i32 %89, 24
%91 = and i32 %90, 255
%92 = trunc i32 %91 to i8
%93 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
store i8 %92, i8* %93
%94 = load i32, i32* %7
%95 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%96 = call i32 @r_write(i32 %94, i8* %95, i32 4)
%97 = load i32, i32* %19
%98 = add nsw i32 %97, 4
store i32 %98, i32* %19
br label %101
99: ; preds = %57
%100 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %5
br label %141
101: ; preds = %65
%102 = load i32, i32* %17
%103 = load i32, i32* %18
%104 = icmp slt i32 %102, %103
br i1 %104, label %105, label %118
105: ; preds = %101
%106 = load i32, i32* %12
%107 = load i32, i32* %7
%108 = load i32*, i32** %16
%109 = load i32, i32* %19
%110 = call i32 @da_aac_rewrite_stco_atom(i32 %106, i32 %107, i32* %108, i32 %109)
store i32 %110, i32* %19
%111 = load i32, i32* %12
%112 = load i32, i32* %7
%113 = load i32*, i32** %16
%114 = load i32, i32* %19
%115 = load i32, i32* %13
%116 = load i32, i32* %6
%117 = call i32 @da_aac_insert_covr_atom(i32 %111, i32 %112, i32* %113, i32 %114, i32 %115, i32 %116)
store i32 %117, i32* %19
br label %131
118: ; preds = %101
%119 = load i32, i32* %12
%120 = load i32, i32* %7
%121 = load i32*, i32** %16
%122 = load i32, i32* %19
%123 = load i32, i32* %13
%124 = load i32, i32* %6
%125 = call i32 @da_aac_insert_covr_atom(i32 %119, i32 %120, i32* %121, i32 %122, i32 %123, i32 %124)
store i32 %125, i32* %19
%126 = load i32, i32* %12
%127 = load i32, i32* %7
%128 = load i32*, i32** %16
%129 = load i32, i32* %19
%130 = call i32 @da_aac_rewrite_stco_atom(i32 %126, i32 %127, i32* %128, i32 %129)
store i32 %130, i32* %19
br label %131
131: ; preds = %118, %105
%132 = load i32, i32* %8
%133 = load i32, i32* %19
%134 = load i32, i32* @SEEK_SET
%135 = call i32 @lseek(i32 %132, i32 %133, i32 %134)
%136 = load i32, i32* %6
%137 = call i32 @r_close(i32 %136)
%138 = load i32*, i32** %16
%139 = call i32 @fclose(i32* %138)
%140 = load i32, i32* %19
store i32 %140, i32* %5
br label %141
141: ; preds = %131, %99, %55, %53, %28
%142 = load i32, i32* %5
ret i32 %142
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [29 x i8] c"Image size (in bytes): %ld.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"moov:trak:mdia:minf:stbl:stco\00"
@.str.3 = [20 x i8] c"moov:udta:meta:ilst\00"
@.str.4 = [5 x i8] c"mdat\00"
@.str.5 = [17 x i8] c"No 'mdat' atom.\0A\00"
@.str.6 = [5 x i8] c"moov\00"
@.str.7 = [29 x i8] c"Could not find 'moov' atom.\0A\00"
@SEEK_SET = global i32 0
define i32 @da_aac_attach_image(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca [4 x i8]
%7 = alloca %struct.stat
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%10 = bitcast %struct.stat* %7 to i8*
%11 = call i32 @fstat(i32 %0, %struct.stat* %7)
%12 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 (i32, i32, i8*, ...) @err_log(i32 5, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %13, 1
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = tail call i32 @r_close(i32 %0)
br label %68
18: ; preds = %4
%19 = add nuw nsw i32 %13, 24
%20 = call i32 @fstat(i32 %2, %struct.stat* %7)
%21 = load i32, i32* %12
%22 = tail call i32 @dup(i32 %2)
%23 = tail call i32* @fdopen(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @aac_drilltoatom(i32* %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%25 = call i32 @aac_drilltoatom(i32* %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%26 = call i32 @aac_drilltoatom(i32* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%27 = icmp eq i32 %26, -1
br i1 %27, label %30, label %28
28: ; preds = %18
%29 = icmp sgt i32 %26, %3
br i1 %29, label %32, label %68
30: ; preds = %18
%31 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %68
32: ; preds = %28
%33 = call i32 @rewind(i32* %23)
%34 = call i32 @scan_aac_findatom(i32* %23, i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32* %5)
%35 = icmp eq i32 %34, -1
br i1 %35, label %54, label %36
36: ; preds = %32
%37 = call i32 @rewind(i32* %23)
%38 = call i32 @fcopyblock(i32* %23, i32 %1, i32 %34)
%39 = load i32, i32* %5
%40 = add i32 %39, %19
store i32 %40, i32* %5
%41 = trunc i32 %40 to i8
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
store i8 %41, i8* %42
%43 = lshr i32 %40, 8
%44 = trunc i32 %43 to i8
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
store i8 %44, i8* %45
%46 = lshr i32 %40, 16
%47 = trunc i32 %46 to i8
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
store i8 %47, i8* %48
%49 = lshr i32 %40, 24
%50 = trunc i32 %49 to i8
store i8 %50, i8* %9
%51 = call i32 @r_write(i32 %1, i8* %9, i32 4)
%52 = add nsw i32 %34, 4
%53 = icmp slt i32 %24, %25
br i1 %53, label %56, label %59
54: ; preds = %32
%55 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %68
56: ; preds = %36
%57 = call i32 @da_aac_rewrite_stco_atom(i32 %19, i32 %1, i32* %23, i32 %52)
%58 = call i32 @da_aac_insert_covr_atom(i32 %19, i32 %1, i32* %23, i32 %57, i32 %21, i32 %0)
br label %62
59: ; preds = %36
%60 = call i32 @da_aac_insert_covr_atom(i32 %19, i32 %1, i32* %23, i32 %52, i32 %21, i32 %0)
%61 = call i32 @da_aac_rewrite_stco_atom(i32 %19, i32 %1, i32* %23, i32 %60)
br label %62
62: ; preds = %59, %56
%63 = phi i32 [ %58, %56 ], [ %61, %59 ]
%64 = load i32, i32* @SEEK_SET
%65 = call i32 @lseek(i32 %2, i32 %63, i32 %64)
%66 = call i32 @r_close(i32 %0)
%67 = call i32 @fclose(i32* %23)
br label %68
68: ; preds = %28, %62, %54, %30, %16
%69 = phi i32 [ 0, %16 ], [ %63, %62 ], [ 0, %54 ], [ 0, %30 ], [ 0, %28 ]
ret i32 %69
}