#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 serverinfo_find_extension(const unsigned char *serverinfo,
                              size_t serverinfo_length,
                              unsigned int extension_type,
                              const unsigned char **extension_data,
                              size_t *extension_length) {
  *extension_data = NULL;
  *extension_length = 0;
  if (serverinfo == NULL || serverinfo_length == 0)
    return 0;
  for (;;) {
    unsigned int type = 0;
    size_t len = 0;
    if (serverinfo_length == 0)
      return -1;
    if (serverinfo_length < 2)
      return 0;
    type = (serverinfo[0] << 8) + serverinfo[1];
    serverinfo += 2;
    serverinfo_length -= 2;
    if (serverinfo_length < 2)
      return 0;
    len = (serverinfo[0] << 8) + serverinfo[1];
    serverinfo += 2;
    serverinfo_length -= 2;
    if (len > serverinfo_length)
      return 0;
    if (type == extension_type) {
      *extension_data = serverinfo;
      *extension_length = len;
      return 1;
    }
    serverinfo += len;
    serverinfo_length -= len;
  }
  return 0;
}


x86 assembly (clang, -O0):
<serverinfo_find_extension>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1143 <serverinfo_find_extension+0x43>
cmpq   $0x0,-0x18(%rbp)
jne    114f <serverinfo_find_extension+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    1271 <serverinfo_find_extension+0x171>
jmp    1154 <serverinfo_find_extension+0x54>
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117a <serverinfo_find_extension+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <serverinfo_find_extension+0x171>
cmpq   $0x2,-0x18(%rbp)
jae    1191 <serverinfo_find_extension+0x91>
movl   $0x0,-0x4(%rbp)
jmp    1271 <serverinfo_find_extension+0x171>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x2,-0x18(%rbp)
jae    11d7 <serverinfo_find_extension+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    1271 <serverinfo_find_extension+0x171>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1223 <serverinfo_find_extension+0x123>
movl   $0x0,-0x4(%rbp)
jmp    1271 <serverinfo_find_extension+0x171>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1251 <serverinfo_find_extension+0x151>
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1271 <serverinfo_find_extension+0x171>
mov    -0x40(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1154 <serverinfo_find_extension+0x54>
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverinfo_find_extension>:
movq   $0x0,(%rcx)
movq   $0x0,(%r8)
xor    %eax,%eax
test   %rdi,%rdi
je     117a <serverinfo_find_extension+0x7a>
test   %rsi,%rsi
je     117a <serverinfo_find_extension+0x7a>
nopw   0x0(%rax,%rax,1)
test   %rsi,%rsi
je     1169 <serverinfo_find_extension+0x69>
cmp    $0x1,%rsi
je     117a <serverinfo_find_extension+0x7a>
mov    %rsi,%r9
and    $0xfffffffffffffffe,%r9
cmp    $0x2,%r9
je     117a <serverinfo_find_extension+0x7a>
movzwl 0x2(%rdi),%r9d
shl    $0x30,%r9
bswap  %r9
add    $0xfffffffffffffffc,%rsi
sub    %r9,%rsi
jb     117a <serverinfo_find_extension+0x7a>
movzwl (%rdi),%r10d
shl    $0x10,%r10d
bswap  %r10d
add    $0x4,%rdi
cmp    %edx,%r10d
je     116f <serverinfo_find_extension+0x6f>
add    %r9,%rdi
test   %rsi,%rsi
jne    1125 <serverinfo_find_extension+0x25>
mov    $0xffffffff,%eax
ret
mov    %rdi,(%rcx)
mov    %r9,(%r8)
mov    $0x1,%eax
ret

LLVM IR (-O0):
define i32 @serverinfo_find_extension(i8* %0, i64 %1, i32 %2, i8** %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8**
%11 = alloca i64*
%12 = alloca i32
%13 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8** %3, i8*** %10
store i64* %4, i64** %11
%14 = load i8**, i8*** %10
store i8* null, i8** %14
%15 = load i64*, i64** %11
store i64 0, i64* %15
%16 = load i8*, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %5
%19 = load i64, i64* %8
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18, %5
store i32 0, i32* %6
br label %85
22: ; preds = %18
br label %23
23: ; preds = %78, %22
store i32 0, i32* %12
store i64 0, i64* %13
%24 = load i64, i64* %8
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 -1, i32* %6
br label %85
27: ; preds = %23
%28 = load i64, i64* %8
%29 = icmp ult i64 %28, 2
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 0, i32* %6
br label %85
31: ; preds = %27
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = shl i32 %35, 8
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = add nsw i32 %36, %40
store i32 %41, i32* %12
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i64 2
store i8* %43, i8** %7
%44 = load i64, i64* %8
%45 = sub i64 %44, 2
store i64 %45, i64* %8
%46 = load i64, i64* %8
%47 = icmp ult i64 %46, 2
br i1 %47, label %48, label %49
48: ; preds = %31
store i32 0, i32* %6
br label %85
49: ; preds = %31
%50 = load i8*, i8** %7
%51 = getelementptr inbounds i8, i8* %50, i64 0
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = shl i32 %53, 8
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = add nsw i32 %54, %58
%60 = sext i32 %59 to i64
store i64 %60, i64* %13
%61 = load i8*, i8** %7
%62 = getelementptr inbounds i8, i8* %61, i64 2
store i8* %62, i8** %7
%63 = load i64, i64* %8
%64 = sub i64 %63, 2
store i64 %64, i64* %8
%65 = load i64, i64* %13
%66 = load i64, i64* %8
%67 = icmp ugt i64 %65, %66
br i1 %67, label %68, label %69
68: ; preds = %49
store i32 0, i32* %6
br label %85
69: ; preds = %49
%70 = load i32, i32* %12
%71 = load i32, i32* %9
%72 = icmp eq i32 %70, %71
br i1 %72, label %73, label %78
73: ; preds = %69
%74 = load i8*, i8** %7
%75 = load i8**, i8*** %10
store i8* %74, i8** %75
%76 = load i64, i64* %13
%77 = load i64*, i64** %11
store i64 %76, i64* %77
store i32 1, i32* %6
br label %85
78: ; preds = %69
%79 = load i64, i64* %13
%80 = load i8*, i8** %7
%81 = getelementptr inbounds i8, i8* %80, i64 %79
store i8* %81, i8** %7
%82 = load i64, i64* %13
%83 = load i64, i64* %8
%84 = sub i64 %83, %82
store i64 %84, i64* %8
br label %23
85: ; preds = %73, %68, %48, %30, %26, %21
%86 = load i32, i32* %6
ret i32 %86
}

LLVM IR (-O2):
define i32 @serverinfo_find_extension(i8* %0, i64 %1, i32 %2, i8** nocapture writeonly %3, i64* nocapture writeonly %4) {
store i8* null, i8** %3
store i64 0, i64* %4
%6 = icmp eq i8* %0, null
%7 = icmp eq i64 %1, 0
%8 = or i1 %6, %7
br i1 %8, label %42, label %9
9: ; preds = %5, %37
%10 = phi i64 [ %39, %37 ], [ %1, %5 ]
%11 = phi i8* [ %38, %37 ], [ %0, %5 ]
switch i64 %10, label %12 [
i64 0, label %40
i64 1, label %42
]
12: ; preds = %9
%13 = load i8, i8* %11
%14 = zext i8 %13 to i32
%15 = shl nuw nsw i32 %14, 8
%16 = getelementptr inbounds i8, i8* %11, i64 1
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = or i32 %15, %18
%20 = and i64 %10, -2
%21 = icmp eq i64 %20, 2
br i1 %21, label %40, label %22
22: ; preds = %12
%23 = getelementptr inbounds i8, i8* %11, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i64
%26 = shl nuw nsw i64 %25, 8
%27 = getelementptr inbounds i8, i8* %11, i64 3
%28 = load i8, i8* %27
%29 = zext i8 %28 to i64
%30 = or i64 %26, %29
%31 = getelementptr inbounds i8, i8* %11, i64 4
%32 = add i64 %10, -4
%33 = icmp ult i64 %32, %30
br i1 %33, label %40, label %34
34: ; preds = %22
%35 = icmp eq i32 %19, %2
br i1 %35, label %36, label %37
36: ; preds = %34
store i8* %31, i8** %3
store i64 %30, i64* %4
br label %42
37: ; preds = %34
%38 = getelementptr inbounds i8, i8* %31, i64 %30
%39 = sub i64 %32, %30
br label %9
40: ; preds = %9, %12, %22
%41 = phi i32 [ -1, %9 ], [ 0, %12 ], [ 0, %22 ]
br label %42
42: ; preds = %9, %40, %36, %5
%43 = phi i32 [ 0, %5 ], [ 1, %36 ], [ %41, %40 ], [ 0, %9 ]
ret i32 %43
}