#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 char u_int8_t;
typedef char u_char;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int *, char *, int);
int ntohs(int);
int /*<<< orphan*/ printf(char *, char *, char *);
char *strerror(int /*<<< orphan*/);
void parseH264STAPANAL(u_char **value, int packetDataLength, char *file,
                       FILE *fp, int *writeNALPrefixCode, int *writePacket) {
  ++(*value);
  int nalusize = 0;
  int totalnalusize = 0;
  memcpy(&nalusize, *value, 2);
  nalusize = ntohs(nalusize);
  char pd[4] = {'\0'};
  int bytes = packetDataLength - 3;
  *writeNALPrefixCode = 0;
  *writePacket = 0;
  if (nalusize != 0) {
    *value += 2;
  } else {
    return;
  }
  while (totalnalusize < bytes) {
    totalnalusize = totalnalusize + nalusize;
    {
      (pd)[0] = (u_int8_t)((0x00000001) >> 24);
      (pd)[1] = (u_int8_t)((0x00000001) >> 16);
      (pd)[2] = (u_int8_t)((0x00000001) >> 8);
      (pd)[3] = (u_int8_t)((0x00000001) >> 0);
    };
    fwrite(pd, 1, 4, fp);
    int ret = fwrite(*value, sizeof(u_char), nalusize, fp);
    if (ret < nalusize) {
      printf("[-]Error: Writing data to file %s:%s \n", file, strerror(errno));
      return;
    }
    *value = *value + nalusize;
    nalusize = 0;
    if (totalnalusize < bytes) {
      memcpy(&nalusize, *value, 2);
      nalusize = ntohs(nalusize);
      bytes = bytes - 2;
    }
    if (nalusize != 0) {
      *value = *value + 2;
    } else {
      break;
    }
  }
}


