#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 scalar_t__ u_int32_t;
typedef int /*<<< orphan*/ ts;
typedef int /*<<< orphan*/ th;
typedef int int32_t;

/* Variables and functions */
scalar_t__ malloc(int);
int read(int, char *, int);
int /*<<< orphan*/ trace(char *, int, ...);
int flv_read_packet(int flv_fd, int *type, u_int32_t *timestamp, char **data,
                    int *size) {
  int ret = 0;
  char th[11];
  char ts[4];
  int32_t data_size = 0;
  u_int32_t time = 0;
  char *pp;
  if ((ret = read(flv_fd, th, sizeof(th))) != sizeof(th)) {
    if (ret == 0) {
      return 100;
    }
    ret = -1;
    trace("read flv tag header failed. ret=%d", ret);
    return ret;
  }
  *type = (int)(th[0] & 0x1F);
  pp = (char *)&data_size;
  pp[2] = th[1];
  pp[1] = th[2];
  pp[0] = th[3];
  pp = (char *)&time;
  pp[2] = th[4];
  pp[1] = th[5];
  pp[0] = th[6];
  pp[3] = th[7];
  *timestamp = time;
  if (data_size <= 0) {
    ret = -1;
    trace("invalid data size. size=%d, ret=%d", data_size, ret);
    return ret;
  }
  *size = data_size;
  *data = (char *)malloc(data_size);
  if ((ret = read(flv_fd, *data, data_size)) != data_size) {
    if (ret == 0) {
      return 100;
    }
    ret = -1;
    trace("read flv tag data failed. size=%d, ret=%d", data_size, ret);
    return ret;
  }
  if ((ret = read(flv_fd, ts, sizeof(ts))) != sizeof(ts)) {
    if (ret == 0) {
      return 100;
    }
    ret = -1;
    trace("read flv tag size failed. ret=%d", ret);
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<flv_read_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x37(%rbp),%rsi
mov    $0xb,%edx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cltq
cmp    $0xb,%rax
je     11ba <flv_read_packet+0x8a>
cmpl   $0x0,-0x2c(%rbp)
jne    1197 <flv_read_packet+0x67>
movl   $0x64,-0x4(%rbp)
jmp    132b <flv_read_packet+0x1fb>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <trace@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <flv_read_packet+0x1fb>
movsbl -0x37(%rbp),%ecx
and    $0x1f,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x36(%rbp),%cl
mov    -0x50(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x35(%rbp),%cl
mov    -0x50(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x34(%rbp),%cl
mov    -0x50(%rbp),%rax
mov    %cl,(%rax)
lea    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x33(%rbp),%cl
mov    -0x50(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x32(%rbp),%cl
mov    -0x50(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x31(%rbp),%cl
mov    -0x50(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%cl
mov    -0x50(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x40(%rbp)
jg     1256 <flv_read_packet+0x126>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x40(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <trace@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <flv_read_packet+0x1fb>
mov    -0x40(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x40(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    -0x40(%rbp),%eax
je     12cb <flv_read_packet+0x19b>
cmpl   $0x0,-0x2c(%rbp)
jne    12a5 <flv_read_packet+0x175>
movl   $0x64,-0x4(%rbp)
jmp    132b <flv_read_packet+0x1fb>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x40(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <trace@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <flv_read_packet+0x1fb>
mov    -0x8(%rbp),%edi
lea    -0x3b(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cltq
cmp    $0x4,%rax
je     1324 <flv_read_packet+0x1f4>
cmpl   $0x0,-0x2c(%rbp)
jne    1301 <flv_read_packet+0x1d1>
movl   $0x64,-0x4(%rbp)
jmp    132b <flv_read_packet+0x1fb>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1050 <trace@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <flv_read_packet+0x1fb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flv_read_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r12
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x9(%rsp),%rsi
mov    $0xb,%edx
call   1030 <read@plt>
mov    $0x64,%r15d
test   %eax,%eax
je     124e <flv_read_packet+0x11e>
cmp    $0xb,%eax
jne    1203 <flv_read_packet+0xd3>
movzbl 0x9(%rsp),%eax
and    $0x1f,%eax
mov    %eax,(%rbx)
movzbl 0xa(%rsp),%eax
movzbl 0xb(%rsp),%ebx
movzbl 0xc(%rsp),%r8d
movzbl 0xd(%rsp),%edx
movzbl 0xe(%rsp),%esi
movzbl 0xf(%rsp),%edi
movzbl 0x10(%rsp),%ecx
shl    $0x18,%rcx
shl    $0x10,%rdx
shl    $0x8,%rsi
or     %rdx,%rsi
or     %rdi,%rsi
or     %rcx,%rsi
mov    %rsi,0x0(%r13)
shl    $0x10,%eax
shl    $0x8,%ebx
or     %eax,%ebx
or     %r8d,%ebx
je     121e <flv_read_packet+0xee>
mov    %ebx,(%r12)
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,(%r14)
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
cmp    %ebx,%eax
jne    122f <flv_read_packet+0xff>
lea    0x14(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <read@plt>
test   %eax,%eax
je     124e <flv_read_packet+0x11e>
cmp    $0x4,%eax
jne    1260 <flv_read_packet+0x130>
xor    %r15d,%r15d
jmp    124e <flv_read_packet+0x11e>
lea    0xdf6(%rip),%rdi
mov    $0xffffffff,%r15d
mov    $0xffffffff,%esi
xor    %eax,%eax
call   1050 <trace@plt>
jmp    124e <flv_read_packet+0x11e>
lea    0xdfe(%rip),%rdi
mov    $0xffffffff,%r15d
xor    %esi,%esi
jmp    1242 <flv_read_packet+0x112>
test   %eax,%eax
je     124e <flv_read_packet+0x11e>
lea    0xe0c(%rip),%rdi
mov    $0xffffffff,%r15d
mov    %ebx,%esi
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1050 <trace@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe09(%rip),%rdi
jmp    120a <flv_read_packet+0xda>

LLVM IR (-O0):
@.str = [35 x i8] c"read flv tag header failed. ret=%d\00"
@.str.1 = [35 x i8] c"invalid data size. size=%d, ret=%d\00"
@.str.2 = [42 x i8] c"read flv tag data failed. size=%d, ret=%d\00"
@.str.3 = [33 x i8] c"read flv tag size failed. ret=%d\00"
define i32 @flv_read_packet(i32 %0, i32* %1, i64* %2, i8** %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64*
%10 = alloca i8**
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [11 x i8]
%14 = alloca [4 x i8]
%15 = alloca i32
%16 = alloca i64
%17 = alloca i8*
store i32 %0, i32* %7
store i32* %1, i32** %8
store i64* %2, i64** %9
store i8** %3, i8*** %10
store i32* %4, i32** %11
store i32 0, i32* %12
store i32 0, i32* %15
store i64 0, i64* %16
%18 = load i32, i32* %7
%19 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 11)
store i32 %20, i32* %12
%21 = sext i32 %20 to i64
%22 = icmp ne i64 %21, 11
br i1 %22, label %23, label %31
23: ; preds = %5
%24 = load i32, i32* %12
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 100, i32* %6
br label %114
27: ; preds = %23
store i32 -1, i32* %12
%28 = load i32, i32* %12
%29 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %12
store i32 %30, i32* %6
br label %114
31: ; preds = %5
%32 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = and i32 %34, 31
%36 = load i32*, i32** %8
store i32 %35, i32* %36
%37 = bitcast i32* %15 to i8*
store i8* %37, i8** %17
%38 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 1
%39 = load i8, i8* %38
%40 = load i8*, i8** %17
%41 = getelementptr inbounds i8, i8* %40, i64 2
store i8 %39, i8* %41
%42 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 2
%43 = load i8, i8* %42
%44 = load i8*, i8** %17
%45 = getelementptr inbounds i8, i8* %44, i64 1
store i8 %43, i8* %45
%46 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 3
%47 = load i8, i8* %46
%48 = load i8*, i8** %17
%49 = getelementptr inbounds i8, i8* %48, i64 0
store i8 %47, i8* %49
%50 = bitcast i64* %16 to i8*
store i8* %50, i8** %17
%51 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 4
%52 = load i8, i8* %51
%53 = load i8*, i8** %17
%54 = getelementptr inbounds i8, i8* %53, i64 2
store i8 %52, i8* %54
%55 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 5
%56 = load i8, i8* %55
%57 = load i8*, i8** %17
%58 = getelementptr inbounds i8, i8* %57, i64 1
store i8 %56, i8* %58
%59 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 6
%60 = load i8, i8* %59
%61 = load i8*, i8** %17
%62 = getelementptr inbounds i8, i8* %61, i64 0
store i8 %60, i8* %62
%63 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 7
%64 = load i8, i8* %63
%65 = load i8*, i8** %17
%66 = getelementptr inbounds i8, i8* %65, i64 3
store i8 %64, i8* %66
%67 = load i64, i64* %16
%68 = load i64*, i64** %9
store i64 %67, i64* %68
%69 = load i32, i32* %15
%70 = icmp sle i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %31
store i32 -1, i32* %12
%72 = load i32, i32* %15
%73 = load i32, i32* %12
%74 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %72, i32 %73)
%75 = load i32, i32* %12
store i32 %75, i32* %6
br label %114
76: ; preds = %31
%77 = load i32, i32* %15
%78 = load i32*, i32** %11
store i32 %77, i32* %78
%79 = load i32, i32* %15
%80 = call i64 @malloc(i32 %79)
%81 = inttoptr i64 %80 to i8*
%82 = load i8**, i8*** %10
store i8* %81, i8** %82
%83 = load i32, i32* %7
%84 = load i8**, i8*** %10
%85 = load i8*, i8** %84
%86 = load i32, i32* %15
%87 = call i32 @read(i32 %83, i8* %85, i32 %86)
store i32 %87, i32* %12
%88 = load i32, i32* %15
%89 = icmp ne i32 %87, %88
br i1 %89, label %90, label %99
90: ; preds = %76
%91 = load i32, i32* %12
%92 = icmp eq i32 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %90
store i32 100, i32* %6
br label %114
94: ; preds = %90
store i32 -1, i32* %12
%95 = load i32, i32* %15
%96 = load i32, i32* %12
%97 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %95, i32 %96)
%98 = load i32, i32* %12
store i32 %98, i32* %6
br label %114
99: ; preds = %76
%100 = load i32, i32* %7
%101 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%102 = call i32 @read(i32 %100, i8* %101, i32 4)
store i32 %102, i32* %12
%103 = sext i32 %102 to i64
%104 = icmp ne i64 %103, 4
br i1 %104, label %105, label %113
105: ; preds = %99
%106 = load i32, i32* %12
%107 = icmp eq i32 %106, 0
br i1 %107, label %108, label %109
108: ; preds = %105
store i32 100, i32* %6
br label %114
109: ; preds = %105
store i32 -1, i32* %12
%110 = load i32, i32* %12
%111 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %110)
%112 = load i32, i32* %12
store i32 %112, i32* %6
br label %114
113: ; preds = %99
store i32 0, i32* %6
br label %114
114: ; preds = %113, %109, %108, %94, %93, %71, %27, %26
%115 = load i32, i32* %6
ret i32 %115
}

LLVM IR (-O2):
@.str = [35 x i8] c"read flv tag header failed. ret=%d\00"
@.str.1 = [35 x i8] c"invalid data size. size=%d, ret=%d\00"
@.str.2 = [42 x i8] c"read flv tag data failed. size=%d, ret=%d\00"
@.str.3 = [33 x i8] c"read flv tag size failed. ret=%d\00"
define i32 @flv_read_packet(i32 %0, i32* nocapture writeonly %1, i64* nocapture writeonly %2, i8** nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = alloca [11 x i8]
%7 = alloca [4 x i8]
%8 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%10 = call i32 @read(i32 %0, i8* %8, i32 11)
switch i32 %10, label %11 [
i32 11, label %13
i32 0, label %65
]
11: ; preds = %5
%12 = tail call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 -1)
br label %65
13: ; preds = %5
%14 = load i8, i8* %8
%15 = and i8 %14, 31
%16 = zext i8 %15 to i32
store i32 %16, i32* %1
%17 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 1
%18 = load i8, i8* %17
%19 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 2
%20 = load i8, i8* %19
%21 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 3
%22 = load i8, i8* %21
%23 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 4
%24 = load i8, i8* %23
%25 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 5
%26 = load i8, i8* %25
%27 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 6
%28 = load i8, i8* %27
%29 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 7
%30 = load i8, i8* %29
%31 = zext i8 %30 to i64
%32 = shl nuw nsw i64 %31, 24
%33 = zext i8 %24 to i64
%34 = shl nuw nsw i64 %33, 16
%35 = zext i8 %26 to i64
%36 = shl nuw nsw i64 %35, 8
%37 = zext i8 %28 to i64
%38 = or i64 %36, %34
%39 = or i64 %38, %37
%40 = or i64 %39, %32
store i64 %40, i64* %2
%41 = zext i8 %18 to i32
%42 = shl nuw nsw i32 %41, 16
%43 = zext i8 %20 to i32
%44 = shl nuw nsw i32 %43, 8
%45 = or i32 %44, %42
%46 = zext i8 %22 to i32
%47 = or i32 %45, %46
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %13
%50 = tail call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 -1)
br label %65
51: ; preds = %13
store i32 %47, i32* %4
%52 = tail call i64 @malloc(i32 %47)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %3
%54 = tail call i32 @read(i32 %0, i8* %53, i32 %47)
%55 = icmp eq i32 %54, %47
br i1 %55, label %60, label %56
56: ; preds = %51
%57 = icmp eq i32 %54, 0
br i1 %57, label %65, label %58
58: ; preds = %56
%59 = tail call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %47, i32 -1)
br label %65
60: ; preds = %51
%61 = call i32 @read(i32 %0, i8* %9, i32 4)
switch i32 %61, label %62 [
i32 4, label %64
i32 0, label %65
]
62: ; preds = %60
%63 = tail call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 -1)
br label %65
64: ; preds = %60
br label %65
65: ; preds = %60, %56, %5, %64, %62, %58, %49, %11
%66 = phi i32 [ -1, %11 ], [ -1, %49 ], [ -1, %58 ], [ -1, %62 ], [ 0, %64 ], [ 100, %5 ], [ 100, %56 ], [ 100, %60 ]
ret i32 %66
}