#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 uint8_t;
typedef int /*<<< orphan*/ uint64_t;
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write_pes_header(int, int, int /*<<< orphan*/, int /*<<< orphan*/,
                            int *, int, int);
int write_ps_header(int *, int /*<<< orphan*/, int /*<<< orphan*/, int,
                    int /*<<< orphan*/, int /*<<< orphan*/, int, int, int,
                    int /*<<< orphan*/);
int bwrite_audio_pes(int pack_size, int apidn, int ac3n, int n, uint64_t pts,
                     uint64_t SCR, uint32_t muxr, uint8_t *buf, int *alength,
                     uint8_t ptsdts, uint8_t *arbuffer, int bsize) {
  int add;
  int pos = 0;
  int p = 0;
  int stuff = 0;
  int length = *alength;
  if (!length)
    return 0;
  p = 14 + 9;
  if (ptsdts == 0x80) {
    p += 5;
  }
  if (length + p >= pack_size) {
    length = pack_size;
  } else {
    if (pack_size - length - p <= 7) {
      stuff = pack_size - length - p;
      length = pack_size;
    } else
      length = length + p;
  }
  pos = write_ps_header(buf, SCR, muxr, apidn + ac3n, 0, 0, 1, 1, 1, 0);
  pos += write_pes_header(0xC0 + n, length - pos, pts, 0, buf + pos, stuff,
                          ptsdts);
  if (length - pos < bsize) {
    memcpy(buf + pos, arbuffer, length - pos);
    add = length - pos;
    *alength = add;
  } else
    return -1;
  pos += add;
  if (pos + 7 < pack_size) {
    pos += write_pes_header(0xBE, pack_size - pos, 0, 0, buf + pos, 0, 0);
    pos = pack_size;
  }
  if (pos != pack_size) {
    fprintf(stderr, "apos: %d\n", pos);
    exit(1);
  }
  return pos;
}


