#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*/ uint8_t;
struct stat {
  int st_size;
};
struct rtp_session {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ send_rtp_packet_test(struct rtp_session *, int,
                                        int /*<<< orphan*/ *, size_t, int);
int sum_next_24_bits(int /*<<< orphan*/ *);
int sum_next_32_bits(int /*<<< orphan*/ *);
void read_nal_bitstream_send(struct rtp_session *session, char *filename,
                             int port) {
  FILE *bitstream = fopen(filename, "rb");
  struct stat buf;
  fstat(fileno(bitstream), &buf);
  int file_size = buf.st_size;
  if (bitstream == NULL) {
    printf("Invalid file.\n");
    return;
  }
  const size_t max_nal_buf_size = 50000;
  uint8_t nal_buf[max_nal_buf_size];
  size_t cur_nal_buf_size = 0;
  uint8_t cur_byte;
  memset(nal_buf, 0, sizeof(uint8_t) * max_nal_buf_size);
  int num_bytes_read = 0;
  int total_nal_bytes_sent = 0;
  int num_packets_sent = 0;
  while (num_bytes_read < file_size) {
    int do_send = 0;
    if (sum_next_24_bits(bitstream) == 0x000001) {
      num_bytes_read += 3;
      fseek(bitstream, 3, SEEK_CUR);
      do_send = 1;
    } else if (sum_next_32_bits(bitstream) == 0x00000001) {
      num_bytes_read += 4;
      fseek(bitstream, 4, SEEK_CUR);
      do_send = 1;
    }
    if (do_send && cur_nal_buf_size > 0) {
      send_rtp_packet_test(session, port, nal_buf, cur_nal_buf_size,
                           num_packets_sent);
      total_nal_bytes_sent += cur_nal_buf_size;
      num_packets_sent++;
      cur_nal_buf_size = 0;
    }
    if (num_bytes_read < file_size) {
      fread(&cur_byte, sizeof(uint8_t), 1, bitstream);
      nal_buf[cur_nal_buf_size] = cur_byte;
      cur_nal_buf_size++;
      num_bytes_read++;
    }
  }
  send_rtp_packet_test(session, port, nal_buf, cur_nal_buf_size,
                       num_packets_sent);
  total_nal_bytes_sent += cur_nal_buf_size;
  printf("Total size of all nal units sent: %d\n", total_nal_bytes_sent);
}


x86 assembly (clang, -O0):
<read_nal_bitstream_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   10c0 <fstat@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ff <read_nal_bitstream_send+0x5f>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13ac <read_nal_bitstream_send+0x20c>
movq   $0xc350,-0x38(%rbp)
mov    %rsp,%rax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rdi
add    $0xfffffffffffcf2c0,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
movq   $0x0,-0x48(%rbp)
xor    %esi,%esi
mov    $0x30d40,%edx
call   1060 <memset@plt>
movl   $0x0,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    136e <read_nal_bitstream_send+0x1ce>
movl   $0x0,-0x5c(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <sum_next_24_bits@plt>
cmp    $0x1,%eax
jne    1299 <read_nal_bitstream_send+0xf9>
mov    -0x50(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    $0x3,%esi
call   1090 <fseek@plt>
movl   $0x1,-0x5c(%rbp)
jmp    12d7 <read_nal_bitstream_send+0x137>
mov    -0x20(%rbp),%rdi
call   1040 <sum_next_32_bits@plt>
cmp    $0x1,%eax
jne    12d2 <read_nal_bitstream_send+0x132>
mov    -0x50(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1090 <fseek@plt>
movl   $0x1,-0x5c(%rbp)
jmp    12d7 <read_nal_bitstream_send+0x137>
cmpl   $0x0,-0x5c(%rbp)
je     1323 <read_nal_bitstream_send+0x183>
cmpq   $0x0,-0x48(%rbp)
jbe    1323 <read_nal_bitstream_send+0x183>
mov    -0x68(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x48(%rbp),%rcx
mov    -0x58(%rbp),%r8d
call   1070 <send_rtp_packet_test@plt>
mov    -0x48(%rbp),%rcx
movslq -0x54(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x54(%rbp)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1369 <read_nal_bitstream_send+0x1c9>
mov    -0x20(%rbp),%rcx
lea    -0x4c(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x68(%rbp),%rax
mov    -0x4c(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1248 <read_nal_bitstream_send+0xa8>
mov    -0x68(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x48(%rbp),%rcx
mov    -0x58(%rbp),%r8d
call   1070 <send_rtp_packet_test@plt>
mov    -0x48(%rbp),%rcx
movslq -0x54(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%esi
lea    0xc74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_nal_bitstream_send>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,-0x2c(%rbp)
mov    %rsi,%rax
mov    %rdi,-0x58(%rbp)
lea    0xe2e(%rip),%rsi
mov    %rax,%rdi
call   10c0 <fopen@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <fileno@plt>
lea    -0x48(%rbp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
test   %r15,%r15
je     1304 <read_nal_bitstream_send+0x154>
mov    %rsp,-0x60(%rbp)
mov    -0x48(%rbp),%ebx
mov    %rsp,%r12
add    $0xfffffffffffcf2c0,%r12
mov    %r12,%rsp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x30d40,%edx
call   1070 <memset@plt>
xor    %eax,%eax
test   %ebx,%ebx
jle    131e <read_nal_bitstream_send+0x16e>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
xor    %r13d,%r13d
xor    %r14d,%r14d
mov    %ebx,-0x4c(%rbp)
jmp    1249 <read_nal_bitstream_send+0x99>
nopl   0x0(%rax,%rax,1)
cmp    %ebx,%r13d
jge    132b <read_nal_bitstream_send+0x17b>
mov    %r15,%rdi
call   10b0 <sum_next_24_bits@plt>
mov    $0x3,%esi
cmp    $0x1,%eax
je     1280 <read_nal_bitstream_send+0xd0>
mov    %r15,%rdi
call   1050 <sum_next_32_bits@plt>
mov    $0x4,%esi
cmp    $0x1,%eax
je     1280 <read_nal_bitstream_send+0xd0>
cmp    %ebx,%r13d
jge    1240 <read_nal_bitstream_send+0x90>
jmp    12da <read_nal_bitstream_send+0x12a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %esi,%r13d
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
call   10a0 <fseek@plt>
test   %r14,%r14
je     12ce <read_nal_bitstream_send+0x11e>
mov    -0x58(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %r12,%rdx
mov    %r14,%rcx
mov    %r12,%rbx
mov    -0x38(%rbp),%r12
mov    %r12d,%r8d
call   1080 <send_rtp_packet_test@plt>
mov    -0x40(%rbp),%rax
add    %r14d,%eax
mov    %rax,-0x40(%rbp)
add    $0x1,%r12d
mov    %r12,-0x38(%rbp)
mov    %rbx,%r12
mov    -0x4c(%rbp),%ebx
xor    %r14d,%r14d
cmp    %ebx,%r13d
jge    1240 <read_nal_bitstream_send+0x90>
lea    -0x50(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    -0x50(%rbp),%eax
mov    %eax,(%r12,%r14,4)
add    $0x1,%r14
add    $0x1,%r13d
jmp    1240 <read_nal_bitstream_send+0x90>
lea    0xd1e(%rip),%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
mov    %rax,-0x40(%rbp)
xor    %r14d,%r14d
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %r12,%rdx
mov    %r14,%rcx
mov    -0x38(%rbp),%r8
call   1080 <send_rtp_packet_test@plt>
add    -0x40(%rbp),%r14d
lea    0xcb7(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    -0x60(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rtp_session = type { i32 }
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [15 x i8] c"Invalid file.\0A\00"
@SEEK_CUR = global i32 0
@.str.2 = [38 x i8] c"Total size of all nal units sent: %d\0A\00"
define void @read_nal_bitstream_send(%struct.rtp_session* %0, i8* %1, i32 %2) {
%4 = alloca %struct.rtp_session*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store %struct.rtp_session* %0, %struct.rtp_session** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%18 = load i8*, i8** %5
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = call i32 @fileno(i32* %20)
%22 = call i32 @fstat(i32 %21, %struct.stat* %8)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %9
%25 = load i32*, i32** %7
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %106
29: ; preds = %3
store i64 50000, i64* %10
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %11
%31 = alloca i32, i64 50000
store i64 0, i64* %12
%32 = call i32 @memset(i32* %31, i32 0, i32 200000)
store i32 0, i32* %14
store i32 0, i32* %15
store i32 0, i32* %16
br label %33
33: ; preds = %91, %29
%34 = load i32, i32* %14
%35 = load i32, i32* %9
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %92
37: ; preds = %33
store i32 0, i32* %17
%38 = load i32*, i32** %7
%39 = call i32 @sum_next_24_bits(i32* %38)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i32, i32* %14
%43 = add nsw i32 %42, 3
store i32 %43, i32* %14
%44 = load i32*, i32** %7
%45 = load i32, i32* @SEEK_CUR
%46 = call i32 @fseek(i32* %44, i32 3, i32 %45)
store i32 1, i32* %17
br label %58
47: ; preds = %37
%48 = load i32*, i32** %7
%49 = call i32 @sum_next_32_bits(i32* %48)
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i32, i32* %14
%53 = add nsw i32 %52, 4
store i32 %53, i32* %14
%54 = load i32*, i32** %7
%55 = load i32, i32* @SEEK_CUR
%56 = call i32 @fseek(i32* %54, i32 4, i32 %55)
store i32 1, i32* %17
br label %57
57: ; preds = %51, %47
br label %58
58: ; preds = %57, %41
%59 = load i32, i32* %17
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %77
61: ; preds = %58
%62 = load i64, i64* %12
%63 = icmp ugt i64 %62, 0
br i1 %63, label %64, label %77
64: ; preds = %61
%65 = load %struct.rtp_session*, %struct.rtp_session** %4
%66 = load i32, i32* %6
%67 = load i64, i64* %12
%68 = load i32, i32* %16
%69 = call i32 @send_rtp_packet_test(%struct.rtp_session* %65, i32 %66, i32* %31, i64 %67, i32 %68)
%70 = load i64, i64* %12
%71 = load i32, i32* %15
%72 = sext i32 %71 to i64
%73 = add i64 %72, %70
%74 = trunc i64 %73 to i32
store i32 %74, i32* %15
%75 = load i32, i32* %16
%76 = add nsw i32 %75, 1
store i32 %76, i32* %16
store i64 0, i64* %12
br label %77
77: ; preds = %64, %61, %58
%78 = load i32, i32* %14
%79 = load i32, i32* %9
%80 = icmp slt i32 %78, %79
br i1 %80, label %81, label %91
81: ; preds = %77
%82 = load i32*, i32** %7
%83 = call i32 @fread(i32* %13, i32 4, i32 1, i32* %82)
%84 = load i32, i32* %13
%85 = load i64, i64* %12
%86 = getelementptr inbounds i32, i32* %31, i64 %85
store i32 %84, i32* %86
%87 = load i64, i64* %12
%88 = add i64 %87, 1
store i64 %88, i64* %12
%89 = load i32, i32* %14
%90 = add nsw i32 %89, 1
store i32 %90, i32* %14
br label %91
91: ; preds = %81, %77
br label %33
92: ; preds = %33
%93 = load %struct.rtp_session*, %struct.rtp_session** %4
%94 = load i32, i32* %6
%95 = load i64, i64* %12
%96 = load i32, i32* %16
%97 = call i32 @send_rtp_packet_test(%struct.rtp_session* %93, i32 %94, i32* %31, i64 %95, i32 %96)
%98 = load i64, i64* %12
%99 = load i32, i32* %15
%100 = sext i32 %99 to i64
%101 = add i64 %100, %98
%102 = trunc i64 %101 to i32
store i32 %102, i32* %15
%103 = load i32, i32* %15
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %103)
%105 = load i8*, i8** %11
br label %106
106: ; preds = %92, %27
ret void
}

LLVM IR (-O2):
%struct.rtp_session = type { i32 }
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@SEEK_CUR = global i32 0
@.str.2 = [38 x i8] c"Total size of all nal units sent: %d\0A\00"
@str = [14 x i8] c"Invalid file.\00"
define void @read_nal_bitstream_send(%struct.rtp_session* %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca i32
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = bitcast %struct.stat* %4 to i8*
%8 = tail call i32 @fileno(i32* %6)
%9 = call i32 @fstat(i32 %8, %struct.stat* %4)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp eq i32* %6, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %67
15: ; preds = %3
%16 = tail call i8* @llvm.stacksave()
%17 = alloca [50000 x i32]
%18 = getelementptr inbounds [50000 x i32], [50000 x i32]* %17, i64 0, i64 0
%19 = bitcast i32* %5 to i8*
%20 = call i32 @memset(i32* %18, i32 0, i32 200000)
%21 = icmp sgt i32 %11, 0
br i1 %21, label %22, label %59
22: ; preds = %15, %55
%23 = phi i32 [ %47, %55 ], [ 0, %15 ]
%24 = phi i32 [ %46, %55 ], [ 0, %15 ]
%25 = phi i32 [ %57, %55 ], [ 0, %15 ]
%26 = phi i64 [ %56, %55 ], [ 0, %15 ]
%27 = call i32 @sum_next_24_bits(i32* %6)
%28 = icmp eq i32 %27, 1
br i1 %28, label %32, label %29
29: ; preds = %22
%30 = call i32 @sum_next_32_bits(i32* %6)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %43
32: ; preds = %29, %22
%33 = phi i32 [ 3, %22 ], [ 4, %29 ]
%34 = add nsw i32 %25, %33
%35 = load i32, i32* @SEEK_CUR
%36 = call i32 @fseek(i32* %6, i32 %33, i32 %35)
%37 = icmp eq i64 %26, 0
br i1 %37, label %43, label %38
38: ; preds = %32
%39 = call i32 @send_rtp_packet_test(%struct.rtp_session* %0, i32 %2, i32* %18, i64 %26, i32 %23)
%40 = trunc i64 %26 to i32
%41 = add i32 %24, %40
%42 = add nsw i32 %23, 1
br label %43
43: ; preds = %29, %38, %32
%44 = phi i32 [ %34, %38 ], [ %34, %32 ], [ %25, %29 ]
%45 = phi i64 [ 0, %38 ], [ 0, %32 ], [ %26, %29 ]
%46 = phi i32 [ %41, %38 ], [ %24, %32 ], [ %24, %29 ]
%47 = phi i32 [ %42, %38 ], [ %23, %32 ], [ %23, %29 ]
%48 = icmp slt i32 %44, %11
br i1 %48, label %49, label %55
49: ; preds = %43
%50 = call i32 @fread(i32* %5, i32 4, i32 1, i32* %6)
%51 = load i32, i32* %5
%52 = getelementptr inbounds [50000 x i32], [50000 x i32]* %17, i64 0, i64 %45
store i32 %51, i32* %52
%53 = add i64 %45, 1
%54 = add nsw i32 %44, 1
br label %55
55: ; preds = %49, %43
%56 = phi i64 [ %53, %49 ], [ %45, %43 ]
%57 = phi i32 [ %54, %49 ], [ %44, %43 ]
%58 = icmp slt i32 %57, %11
br i1 %58, label %22, label %59
59: ; preds = %55, %15
%60 = phi i64 [ 0, %15 ], [ %56, %55 ]
%61 = phi i32 [ 0, %15 ], [ %46, %55 ]
%62 = phi i32 [ 0, %15 ], [ %47, %55 ]
%63 = call i32 @send_rtp_packet_test(%struct.rtp_session* %0, i32 %2, i32* %18, i64 %60, i32 %62)
%64 = trunc i64 %60 to i32
%65 = add i32 %61, %64
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %59, %13
ret void
}