#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 jpeg_parser {
  int dummy;
};

/* Variables and functions */
scalar_t__ jpeg_parser_get_APP4_marker(struct jpeg_parser *, unsigned char **,
                                       int *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
int vp8_paylod_parser(struct jpeg_parser *handle, unsigned char *out_buffer,
                      unsigned char **header) {
  unsigned char *marker;
  int marker_length;
  if (jpeg_parser_get_APP4_marker(handle, &marker, &marker_length) == 0) {
    printf("ERROR: jpeg_parser_get_APP4_marker");
    return -1;
  }
  if (marker_length < 26) {
    printf("ERROR: APP4 marker size shall > 26");
    return -1;
  }
  *header = marker;
  int version = (marker[0] << 8) | marker[1];
  int header_size = (marker[2] << 8) | marker[3];
  int payload_size =
      (marker[header_size] << 24) | (marker[header_size + 1] << 16) |
      (marker[header_size + 2] << 8) | (marker[header_size + 3] << 0);
  if (version != 0x0100) {
    return -1;
  }
  if (payload_size > (512 * 1024)) {
    printf("ERROR: frame too large");
    return -1;
  }
  int received_size = marker_length - header_size - 4;
  memcpy(out_buffer, &marker[header_size + 4], received_size);
  while (received_size < payload_size) {
    if (jpeg_parser_get_APP4_marker(handle, &marker, &marker_length) == 0) {
      printf("ERROR: received data size(%d) < expected size(%d)", received_size,
             payload_size);
      return -1;
    }
    memcpy(out_buffer + received_size, marker, marker_length);
    received_size += marker_length;
  }
  if (received_size != payload_size) {
    printf("ERROR: received data size(%d) != expected size(%d)", received_size,
           payload_size);
    return -1;
  }
  return payload_size;
}


x86 assembly (clang, -O0):
<vp8_paylod_parser>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1030 <jpeg_parser_get_APP4_marker@plt>
cmp    $0x0,%rax
jne    1179 <vp8_paylod_parser+0x49>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <vp8_paylod_parser+0x200>
cmpl   $0x1a,-0x2c(%rbp)
jge    119d <vp8_paylod_parser+0x6d>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <vp8_paylod_parser+0x200>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x28(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movzbl 0x2(%rax),%eax
shl    $0x8,%eax
mov    -0x28(%rbp),%rcx
movzbl 0x3(%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
shl    $0x18,%eax
mov    -0x28(%rbp),%rcx
mov    -0x34(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x28(%rbp),%rcx
mov    -0x34(%rbp),%edx
add    $0x2,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x28(%rbp),%rcx
mov    -0x34(%rbp),%edx
add    $0x3,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
shl    $0x0,%ecx
or     %ecx,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x100,-0x30(%rbp)
je     1244 <vp8_paylod_parser+0x114>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <vp8_paylod_parser+0x200>
cmpl   $0x80000,-0x38(%rbp)
jle    126b <vp8_paylod_parser+0x13b>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <vp8_paylod_parser+0x200>
mov    -0x2c(%rbp),%eax
sub    -0x34(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%eax
add    $0x4,%eax
cltq
add    %rax,%rsi
mov    -0x3c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    12fe <vp8_paylod_parser+0x1ce>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1030 <jpeg_parser_get_APP4_marker@plt>
cmp    $0x0,%rax
jne    12d9 <vp8_paylod_parser+0x1a9>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%edx
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <vp8_paylod_parser+0x200>
mov    -0x18(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x2c(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1292 <vp8_paylod_parser+0x162>
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
je     132a <vp8_paylod_parser+0x1fa>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%edx
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <vp8_paylod_parser+0x200>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vp8_paylod_parser>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r13
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1030 <jpeg_parser_get_APP4_marker@plt>
test   %rax,%rax
je     116d <vp8_paylod_parser+0x3d>
mov    0xc(%rsp),%ebx
cmp    $0x19,%ebx
jg     1193 <vp8_paylod_parser+0x63>
lea    0xeb8(%rip),%rdi
jmp    1174 <vp8_paylod_parser+0x44>
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%rax
mov    %rax,0x0(%rbp)
movzwl (%rax),%ecx
shl    $0x10,%ecx
bswap  %ecx
mov    $0xffffffff,%r14d
cmp    $0x100,%ecx
jne    1181 <vp8_paylod_parser+0x51>
movzwl 0x2(%rax),%ecx
shl    $0x30,%rcx
bswap  %rcx
mov    (%rax,%rcx,1),%r12d
bswap  %r12d
cmp    $0x80001,%r12d
jl     11dd <vp8_paylod_parser+0xad>
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1181 <vp8_paylod_parser+0x51>
sub    %ecx,%ebx
add    $0xfffffffc,%ebx
mov    %ecx,%ecx
lea    (%rcx,%rax,1),%rsi
add    $0x4,%rsi
mov    %r15,%rdi
mov    %ebx,%edx
call   1050 <memcpy@plt>
cmp    %r12d,%ebx
jge    1233 <vp8_paylod_parser+0x103>
lea    0xc(%rsp),%rbp
mov    %r13,%rdi
lea    0x10(%rsp),%rsi
mov    %rbp,%rdx
call   1030 <jpeg_parser_get_APP4_marker@plt>
test   %rax,%rax
je     1249 <vp8_paylod_parser+0x119>
movslq %ebx,%rbx
lea    (%r15,%rbx,1),%rdi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
call   1050 <memcpy@plt>
add    0xc(%rsp),%ebx
cmp    %r12d,%ebx
jl     1200 <vp8_paylod_parser+0xd0>
cmp    %r12d,%ebx
jne    1240 <vp8_paylod_parser+0x110>
mov    %r12d,%r14d
jmp    1181 <vp8_paylod_parser+0x51>
lea    0xe48(%rip),%rdi
jmp    1250 <vp8_paylod_parser+0x120>
lea    0xe0d(%rip),%rdi
mov    %ebx,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1181 <vp8_paylod_parser+0x51>

LLVM IR (-O0):
%struct.jpeg_parser = type { i32 }
@.str = [35 x i8] c"ERROR: jpeg_parser_get_APP4_marker\00"
@.str.1 = [35 x i8] c"ERROR: APP4 marker size shall > 26\00"
@.str.2 = [23 x i8] c"ERROR: frame too large\00"
@.str.3 = [50 x i8] c"ERROR: received data size(%d) < expected size(%d)\00"
@.str.4 = [51 x i8] c"ERROR: received data size(%d) != expected size(%d)\00"
define i32 @vp8_paylod_parser(%struct.jpeg_parser* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca %struct.jpeg_parser*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store %struct.jpeg_parser* %0, %struct.jpeg_parser** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
%14 = load %struct.jpeg_parser*, %struct.jpeg_parser** %5
%15 = call i64 @jpeg_parser_get_APP4_marker(%struct.jpeg_parser* %14, i8** %8, i32* %9)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %135
19: ; preds = %3
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 26
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %135
24: ; preds = %19
%25 = load i8*, i8** %8
%26 = load i8**, i8*** %7
store i8* %25, i8** %26
%27 = load i8*, i8** %8
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = shl i32 %30, 8
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = or i32 %31, %35
store i32 %36, i32* %10
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i64 2
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = shl i32 %40, 8
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i64 3
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = or i32 %41, %45
store i32 %46, i32* %11
%47 = load i8*, i8** %8
%48 = load i32, i32* %11
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = shl i32 %52, 24
%54 = load i8*, i8** %8
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = shl i32 %60, 16
%62 = or i32 %53, %61
%63 = load i8*, i8** %8
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 2
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = shl i32 %69, 8
%71 = or i32 %62, %70
%72 = load i8*, i8** %8
%73 = load i32, i32* %11
%74 = add nsw i32 %73, 3
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %72, i64 %75
%77 = load i8, i8* %76
%78 = zext i8 %77 to i32
%79 = shl i32 %78, 0
%80 = or i32 %71, %79
store i32 %80, i32* %12
%81 = load i32, i32* %10
%82 = icmp ne i32 %81, 256
br i1 %82, label %83, label %84
83: ; preds = %24
store i32 -1, i32* %4
br label %135
84: ; preds = %24
%85 = load i32, i32* %12
%86 = icmp sgt i32 %85, 524288
br i1 %86, label %87, label %89
87: ; preds = %84
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %135
89: ; preds = %84
%90 = load i32, i32* %9
%91 = load i32, i32* %11
%92 = sub nsw i32 %90, %91
%93 = sub nsw i32 %92, 4
store i32 %93, i32* %13
%94 = load i8*, i8** %6
%95 = load i8*, i8** %8
%96 = load i32, i32* %11
%97 = add nsw i32 %96, 4
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds i8, i8* %95, i64 %98
%100 = load i32, i32* %13
%101 = call i32 @memcpy(i8* %94, i8* %99, i32 %100)
br label %102
102: ; preds = %114, %89
%103 = load i32, i32* %13
%104 = load i32, i32* %12
%105 = icmp slt i32 %103, %104
br i1 %105, label %106, label %125
106: ; preds = %102
%107 = load %struct.jpeg_parser*, %struct.jpeg_parser** %5
%108 = call i64 @jpeg_parser_get_APP4_marker(%struct.jpeg_parser* %107, i8** %8, i32* %9)
%109 = icmp eq i64 %108, 0
br i1 %109, label %110, label %114
110: ; preds = %106
%111 = load i32, i32* %13
%112 = load i32, i32* %12
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %111, i32 %112)
store i32 -1, i32* %4
br label %135
114: ; preds = %106
%115 = load i8*, i8** %6
%116 = load i32, i32* %13
%117 = sext i32 %116 to i64
%118 = getelementptr inbounds i8, i8* %115, i64 %117
%119 = load i8*, i8** %8
%120 = load i32, i32* %9
%121 = call i32 @memcpy(i8* %118, i8* %119, i32 %120)
%122 = load i32, i32* %9
%123 = load i32, i32* %13
%124 = add nsw i32 %123, %122
store i32 %124, i32* %13
br label %102
125: ; preds = %102
%126 = load i32, i32* %13
%127 = load i32, i32* %12
%128 = icmp ne i32 %126, %127
br i1 %128, label %129, label %133
129: ; preds = %125
%130 = load i32, i32* %13
%131 = load i32, i32* %12
%132 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %130, i32 %131)
store i32 -1, i32* %4
br label %135
133: ; preds = %125
%134 = load i32, i32* %12
store i32 %134, i32* %4
br label %135
135: ; preds = %133, %129, %110, %87, %83, %22, %17
%136 = load i32, i32* %4
ret i32 %136
}

LLVM IR (-O2):
%struct.jpeg_parser = type { i32 }
@.str = [35 x i8] c"ERROR: jpeg_parser_get_APP4_marker\00"
@.str.1 = [35 x i8] c"ERROR: APP4 marker size shall > 26\00"
@.str.2 = [23 x i8] c"ERROR: frame too large\00"
@.str.3 = [50 x i8] c"ERROR: received data size(%d) < expected size(%d)\00"
@.str.4 = [51 x i8] c"ERROR: received data size(%d) != expected size(%d)\00"
define i32 @vp8_paylod_parser(%struct.jpeg_parser* %0, i8* %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast i8** %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i64 @jpeg_parser_get_APP4_marker(%struct.jpeg_parser* %0, i8** %4, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %92
12: ; preds = %3
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 26
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %92
17: ; preds = %12
%18 = load i8*, i8** %4
store i8* %18, i8** %2
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = shl nuw nsw i32 %20, 8
%22 = getelementptr inbounds i8, i8* %18, i64 1
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = or i32 %21, %24
%26 = getelementptr inbounds i8, i8* %18, i64 2
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = shl nuw nsw i32 %28, 8
%30 = getelementptr inbounds i8, i8* %18, i64 3
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = or i32 %29, %32
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %18, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = shl nuw i32 %37, 24
%39 = add nuw nsw i32 %33, 1
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %18, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = shl nuw nsw i32 %43, 16
%45 = or i32 %44, %38
%46 = add nuw nsw i32 %33, 2
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %18, i64 %47
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = shl nuw nsw i32 %50, 8
%52 = or i32 %45, %51
%53 = add nuw nsw i32 %33, 3
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %18, i64 %54
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = or i32 %52, %57
%59 = icmp eq i32 %25, 256
br i1 %59, label %60, label %92
60: ; preds = %17
%61 = icmp sgt i32 %58, 524288
br i1 %61, label %62, label %64
62: ; preds = %60
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %92
64: ; preds = %60
%65 = sub nsw i32 %13, %33
%66 = add nsw i32 %65, -4
%67 = add nuw nsw i32 %33, 4
%68 = zext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %18, i64 %68
%70 = call i32 @memcpy(i8* %1, i8* %69, i32 %66)
%71 = icmp slt i32 %66, %58
br i1 %71, label %72, label %87
72: ; preds = %64, %78
%73 = phi i32 [ %85, %78 ], [ %66, %64 ]
%74 = call i64 @jpeg_parser_get_APP4_marker(%struct.jpeg_parser* %0, i8** %4, i32* %5)
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %72
%77 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %73, i32 %58)
br label %92
78: ; preds = %72
%79 = sext i32 %73 to i64
%80 = getelementptr inbounds i8, i8* %1, i64 %79
%81 = load i8*, i8** %4
%82 = load i32, i32* %5
%83 = call i32 @memcpy(i8* %80, i8* %81, i32 %82)
%84 = load i32, i32* %5
%85 = add nsw i32 %84, %73
%86 = icmp slt i32 %85, %58
br i1 %86, label %72, label %87
87: ; preds = %78, %64
%88 = phi i32 [ %66, %64 ], [ %85, %78 ]
%89 = icmp eq i32 %88, %58
br i1 %89, label %92, label %90
90: ; preds = %87
%91 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %88, i32 %58)
br label %92
92: ; preds = %62, %17, %87, %90, %76, %15, %10
%93 = phi i32 [ -1, %10 ], [ -1, %15 ], [ -1, %62 ], [ -1, %17 ], [ -1, %76 ], [ -1, %90 ], [ %58, %87 ]
ret i32 %93
}