x86 assembly (clang, -O0):
<parseH264STAPANAL>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    -0x34(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    -0x34(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x34(%rbp)
lea    -0x3c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x0,-0x34(%rbp)
je     1205 <parseH264STAPANAL+0xa5>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x2,%rcx
mov    %rcx,(%rax)
jmp    120a <parseH264STAPANAL+0xaa>
jmp    131c <parseH264STAPANAL+0x1bc>
jmp    120f <parseH264STAPANAL+0xaf>
mov    -0x38(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    131c <parseH264STAPANAL+0x1bc>
mov    -0x38(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
movb   $0x0,-0x3c(%rbp)
movb   $0x0,-0x3b(%rbp)
movb   $0x0,-0x3a(%rbp)
movb   $0x1,-0x39(%rbp)
lea    -0x3c(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    12a0 <parseH264STAPANAL+0x140>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xd6a(%rip),%rdi
call   1030 <printf@plt>
jmp    131c <parseH264STAPANAL+0x1bc>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
movslq -0x34(%rbp),%rax
add    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    12f1 <parseH264STAPANAL+0x191>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    -0x34(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    -0x34(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1312 <parseH264STAPANAL+0x1b2>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x2,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1317 <parseH264STAPANAL+0x1b7>
jmp    131c <parseH264STAPANAL+0x1bc>
jmp    120f <parseH264STAPANAL+0xaf>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parseH264STAPANAL>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %r8,%r12
mov    %rcx,%r15
mov    %rdx,0x10(%rsp)
mov    %esi,%ebp
mov    %rdi,%r13
mov    (%rdi),%rsi
add    $0x1,%rsi
mov    %rsi,(%rdi)
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
mov    %eax,0x8(%rsp)
movl   $0x0,(%r12)
movl   $0x0,(%r14)
test   %eax,%eax
je     128a <parseH264STAPANAL+0x13a>
addq   $0x2,0x0(%r13)
cmp    $0x4,%ebp
jl     128a <parseH264STAPANAL+0x13a>
mov    %eax,%ebx
add    $0xfffffffd,%ebp
xor    %r14d,%r14d
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movl   $0x1000000,0xc(%rsp)
01
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r15,%rcx
call   1060 <fwrite@plt>
mov    0x0(%r13),%rdi
mov    0x8(%rsp),%edx
mov    $0x1,%esi
mov    %r15,%rcx
call   1060 <fwrite@plt>
movslq 0x8(%rsp),%rsi
cmp    %esi,%eax
jl     1268 <parseH264STAPANAL+0x118>
add    0x0(%r13),%rsi
add    %ebx,%r14d
mov    %rsi,0x0(%r13)
movl   $0x0,0x8(%rsp)
cmp    %ebp,%r14d
jge    128a <parseH264STAPANAL+0x13a>
lea    0x8(%rsp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
je     128a <parseH264STAPANAL+0x13a>
mov    %eax,%ebx
add    $0xfffffffe,%ebp
addq   $0x2,0x0(%r13)
cmp    %ebp,%r14d
jl     11e0 <parseH264STAPANAL+0x90>
jmp    128a <parseH264STAPANAL+0x13a>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd83(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1030 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"[-]Error: Writing data to file %s:%s \0A\00"
@errno = global i32 0
define void @parseH264STAPANAL(i8** %0, i32 %1, i8* %2, i32* %3, i32* %4, i32* %5) {
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca [4 x i8]
%16 = alloca i32
%17 = alloca i32
store i8** %0, i8*** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32* %3, i32** %10
store i32* %4, i32** %11
store i32* %5, i32** %12
%18 = load i8**, i8*** %7
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %18
store i32 0, i32* %13
store i32 0, i32* %14
%21 = load i8**, i8*** %7
%22 = load i8*, i8** %21
%23 = call i32 @memcpy(i32* %13, i8* %22, i32 2)
%24 = load i32, i32* %13
%25 = call i32 @ntohs(i32 %24)
store i32 %25, i32* %13
%26 = bitcast [4 x i8]* %15 to i8*
%27 = load i32, i32* %8
%28 = sub nsw i32 %27, 3
store i32 %28, i32* %16
%29 = load i32*, i32** %11
store i32 0, i32* %29
%30 = load i32*, i32** %12
store i32 0, i32* %30
%31 = load i32, i32* %13
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %6
%34 = load i8**, i8*** %7
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 2
store i8* %36, i8** %34
br label %38
37: ; preds = %6
br label %95
38: ; preds = %33
br label %39
39: ; preds = %94, %38
%40 = load i32, i32* %14
%41 = load i32, i32* %16
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %95
43: ; preds = %39
%44 = load i32, i32* %14
%45 = load i32, i32* %13
%46 = add nsw i32 %44, %45
store i32 %46, i32* %14
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %15, i64 0, i64 0
store i8 0, i8* %47
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %15, i64 0, i64 1
store i8 0, i8* %48
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %15, i64 0, i64 2
store i8 0, i8* %49
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %15, i64 0, i64 3
store i8 1, i8* %50
%51 = getelementptr inbounds [4 x i8], [4 x i8]* %15, i64 0, i64 0
%52 = load i32*, i32** %10
%53 = call i32 @fwrite(i8* %51, i32 1, i32 4, i32* %52)
%54 = load i8**, i8*** %7
%55 = load i8*, i8** %54
%56 = load i32, i32* %13
%57 = load i32*, i32** %10
%58 = call i32 @fwrite(i8* %55, i32 1, i32 %56, i32* %57)
store i32 %58, i32* %17
%59 = load i32, i32* %17
%60 = load i32, i32* %13
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %67
62: ; preds = %43
%63 = load i8*, i8** %9
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %63, i8* %65)
br label %95
67: ; preds = %43
%68 = load i8**, i8*** %7
%69 = load i8*, i8** %68
%70 = load i32, i32* %13
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i8**, i8*** %7
store i8* %72, i8** %73
store i32 0, i32* %13
%74 = load i32, i32* %14
%75 = load i32, i32* %16
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %85
77: ; preds = %67
%78 = load i8**, i8*** %7
%79 = load i8*, i8** %78
%80 = call i32 @memcpy(i32* %13, i8* %79, i32 2)
%81 = load i32, i32* %13
%82 = call i32 @ntohs(i32 %81)
store i32 %82, i32* %13
%83 = load i32, i32* %16
%84 = sub nsw i32 %83, 2
store i32 %84, i32* %16
br label %85
85: ; preds = %77, %67
%86 = load i32, i32* %13
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %93
88: ; preds = %85
%89 = load i8**, i8*** %7
%90 = load i8*, i8** %89
%91 = getelementptr inbounds i8, i8* %90, i64 2
%92 = load i8**, i8*** %7
store i8* %91, i8** %92
br label %94
93: ; preds = %85
br label %95
94: ; preds = %88
br label %39
95: ; preds = %37, %62, %93, %39
ret void
}

LLVM IR (-O2):
@.str = [39 x i8] c"[-]Error: Writing data to file %s:%s \0A\00"
@errno = global i32 0
define void @parseH264STAPANAL(i8** nocapture %0, i32 %1, i8* %2, i32* nocapture %3, i32* nocapture writeonly %4, i32* nocapture writeonly %5) {
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i32* %8 to [4 x i8]*
%10 = load i8*, i8** %0
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8* %11, i8** %0
%12 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%13 = call i32 @memcpy(i32* %7, i8* %11, i32 2)
%14 = load i32, i32* %7
%15 = call i32 @ntohs(i32 %14)
store i32 %15, i32* %7
%16 = bitcast i32* %8 to i8*
store i32 0, i32* %4
store i32 0, i32* %5
%17 = icmp eq i32 %15, 0
br i1 %17, label %57, label %18
18: ; preds = %6
%19 = load i8*, i8** %0
%20 = getelementptr inbounds i8, i8* %19, i64 2
store i8* %20, i8** %0
%21 = icmp sgt i32 %1, 3
br i1 %21, label %22, label %57
22: ; preds = %18
%23 = add nsw i32 %1, -3
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
br label %31
27: ; preds = %51
%28 = load i8*, i8** %0
%29 = getelementptr inbounds i8, i8* %28, i64 2
store i8* %29, i8** %0
%30 = icmp slt i32 %35, %56
br i1 %30, label %31, label %57
31: ; preds = %22, %27
%32 = phi i32 [ %15, %22 ], [ %54, %27 ]
%33 = phi i32 [ 0, %22 ], [ %35, %27 ]
%34 = phi i32 [ %23, %22 ], [ %56, %27 ]
%35 = add nsw i32 %32, %33
store i8 0, i8* %16
store i8 0, i8* %24
store i8 0, i8* %25
store i8 1, i8* %26
%36 = call i32 @fwrite(i8* %16, i32 1, i32 4, i32* %3)
%37 = load i8*, i8** %0
%38 = load i32, i32* %7
%39 = call i32 @fwrite(i8* %37, i32 1, i32 %38, i32* %3)
%40 = load i32, i32* %7
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %31
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %44)
br label %57
46: ; preds = %31
%47 = load i8*, i8** %0
%48 = sext i32 %40 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %0
store i32 0, i32* %7
%50 = icmp slt i32 %35, %34
br i1 %50, label %51, label %57
51: ; preds = %46
%52 = call i32 @memcpy(i32* %7, i8* %49, i32 2)
%53 = load i32, i32* %7
%54 = call i32 @ntohs(i32 %53)
store i32 %54, i32* %7
%55 = icmp eq i32 %54, 0
%56 = add nsw i32 %34, -2
br i1 %55, label %57, label %27
57: ; preds = %27, %51, %46, %18, %42, %6
ret void
}