#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 */

/* Variables and functions */
int ntohs(unsigned short);
int block_haschildren(unsigned char *buf, int len) {
  int pos = 0;
  while (pos < len) {
    int l = ntohs(*(unsigned short *)(buf + 1 + pos));
    if (l == 0)
      break;
    pos += l + 3;
  }
  if (pos == len)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<block_haschildren>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1167 <block_haschildren+0x57>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzwl 0x1(%rax,%rcx,1),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1156 <block_haschildren+0x46>
jmp    1167 <block_haschildren+0x57>
mov    -0x1c(%rbp),%eax
add    $0x3,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <block_haschildren+0x16>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    117f <block_haschildren+0x6f>
movl   $0x1,-0x4(%rbp)
jmp    1186 <block_haschildren+0x76>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_haschildren>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
mov    %ecx,%ebx
cmp    %ebp,%ecx
jge    113e <block_haschildren+0x2e>
movslq %ebx,%rax
movzwl 0x1(%r14,%rax,1),%edi
call   1030 <ntohs@plt>
lea    (%rbx,%rax,1),%ecx
add    $0x3,%ecx
test   %eax,%eax
jne    1120 <block_haschildren+0x10>
xor    %eax,%eax
cmp    %ebp,%ebx
sete   %al
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @block_haschildren(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %24, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %29
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = bitcast i8* %17 to i16*
%19 = load i16, i16* %18
%20 = call i32 @ntohs(i16 zeroext %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %12
br label %29
24: ; preds = %12
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 3
%27 = load i32, i32* %6
%28 = add nsw i32 %27, %26
store i32 %28, i32* %6
br label %8
29: ; preds = %23, %8
%30 = load i32, i32* %6
%31 = load i32, i32* %5
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 1, i32* %3
br label %35
34: ; preds = %29
store i32 0, i32* %3
br label %35
35: ; preds = %34, %33
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @block_haschildren(i8* nocapture %0, i32 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 1
br label %4
4: ; preds = %7, %2
%5 = phi i32 [ 0, %2 ], [ %15, %7 ]
%6 = icmp slt i32 %5, %1
br i1 %6, label %7, label %16
7: ; preds = %4
%8 = sext i32 %5 to i64
%9 = getelementptr inbounds i8, i8* %3, i64 %8
%10 = bitcast i8* %9 to i16*
%11 = load i16, i16* %10
%12 = tail call i32 @ntohs(i16 zeroext %11)
%13 = icmp eq i32 %12, 0
%14 = add i32 %5, 3
%15 = add i32 %14, %12
br i1 %13, label %16, label %4
16: ; preds = %7, %4
%17 = icmp eq i32 %5, %1
%18 = zext i1 %17 to i32
ret i32 %18
}