#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 zl;
typedef int /*<<< orphan*/ uint32_t;
typedef unsigned int uint16_t;

/* Variables and functions */
unsigned int UINT16_MAX;
int /*<<< orphan*/ zipRawEntryLength(unsigned char *);
unsigned int ziplistLen(unsigned char *zl) {
  unsigned int len = 0;
  if ((*((uint16_t *)((zl) + sizeof(uint32_t) * 2))) < UINT16_MAX) {
    len = (*((uint16_t *)((zl) + sizeof(uint32_t) * 2)));
  } else {
    unsigned char *p = zl + (sizeof(uint32_t) * 2 + sizeof(uint16_t));
    while (*p != 255) {
      p += zipRawEntryLength(p);
      len++;
    }
    if (len < UINT16_MAX)
      (*((uint16_t *)((zl) + sizeof(uint32_t) * 2))) = len;
  }
  return len;
}


x86 assembly (clang, -O0):
<ziplistLen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2eaf(%rip),%rcx
cmp    (%rcx),%eax
jae    1148 <ziplistLen+0x38>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    11ae <ziplistLen+0x9e>
mov    -0x8(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xff,%eax
je     118d <ziplistLen+0x7d>
mov    -0x18(%rbp),%rdi
call   1030 <zipRawEntryLength@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1154 <ziplistLen+0x44>
mov    -0xc(%rbp),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
jae    11a9 <ziplistLen+0x99>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
jmp    11ae <ziplistLen+0x9e>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ziplistLen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rdi),%ebp
mov    0x2ebf(%rip),%r15
mov    (%r15),%eax
cmp    %eax,%ebp
jb     1162 <ziplistLen+0x52>
mov    %rdi,%r14
cmpb   $0xff,0xc(%rdi)
je     116f <ziplistLen+0x5f>
lea    0xc(%r14),%rbx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <zipRawEntryLength@plt>
cltq
add    $0x1,%ebp
cmpb   $0xff,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rbx
jne    1140 <ziplistLen+0x30>
mov    (%r15),%eax
cmp    %eax,%ebp
jae    1162 <ziplistLen+0x52>
mov    %ebp,0x8(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
cmp    %eax,%ebp
jb     115e <ziplistLen+0x4e>
jmp    1162 <ziplistLen+0x52>

LLVM IR (-O0):
@UINT16_MAX = global i32 0
define i32 @ziplistLen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
%5 = load i8*, i8** %2
%6 = getelementptr inbounds i8, i8* %5, i64 8
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
%9 = load i32, i32* @UINT16_MAX
%10 = icmp ult i32 %8, %9
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = getelementptr inbounds i8, i8* %12, i64 8
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
store i32 %15, i32* %3
br label %42
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = getelementptr inbounds i8, i8* %17, i64 12
store i8* %18, i8** %4
br label %19
19: ; preds = %24, %16
%20 = load i8*, i8** %4
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp ne i32 %22, 255
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = call i32 @zipRawEntryLength(i8* %25)
%27 = load i8*, i8** %4
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %4
%30 = load i32, i32* %3
%31 = add i32 %30, 1
store i32 %31, i32* %3
br label %19
32: ; preds = %19
%33 = load i32, i32* %3
%34 = load i32, i32* @UINT16_MAX
%35 = icmp ult i32 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* %3
%38 = load i8*, i8** %2
%39 = getelementptr inbounds i8, i8* %38, i64 8
%40 = bitcast i8* %39 to i32*
store i32 %37, i32* %40
br label %41
41: ; preds = %36, %32
br label %42
42: ; preds = %41, %11
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@UINT16_MAX = global i32 0
define i32 @ziplistLen(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 8
%3 = bitcast i8* %2 to i32*
%4 = load i32, i32* %3
%5 = load i32, i32* @UINT16_MAX
%6 = icmp ult i32 %4, %5
br i1 %6, label %27, label %7
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %0, i64 12
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, -1
br i1 %10, label %22, label %11
11: ; preds = %7, %11
%12 = phi i8* [ %16, %11 ], [ %8, %7 ]
%13 = phi i32 [ %17, %11 ], [ 0, %7 ]
%14 = tail call i32 @zipRawEntryLength(i8* %12)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = add i32 %13, 1
%18 = load i8, i8* %16
%19 = icmp eq i8 %18, -1
br i1 %19, label %20, label %11
20: ; preds = %11
%21 = load i32, i32* @UINT16_MAX
br label %22
22: ; preds = %20, %7
%23 = phi i32 [ %5, %7 ], [ %21, %20 ]
%24 = phi i32 [ 0, %7 ], [ %17, %20 ]
%25 = icmp ult i32 %24, %23
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 %24, i32* %3
br label %27
27: ; preds = %22, %26, %1
%28 = phi i32 [ %4, %1 ], [ %24, %26 ], [ %24, %22 ]
ret i32 %28
}