x86 assembly (clang, -O0):
<bwrite_audio_pes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x38(%rbp),%eax
mov    0x30(%rbp),%rax
mov    0x28(%rbp),%eax
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%eax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    11b5 <bwrite_audio_pes+0x65>
movl   $0x0,-0x4(%rbp)
jmp    137d <bwrite_audio_pes+0x22d>
movl   $0x17,-0x28(%rbp)
cmpl   $0x80,0x28(%rbp)
jne    11d2 <bwrite_audio_pes+0x82>
mov    -0x28(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
add    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     11ec <bwrite_audio_pes+0x9c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    1223 <bwrite_audio_pes+0xd3>
mov    -0x8(%rbp),%eax
sub    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
cmp    $0x7,%eax
jg     1215 <bwrite_audio_pes+0xc5>
mov    -0x8(%rbp),%eax
sub    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    121e <bwrite_audio_pes+0xce>
mov    -0x30(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    1223 <bwrite_audio_pes+0xd3>
mov    0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
xor    %r9d,%r9d
mov    %r9d,%r8d
movl   $0x1,(%rsp)
movl   $0x1,0x8(%rsp)
movl   $0x1,0x10(%rsp)
movl   $0x0,0x18(%rsp)
call   1070 <write_ps_header@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
add    $0xc0,%edi
mov    -0x30(%rbp),%esi
sub    -0x24(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    0x18(%rbp),%r8
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%r8
mov    -0x2c(%rbp),%r9d
mov    0x28(%rbp),%eax
xor    %ecx,%ecx
mov    %eax,(%rsp)
call   1030 <write_pes_header@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%eax
sub    -0x24(%rbp),%eax
cmp    0x38(%rbp),%eax
jge    12e3 <bwrite_audio_pes+0x193>
mov    0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    0x30(%rbp),%rsi
mov    -0x30(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    12ef <bwrite_audio_pes+0x19f>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <bwrite_audio_pes+0x22d>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x7,%eax
cmp    -0x8(%rbp),%eax
jge    1349 <bwrite_audio_pes+0x1f9>
mov    -0x8(%rbp),%esi
sub    -0x24(%rbp),%esi
mov    0x18(%rbp),%r8
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%r8
mov    $0xbe,%edi
xor    %r9d,%r9d
mov    %r9d,%edx
mov    %r9d,%ecx
movl   $0x0,(%rsp)
call   1030 <write_pes_header@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1377 <bwrite_audio_pes+0x227>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xc98(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bwrite_audio_pes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x50(%rsp),%rax
mov    (%rax),%eax
test   %eax,%eax
je     1268 <bwrite_audio_pes+0x118>
mov    %ecx,%r13d
mov    %edx,%ecx
mov    %edi,%r12d
mov    0x58(%rsp),%edi
xor    %edx,%edx
cmp    $0x80,%edi
sete   %dl
lea    (%rdx,%rdx,4),%edx
add    %edx,%eax
add    $0x17,%eax
mov    %r12d,%ebp
sub    %eax,%ebp
xor    %edx,%edx
cmp    $0x8,%ebp
mov    %eax,%ebx
cmovl  %r12d,%ebx
cmovge %edx,%ebp
mov    0x48(%rsp),%rdi
cmp    %eax,%r12d
cmovle %edx,%ebp
mov    0x40(%rsp),%edx
cmovle %r12d,%ebx
add    %esi,%ecx
mov    %r9d,%esi
mov    %r8d,%r15d
xor    %r8d,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0x1
push   $0x1
push   $0x1
call   1070 <write_ps_header@plt>
add    $0x20,%rsp
add    $0xc0,%r13d
mov    %ebx,%esi
sub    %eax,%esi
movslq %eax,%r14
mov    0x48(%rsp),%rax
lea    (%rax,%r14,4),%r8
sub    $0x8,%rsp
mov    %r13d,%edi
mov    %r15d,%edx
xor    %ecx,%ecx
mov    %ebp,%r9d
mov    0x60(%rsp),%eax
push   %rax
call   1030 <write_pes_header@plt>
add    $0x10,%rsp
add    %eax,%r14d
mov    %ebx,%ebp
sub    %r14d,%ebp
mov    $0xffffffff,%eax
cmp    0x68(%rsp),%ebp
jge    1274 <bwrite_audio_pes+0x124>
mov    0x60(%rsp),%rsi
movslq %r14d,%rax
mov    0x48(%rsp),%r14
lea    (%r14,%rax,4),%rdi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    0x50(%rsp),%rax
mov    %ebp,(%rax)
lea    0x7(%rbx),%eax
cmp    %r12d,%eax
jge    126c <bwrite_audio_pes+0x11c>
mov    %r12d,%esi
sub    %ebx,%esi
movslq %ebx,%rax
lea    (%r14,%rax,4),%r8
sub    $0x8,%rsp
mov    $0xbe,%edi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
push   $0x0
call   1030 <write_pes_header@plt>
add    $0x10,%rsp
mov    %r12d,%eax
jmp    1274 <bwrite_audio_pes+0x124>
xor    %eax,%eax
jmp    1274 <bwrite_audio_pes+0x124>
mov    %r12d,%eax
cmp    %r12d,%ebx
jne    1283 <bwrite_audio_pes+0x133>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [10 x i8] c"apos: %d\0A\00"
define i32 @bwrite_audio_pes(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32* %7, i32* %8, i32 %9, i32* %10, i32 %11) {
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32*
%22 = alloca i32*
%23 = alloca i32
%24 = alloca i32*
%25 = alloca i32
%26 = alloca i32
%27 = alloca i32
%28 = alloca i32
%29 = alloca i32
%30 = alloca i32
store i32 %0, i32* %14
store i32 %1, i32* %15
store i32 %2, i32* %16
store i32 %3, i32* %17
store i32 %4, i32* %18
store i32 %5, i32* %19
store i32 %6, i32* %20
store i32* %7, i32** %21
store i32* %8, i32** %22
store i32 %9, i32* %23
store i32* %10, i32** %24
store i32 %11, i32* %25
store i32 0, i32* %27
store i32 0, i32* %28
store i32 0, i32* %29
%31 = load i32*, i32** %22
%32 = load i32, i32* %31
store i32 %32, i32* %30
%33 = load i32, i32* %30
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %12
store i32 0, i32* %13
br label %148
36: ; preds = %12
store i32 23, i32* %28
%37 = load i32, i32* %23
%38 = icmp eq i32 %37, 128
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %28
%41 = add nsw i32 %40, 5
store i32 %41, i32* %28
br label %42
42: ; preds = %39, %36
%43 = load i32, i32* %30
%44 = load i32, i32* %28
%45 = add nsw i32 %43, %44
%46 = load i32, i32* %14
%47 = icmp sge i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = load i32, i32* %14
store i32 %49, i32* %30
br label %69
50: ; preds = %42
%51 = load i32, i32* %14
%52 = load i32, i32* %30
%53 = sub nsw i32 %51, %52
%54 = load i32, i32* %28
%55 = sub nsw i32 %53, %54
%56 = icmp sle i32 %55, 7
br i1 %56, label %57, label %64
57: ; preds = %50
%58 = load i32, i32* %14
%59 = load i32, i32* %30
%60 = sub nsw i32 %58, %59
%61 = load i32, i32* %28
%62 = sub nsw i32 %60, %61
store i32 %62, i32* %29
%63 = load i32, i32* %14
store i32 %63, i32* %30
br label %68
64: ; preds = %50
%65 = load i32, i32* %30
%66 = load i32, i32* %28
%67 = add nsw i32 %65, %66
store i32 %67, i32* %30
br label %68
68: ; preds = %64, %57
br label %69
69: ; preds = %68, %48
%70 = load i32*, i32** %21
%71 = load i32, i32* %19
%72 = load i32, i32* %20
%73 = load i32, i32* %15
%74 = load i32, i32* %16
%75 = add nsw i32 %73, %74
%76 = call i32 @write_ps_header(i32* %70, i32 %71, i32 %72, i32 %75, i32 0, i32 0, i32 1, i32 1, i32 1, i32 0)
store i32 %76, i32* %27
%77 = load i32, i32* %17
%78 = add nsw i32 192, %77
%79 = load i32, i32* %30
%80 = load i32, i32* %27
%81 = sub nsw i32 %79, %80
%82 = load i32, i32* %18
%83 = load i32*, i32** %21
%84 = load i32, i32* %27
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %29
%88 = load i32, i32* %23
%89 = call i64 @write_pes_header(i32 %78, i32 %81, i32 %82, i32 0, i32* %86, i32 %87, i32 %88)
%90 = load i32, i32* %27
%91 = sext i32 %90 to i64
%92 = add nsw i64 %91, %89
%93 = trunc i64 %92 to i32
store i32 %93, i32* %27
%94 = load i32, i32* %30
%95 = load i32, i32* %27
%96 = sub nsw i32 %94, %95
%97 = load i32, i32* %25
%98 = icmp slt i32 %96, %97
br i1 %98, label %99, label %114
99: ; preds = %69
%100 = load i32*, i32** %21
%101 = load i32, i32* %27
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i32, i32* %100, i64 %102
%104 = load i32*, i32** %24
%105 = load i32, i32* %30
%106 = load i32, i32* %27
%107 = sub nsw i32 %105, %106
%108 = call i32 @memcpy(i32* %103, i32* %104, i32 %107)
%109 = load i32, i32* %30
%110 = load i32, i32* %27
%111 = sub nsw i32 %109, %110
store i32 %111, i32* %26
%112 = load i32, i32* %26
%113 = load i32*, i32** %22
store i32 %112, i32* %113
br label %115
114: ; preds = %69
store i32 -1, i32* %13
br label %148
115: ; preds = %99
%116 = load i32, i32* %26
%117 = load i32, i32* %27
%118 = add nsw i32 %117, %116
store i32 %118, i32* %27
%119 = load i32, i32* %27
%120 = add nsw i32 %119, 7
%121 = load i32, i32* %14
%122 = icmp slt i32 %120, %121
br i1 %122, label %123, label %137
123: ; preds = %115
%124 = load i32, i32* %14
%125 = load i32, i32* %27
%126 = sub nsw i32 %124, %125
%127 = load i32*, i32** %21
%128 = load i32, i32* %27
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds i32, i32* %127, i64 %129
%131 = call i64 @write_pes_header(i32 190, i32 %126, i32 0, i32 0, i32* %130, i32 0, i32 0)
%132 = load i32, i32* %27
%133 = sext i32 %132 to i64
%134 = add nsw i64 %133, %131
%135 = trunc i64 %134 to i32
store i32 %135, i32* %27
%136 = load i32, i32* %14
store i32 %136, i32* %27
br label %137
137: ; preds = %123, %115
%138 = load i32, i32* %27
%139 = load i32, i32* %14
%140 = icmp ne i32 %138, %139
br i1 %140, label %141, label %146
141: ; preds = %137
%142 = load i32, i32* @stderr
%143 = load i32, i32* %27
%144 = call i32 @fprintf(i32 %142, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %143)
%145 = call i32 @exit(i32 1)
unreachable
146: ; preds = %137
%147 = load i32, i32* %27
store i32 %147, i32* %13
br label %148
148: ; preds = %146, %114, %35
%149 = load i32, i32* %13
ret i32 %149
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [10 x i8] c"apos: %d\0A\00"
define i32 @bwrite_audio_pes(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32* %7, i32* nocapture %8, i32 %9, i32* %10, i32 %11) {
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, 0
br i1 %14, label %54, label %15
15: ; preds = %12
%16 = icmp eq i32 %9, 128
%17 = select i1 %16, i32 28, i32 23
%18 = add i32 %13, %17
%19 = icmp slt i32 %18, %0
%20 = sub i32 %0, %18
%21 = icmp slt i32 %20, 8
%22 = select i1 %21, i32 %0, i32 %18
%23 = and i1 %19, %21
%24 = select i1 %23, i32 %20, i32 0
%25 = select i1 %19, i32 %22, i32 %0
%26 = add nsw i32 %2, %1
%27 = tail call i32 @write_ps_header(i32* %7, i32 %5, i32 %6, i32 %26, i32 0, i32 0, i32 1, i32 1, i32 1, i32 0)
%28 = add nsw i32 %3, 192
%29 = sub nsw i32 %25, %27
%30 = sext i32 %27 to i64
%31 = getelementptr inbounds i32, i32* %7, i64 %30
%32 = tail call i64 @write_pes_header(i32 %28, i32 %29, i32 %4, i32 0, i32* %31, i32 %24, i32 %9)
%33 = trunc i64 %32 to i32
%34 = add i32 %27, %33
%35 = sub nsw i32 %25, %34
%36 = icmp slt i32 %35, %11
br i1 %36, label %37, label %54
37: ; preds = %15
%38 = sext i32 %34 to i64
%39 = getelementptr inbounds i32, i32* %7, i64 %38
%40 = tail call i32 @memcpy(i32* %39, i32* %10, i32 %35)
store i32 %35, i32* %8
%41 = add nsw i32 %25, 7
%42 = icmp slt i32 %41, %0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = sub nsw i32 %0, %25
%45 = sext i32 %25 to i64
%46 = getelementptr inbounds i32, i32* %7, i64 %45
%47 = tail call i64 @write_pes_header(i32 190, i32 %44, i32 0, i32 0, i32* %46, i32 0, i32 0)
br label %54
48: ; preds = %37
%49 = icmp eq i32 %25, %0
br i1 %49, label %54, label %50
50: ; preds = %48
%51 = load i32, i32* @stderr
%52 = tail call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %25)
%53 = tail call i32 @exit(i32 1)
unreachable
54: ; preds = %43, %48, %15, %12
%55 = phi i32 [ 0, %12 ], [ -1, %15 ], [ %0, %48 ], [ %0, %43 ]
ret i32 %55
}