#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_CUR;
int /*<<< orphan*/ SEEK_SET;
int aac_drilltoatom(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ err_log(int, int, char *, ...);
int /*<<< orphan*/ fcopyblock(int /*<<< orphan*/ *, int, int);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fstat(int, struct stat *);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ r_write(int, unsigned char *, int);
off_t da_aac_rewrite_stco_atom(off_t extra_size, int out_fd, FILE *aac_fp,
                               off_t last_pos) {
  int aac_fd;
  struct stat sb;
  unsigned char buffer[4];
  off_t file_size;
  int atom_offset;
  int atom_length;
  off_t cur_pos;
  off_t old_pos;
  int i;
  unsigned int num_entries;
  unsigned int offset_entry;
  aac_fd = fileno(aac_fp);
  fstat(aac_fd, &sb);
  file_size = sb.st_size;
  atom_offset =
      aac_drilltoatom(aac_fp, "moov:trak:mdia:minf:stbl:stco", &atom_length);
  if (atom_offset != -1) {
    fseek(aac_fp, 4, SEEK_CUR);
    old_pos = last_pos;
    cur_pos = ftell(aac_fp);
    fseek(aac_fp, old_pos, SEEK_SET);
    fcopyblock(aac_fp, out_fd, cur_pos - old_pos);
    fread(buffer, 1, 4, aac_fp);
    r_write(out_fd, buffer, 4);
    num_entries =
        (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
    {
      err_log(9, 0x00000100, "Readjusting %d 'stco' table offsets.\n",
              num_entries);
    };
    for (i = 0; i < num_entries; i++) {
      fread(buffer, 1, 4, aac_fp);
      offset_entry =
          (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
      offset_entry += extra_size;
      buffer[3] = offset_entry & 0xFF;
      buffer[2] = (offset_entry >> 8) & 0xFF;
      buffer[1] = (offset_entry >> 16) & 0xFF;
      buffer[0] = (offset_entry >> 24) & 0xFF;
      r_write(out_fd, buffer, 4);
      offset_entry = 0;
    }
    return ftell(aac_fp);
  } else {
    { err_log(1, 0x00000100, "No 'stco' atom found.\n"); };
  }
  return last_pos;
}


x86 assembly (clang, -O0):
<da_aac_rewrite_stco_atom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe32(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   10a0 <aac_drilltoatom@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
je     135a <da_aac_rewrite_stco_atom+0x1ca>
mov    -0x18(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1090 <fseek@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x3c(%rbp),%edx
sub    -0x40(%rbp),%edx
call   1050 <fcopyblock@plt>
lea    -0x2c(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    -0xc(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <r_write@plt>
movzbl -0x2c(%rbp),%eax
shl    $0x18,%eax
movzbl -0x2b(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movzbl -0x2a(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x29(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%ecx
mov    $0x9,%edi
mov    $0x100,%esi
lea    0xd8c(%rip),%rdx
mov    $0x0,%al
call   1040 <err_log@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jae    1349 <da_aac_rewrite_stco_atom+0x1b9>
lea    -0x2c(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
movzbl -0x2c(%rbp),%eax
shl    $0x18,%eax
movzbl -0x2b(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movzbl -0x2a(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x29(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x29(%rbp)
mov    -0x4c(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x2a(%rbp)
mov    -0x4c(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x2b(%rbp)
mov    -0x4c(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <r_write@plt>
movl   $0x0,-0x4c(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    12a0 <da_aac_rewrite_stco_atom+0x110>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x4(%rbp)
jmp    1378 <da_aac_rewrite_stco_atom+0x1e8>
mov    $0x1,%edi
mov    $0x100,%esi
lea    0xcd9(%rip),%rdx
mov    $0x0,%al
call   1040 <err_log@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<da_aac_rewrite_stco_atom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %esi,%r12d
mov    %edi,%r14d
mov    %rdx,%rdi
call   1080 <fileno@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
lea    0xe3b(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %r13,%rdi
call   10a0 <aac_drilltoatom@plt>
cmp    $0xffffffff,%eax
je     12c6 <da_aac_rewrite_stco_atom+0x136>
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0x4,%esi
call   1090 <fseek@plt>
mov    %r13,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    %r15d,%esi
call   1090 <fseek@plt>
sub    %r15d,%ebx
mov    %r13,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1050 <fcopyblock@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <r_write@plt>
mov    0xc(%rsp),%ebp
bswap  %ebp
lea    0xdc8(%rip),%rdx
mov    $0x9,%edi
mov    $0x100,%esi
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <err_log@plt>
test   %ebp,%ebp
je     12b9 <da_aac_rewrite_stco_atom+0x129>
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    0xc(%rsp),%eax
bswap  %eax
add    %r14d,%eax
bswap  %eax
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <r_write@plt>
add    $0xffffffff,%ebp
jne    1280 <da_aac_rewrite_stco_atom+0xf0>
mov    %r13,%rdi
call   1060 <ftell@plt>
mov    %eax,%r15d
jmp    12de <da_aac_rewrite_stco_atom+0x14e>
lea    0xd77(%rip),%rdx
mov    $0x1,%edi
mov    $0x100,%esi
xor    %eax,%eax
call   1040 <err_log@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [30 x i8] c"moov:trak:mdia:minf:stbl:stco\00"
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [38 x i8] c"Readjusting %d 'stco' table offsets.\0A\00"
@.str.2 = [23 x i8] c"No 'stco' atom found.\0A\00"
define i32 @da_aac_rewrite_stco_atom(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 %struct.stat
%12 = alloca [4 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%21 = load i32*, i32** %8
%22 = call i32 @fileno(i32* %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = call i32 @fstat(i32 %23, %struct.stat* %11)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %13
%27 = load i32*, i32** %8
%28 = call i32 @aac_drilltoatom(i32* %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32* %15)
store i32 %28, i32* %14
%29 = load i32, i32* %14
%30 = icmp ne i32 %29, -1
br i1 %30, label %31, label %131
31: ; preds = %4
%32 = load i32*, i32** %8
%33 = load i32, i32* @SEEK_CUR
%34 = call i32 @fseek(i32* %32, i32 4, i32 %33)
%35 = load i32, i32* %9
store i32 %35, i32* %17
%36 = load i32*, i32** %8
%37 = call i32 @ftell(i32* %36)
store i32 %37, i32* %16
%38 = load i32*, i32** %8
%39 = load i32, i32* %17
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @fseek(i32* %38, i32 %39, i32 %40)
%42 = load i32*, i32** %8
%43 = load i32, i32* %7
%44 = load i32, i32* %16
%45 = load i32, i32* %17
%46 = sub nsw i32 %44, %45
%47 = call i32 @fcopyblock(i32* %42, i32 %43, i32 %46)
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%49 = load i32*, i32** %8
%50 = call i32 @fread(i8* %48, i32 1, i32 4, i32* %49)
%51 = load i32, i32* %7
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%53 = call i32 @r_write(i32 %51, i8* %52, i32 4)
%54 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = shl i32 %56, 24
%58 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 1
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = shl i32 %60, 16
%62 = or i32 %57, %61
%63 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 2
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = shl i32 %65, 8
%67 = or i32 %62, %66
%68 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 3
%69 = load i8, i8* %68
%70 = zext i8 %69 to i32
%71 = or i32 %67, %70
store i32 %71, i32* %19
%72 = load i32, i32* %19
%73 = call i32 (i32, i32, i8*, ...) @err_log(i32 9, i32 256, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %72)
store i32 0, i32* %18
br label %74
74: ; preds = %125, %31
%75 = load i32, i32* %18
%76 = load i32, i32* %19
%77 = icmp ult i32 %75, %76
br i1 %77, label %78, label %128
78: ; preds = %74
%79 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%80 = load i32*, i32** %8
%81 = call i32 @fread(i8* %79, i32 1, i32 4, i32* %80)
%82 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%83 = load i8, i8* %82
%84 = zext i8 %83 to i32
%85 = shl i32 %84, 24
%86 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 1
%87 = load i8, i8* %86
%88 = zext i8 %87 to i32
%89 = shl i32 %88, 16
%90 = or i32 %85, %89
%91 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 2
%92 = load i8, i8* %91
%93 = zext i8 %92 to i32
%94 = shl i32 %93, 8
%95 = or i32 %90, %94
%96 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 3
%97 = load i8, i8* %96
%98 = zext i8 %97 to i32
%99 = or i32 %95, %98
store i32 %99, i32* %20
%100 = load i32, i32* %6
%101 = load i32, i32* %20
%102 = add i32 %101, %100
store i32 %102, i32* %20
%103 = load i32, i32* %20
%104 = and i32 %103, 255
%105 = trunc i32 %104 to i8
%106 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 3
store i8 %105, i8* %106
%107 = load i32, i32* %20
%108 = lshr i32 %107, 8
%109 = and i32 %108, 255
%110 = trunc i32 %109 to i8
%111 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 2
store i8 %110, i8* %111
%112 = load i32, i32* %20
%113 = lshr i32 %112, 16
%114 = and i32 %113, 255
%115 = trunc i32 %114 to i8
%116 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 1
store i8 %115, i8* %116
%117 = load i32, i32* %20
%118 = lshr i32 %117, 24
%119 = and i32 %118, 255
%120 = trunc i32 %119 to i8
%121 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
store i8 %120, i8* %121
%122 = load i32, i32* %7
%123 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%124 = call i32 @r_write(i32 %122, i8* %123, i32 4)
store i32 0, i32* %20
br label %125
125: ; preds = %78
%126 = load i32, i32* %18
%127 = add nsw i32 %126, 1
store i32 %127, i32* %18
br label %74
128: ; preds = %74
%129 = load i32*, i32** %8
%130 = call i32 @ftell(i32* %129)
store i32 %130, i32* %5
br label %135
131: ; preds = %4
%132 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 256, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %133
133: ; preds = %131
%134 = load i32, i32* %9
store i32 %134, i32* %5
br label %135
135: ; preds = %133, %128
%136 = load i32, i32* %5
ret i32 %136
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [30 x i8] c"moov:trak:mdia:minf:stbl:stco\00"
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [38 x i8] c"Readjusting %d 'stco' table offsets.\0A\00"
@.str.2 = [23 x i8] c"No 'stco' atom found.\0A\00"
define i32 @da_aac_rewrite_stco_atom(i32 %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca %struct.stat
%6 = alloca [4 x i8]
%7 = alloca i32
%8 = bitcast %struct.stat* %5 to i8*
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%10 = bitcast i32* %7 to i8*
%11 = tail call i32 @fileno(i32* %2)
%12 = call i32 @fstat(i32 %11, %struct.stat* %5)
%13 = call i32 @aac_drilltoatom(i32* %2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32* %7)
%14 = icmp eq i32 %13, -1
br i1 %14, label %74, label %15
15: ; preds = %4
%16 = load i32, i32* @SEEK_CUR
%17 = call i32 @fseek(i32* %2, i32 4, i32 %16)
%18 = call i32 @ftell(i32* %2)
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %2, i32 %3, i32 %19)
%21 = sub nsw i32 %18, %3
%22 = call i32 @fcopyblock(i32* %2, i32 %1, i32 %21)
%23 = call i32 @fread(i8* %9, i32 1, i32 4, i32* %2)
%24 = call i32 @r_write(i32 %1, i8* %9, i32 4)
%25 = load i8, i8* %9
%26 = zext i8 %25 to i32
%27 = shl nuw i32 %26, 24
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = shl nuw nsw i32 %30, 16
%32 = or i32 %31, %27
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = shl nuw nsw i32 %35, 8
%37 = or i32 %32, %36
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = or i32 %37, %40
%42 = call i32 (i32, i32, i8*, ...) @err_log(i32 9, i32 256, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = icmp eq i32 %41, 0
br i1 %43, label %72, label %44
44: ; preds = %15, %44
%45 = phi i32 [ %70, %44 ], [ 0, %15 ]
%46 = call i32 @fread(i8* %9, i32 1, i32 4, i32* %2)
%47 = load i8, i8* %9
%48 = zext i8 %47 to i32
%49 = shl nuw i32 %48, 24
%50 = load i8, i8* %28
%51 = zext i8 %50 to i32
%52 = shl nuw nsw i32 %51, 16
%53 = or i32 %52, %49
%54 = load i8, i8* %33
%55 = zext i8 %54 to i32
%56 = shl nuw nsw i32 %55, 8
%57 = or i32 %53, %56
%58 = load i8, i8* %38
%59 = zext i8 %58 to i32
%60 = or i32 %57, %59
%61 = add i32 %60, %0
%62 = trunc i32 %61 to i8
store i8 %62, i8* %38
%63 = lshr i32 %61, 8
%64 = trunc i32 %63 to i8
store i8 %64, i8* %33
%65 = lshr i32 %61, 16
%66 = trunc i32 %65 to i8
store i8 %66, i8* %28
%67 = lshr i32 %61, 24
%68 = trunc i32 %67 to i8
store i8 %68, i8* %9
%69 = call i32 @r_write(i32 %1, i8* %9, i32 4)
%70 = add nuw i32 %45, 1
%71 = icmp eq i32 %70, %41
br i1 %71, label %72, label %44
72: ; preds = %44, %15
%73 = call i32 @ftell(i32* %2)
br label %76
74: ; preds = %4
%75 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 256, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %76
76: ; preds = %74, %72
%77 = phi i32 [ %73, %72 ], [ %3, %74 ]
ret i32 %77
}