#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 */
void *bson_get_current(char *, char **, int *);
char *bson_next(char *);
char *strdup(char *);
int bson_array_find_next_string(char **buffer, char **field, char **data) {
  char *read_field;
  int read_type;
  void *return_data;
  return_data = bson_get_current(*buffer, &read_field, &read_type);
  if (read_type == 0x02) {
    *data = (char *)return_data + 4;
    if (field) {
      *field = strdup(read_field);
    }
  }
  *buffer = bson_next(*buffer);
  return *buffer == NULL ? 0 : 1;
}


x86 assembly (clang, -O0):
<bson_array_find_next_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <bson_get_current@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x24(%rbp)
jne    1198 <bson_array_find_next_string+0x68>
mov    -0x30(%rbp),%rcx
add    $0x4,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1193 <bson_array_find_next_string+0x63>
mov    -0x20(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1198 <bson_array_find_next_string+0x68>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <bson_next@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bson_array_find_next_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1040 <bson_get_current@plt>
cmpl   $0x2,0x4(%rsp)
jne    1174 <bson_array_find_next_string+0x44>
add    $0x4,%rax
mov    %rax,(%r15)
test   %r14,%r14
je     1174 <bson_array_find_next_string+0x44>
mov    0x8(%rsp),%rdi
call   1050 <strdup@plt>
mov    %rax,(%r14)
mov    (%rbx),%rdi
call   1030 <bson_next@plt>
mov    %rax,(%rbx)
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @bson_array_find_next_string(i8** %0, i8** %1, i8** %2) {
%4 = alloca i8**
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%10 = load i8**, i8*** %4
%11 = load i8*, i8** %10
%12 = call i8* @bson_get_current(i8* %11, i8** %7, i32* %8)
store i8* %12, i8** %9
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %26
15: ; preds = %3
%16 = load i8*, i8** %9
%17 = getelementptr inbounds i8, i8* %16, i64 4
%18 = load i8**, i8*** %6
store i8* %17, i8** %18
%19 = load i8**, i8*** %5
%20 = icmp ne i8** %19, null
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i8*, i8** %7
%23 = call i8* @strdup(i8* %22)
%24 = load i8**, i8*** %5
store i8* %23, i8** %24
br label %25
25: ; preds = %21, %15
br label %26
26: ; preds = %25, %3
%27 = load i8**, i8*** %4
%28 = load i8*, i8** %27
%29 = call i8* @bson_next(i8* %28)
%30 = load i8**, i8*** %4
store i8* %29, i8** %30
%31 = load i8**, i8*** %4
%32 = load i8*, i8** %31
%33 = icmp eq i8* %32, null
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 0, i32 1
ret i32 %35
}

LLVM IR (-O2):
define i32 @bson_array_find_next_string(i8** nocapture %0, i8** writeonly %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast i8** %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = load i8*, i8** %0
%9 = call i8* @bson_get_current(i8* %8, i8** %4, i32* %5)
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = getelementptr inbounds i8, i8* %9, i64 4
store i8* %13, i8** %2
%14 = icmp eq i8** %1, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %1
br label %18
18: ; preds = %12, %15, %3
%19 = load i8*, i8** %0
%20 = call i8* @bson_next(i8* %19)
store i8* %20, i8** %0
%21 = icmp ne i8* %20, null
%22 = zext i1 %21 to i32
ret i32 %22
}