#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 /*<<< orphan*/ int64_t;
typedef int int32_t;

/* Variables and functions */
char *bson_skip_field_name(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ strchr(char *, char);
char *bson_next(char *data) {
  unsigned char type = data[0];
  int32_t length;
  if (type == 0) {
    return NULL;
  }
  data = bson_skip_field_name(data + 1);
  switch (type) {
  case 0x01:
    return data + sizeof(double);
  case 0x02:
  case 0x0D:
  case 0x0E:
    length = (*(int *)data);
    return data + sizeof(int32_t) + length;
  case 0x03:
  case 0x04:
    length = (*(int *)data);
    return data + length;
  case 0x05:
    length = (*(int *)data);
    return data + sizeof(int32_t) + 1 + length;
  case 0x06:
  case 0x0A:
  case 0xFF:
  case 0x7F:
    return data;
  case 0x07:
    return data + 12;
  case 0x08:
    return data + 1;
  case 0x09:
  case 0x11:
  case 0x12:
    return data + sizeof(int64_t);
  case 0x0B:
    return strchr(data, '\0') + 1;
  case 0x0C:
    length = (*(int *)data);
    return data + sizeof(int32_t) + length + 12;
  case 0x0F:
    exit(-3);
  case 0x10:
    return data + sizeof(int32_t);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<bson_next>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    115f <bson_next+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <bson_skip_field_name@plt>
mov    %rax,-0x10(%rbp)
movzbl -0x11(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x1,%eax
je     12a0 <bson_next+0x170>
jmp    1185 <bson_next+0x55>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     12b1 <bson_next+0x181>
jmp    1196 <bson_next+0x66>
mov    -0x1c(%rbp),%eax
add    $0xfffffffd,%eax
sub    $0x2,%eax
jb     12d2 <bson_next+0x1a2>
jmp    11aa <bson_next+0x7a>
mov    -0x1c(%rbp),%eax
sub    $0x5,%eax
je     12ef <bson_next+0x1bf>
jmp    11bb <bson_next+0x8b>
mov    -0x1c(%rbp),%eax
sub    $0x6,%eax
je     1314 <bson_next+0x1e4>
jmp    11cc <bson_next+0x9c>
mov    -0x1c(%rbp),%eax
sub    $0x7,%eax
je     1321 <bson_next+0x1f1>
jmp    11dd <bson_next+0xad>
mov    -0x1c(%rbp),%eax
sub    $0x8,%eax
je     1332 <bson_next+0x202>
jmp    11ee <bson_next+0xbe>
mov    -0x1c(%rbp),%eax
sub    $0x9,%eax
je     1343 <bson_next+0x213>
jmp    11ff <bson_next+0xcf>
mov    -0x1c(%rbp),%eax
sub    $0xa,%eax
je     1314 <bson_next+0x1e4>
jmp    1210 <bson_next+0xe0>
mov    -0x1c(%rbp),%eax
sub    $0xb,%eax
je     1354 <bson_next+0x224>
jmp    1221 <bson_next+0xf1>
mov    -0x1c(%rbp),%eax
sub    $0xc,%eax
je     136d <bson_next+0x23d>
jmp    1232 <bson_next+0x102>
mov    -0x1c(%rbp),%eax
add    $0xfffffff3,%eax
sub    $0x2,%eax
jb     12b1 <bson_next+0x181>
jmp    1246 <bson_next+0x116>
mov    -0x1c(%rbp),%eax
sub    $0xf,%eax
je     1392 <bson_next+0x262>
jmp    1257 <bson_next+0x127>
mov    -0x1c(%rbp),%eax
sub    $0x10,%eax
je     139c <bson_next+0x26c>
jmp    1268 <bson_next+0x138>
mov    -0x1c(%rbp),%eax
add    $0xffffffef,%eax
sub    $0x2,%eax
jb     1343 <bson_next+0x213>
jmp    127c <bson_next+0x14c>
mov    -0x1c(%rbp),%eax
sub    $0x7f,%eax
je     1314 <bson_next+0x1e4>
jmp    128d <bson_next+0x15d>
mov    -0x1c(%rbp),%eax
sub    $0xff,%eax
je     1314 <bson_next+0x1e4>
jmp    13ad <bson_next+0x27d>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
add    $0x1,%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
add    $0xc,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
mov    $0xfffffffd,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
jmp    13b5 <bson_next+0x285>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bson_next>:
push   %rbx
mov    (%rdi),%bl
test   %bl,%bl
je     115d <bson_next+0x2d>
add    $0x1,%rdi
call   1040 <bson_skip_field_name@plt>
add    $0x1,%bl
cmp    $0x80,%bl
ja     115d <bson_next+0x2d>
movzbl %bl,%ecx
lea    0xeae(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
add    $0x4,%rax
pop    %rbx
ret
movslq (%rax),%rcx
add    %rcx,%rax
add    $0x4,%rax
pop    %rbx
ret
movslq (%rax),%rcx
add    %rcx,%rax
pop    %rbx
ret
add    $0x8,%rax
pop    %rbx
ret
movslq (%rax),%rcx
add    %rcx,%rax
add    $0x5,%rax
pop    %rbx
ret
add    $0xc,%rax
pop    %rbx
ret
add    $0x1,%rax
pop    %rbx
ret
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
add    $0x1,%eax
cltq
pop    %rbx
ret
movslq (%rax),%rcx
add    %rcx,%rax
add    $0x10,%rax
pop    %rbx
ret
mov    $0xfffffffd,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
define i8* @bson_next(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %49, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = tail call i8* @bson_skip_field_name(i8* %5)
switch i8 %2, label %48 [
i8 1, label %7
i8 2, label %9
i8 13, label %9
i8 14, label %9
i8 3, label %15
i8 4, label %15
i8 5, label %20
i8 6, label %49
i8 10, label %49
i8 -1, label %49
i8 127, label %49
i8 7, label %26
i8 8, label %28
i8 9, label %30
i8 17, label %30
i8 18, label %30
i8 11, label %32
i8 12, label %37
i8 15, label %44
i8 16, label %46
]
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %6, i64 8
br label %49
9: ; preds = %4, %4, %4
%10 = bitcast i8* %6 to i32*
%11 = load i32, i32* %10
%12 = getelementptr inbounds i8, i8* %6, i64 4
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds i8, i8* %12, i64 %13
br label %49
15: ; preds = %4, %4
%16 = bitcast i8* %6 to i32*
%17 = load i32, i32* %16
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %6, i64 %18
br label %49
20: ; preds = %4
%21 = bitcast i8* %6 to i32*
%22 = load i32, i32* %21
%23 = getelementptr inbounds i8, i8* %6, i64 5
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
br label %49
26: ; preds = %4
%27 = getelementptr inbounds i8, i8* %6, i64 12
br label %49
28: ; preds = %4
%29 = getelementptr inbounds i8, i8* %6, i64 1
br label %49
30: ; preds = %4, %4, %4
%31 = getelementptr inbounds i8, i8* %6, i64 4
br label %49
32: ; preds = %4
%33 = tail call i32 @strchr(i8* %6, i8 signext 0)
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = inttoptr i64 %35 to i8*
br label %49
37: ; preds = %4
%38 = bitcast i8* %6 to i32*
%39 = load i32, i32* %38
%40 = getelementptr inbounds i8, i8* %6, i64 4
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = getelementptr inbounds i8, i8* %42, i64 12
br label %49
44: ; preds = %4
%45 = tail call i32 @exit(i32 -3)
unreachable
46: ; preds = %4
%47 = getelementptr inbounds i8, i8* %6, i64 4
br label %49
48: ; preds = %4
br label %49
49: ; preds = %4, %4, %4, %4, %1, %48, %46, %37, %32, %30, %28, %26, %20, %15, %9, %7
%50 = phi i8* [ null, %48 ], [ %47, %46 ], [ %43, %37 ], [ %36, %32 ], [ %31, %30 ], [ %29, %28 ], [ %27, %26 ], [ %25, %20 ], [ %19, %15 ], [ %14, %9 ], [ %8, %7 ], [ null, %1 ], [ %6, %4 ], [ %6, %4 ], [ %6, %4 ], [ %6, %4 ]
ret i8* %50
}