#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 */
struct index_external {
  int dummy;
};
struct index {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ bnode_insert_string(struct index_external *, int, long,
                                       char const *);
int /*<<< orphan*/
index_external_append_offset_did_pair(struct index_external *, long, int);
int /*<<< orphan*/ index_external_append_string(struct index_external *,
                                                char const *, long *);
int strlen(char const *);
int index_external_put_document(struct index *index, int did,
                                const char *text) {
  struct index_external *idx;
  int i;
  int l = strlen(text);
  long offset;
  idx = (struct index_external *)index;
  if (!index_external_append_string(idx, text, &offset))
    return 0;
  if (!index_external_append_offset_did_pair(idx, offset, did))
    return 0;
  for (i = 0; i < l; i++) {
    if (!(bnode_insert_string((struct index_external *)index, did, offset + i,
                              text + i)))
      return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<index_external_put_document>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <index_external_append_string@plt>
cmp    $0x0,%eax
jne    118d <index_external_put_document+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    1216 <index_external_put_document+0xd6>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <index_external_append_offset_did_pair@plt>
cmp    $0x0,%eax
jne    11b2 <index_external_put_document+0x72>
movl   $0x0,-0x4(%rbp)
jmp    1216 <index_external_put_document+0xd6>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    120f <index_external_put_document+0xcf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rdx
movslq -0x2c(%rbp),%rax
add    %rax,%rdx
mov    -0x20(%rbp),%rcx
movslq -0x2c(%rbp),%rax
add    %rax,%rcx
call   1050 <bnode_insert_string@plt>
cmp    $0x0,%eax
jne    11fc <index_external_put_document+0xbc>
movl   $0x0,-0x4(%rbp)
jmp    1216 <index_external_put_document+0xd6>
jmp    1201 <index_external_put_document+0xc1>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b9 <index_external_put_document+0x79>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<index_external_put_document>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%r13
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %rsp,%rdx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1030 <index_external_append_string@plt>
xor    %r15d,%r15d
test   %eax,%eax
je     11c8 <index_external_put_document+0x88>
mov    (%rsp),%rsi
mov    %r13,%rdi
mov    %r14d,%edx
call   1060 <index_external_append_offset_did_pair@plt>
test   %eax,%eax
je     11c8 <index_external_put_document+0x88>
mov    $0x1,%r15d
test   %r12d,%r12d
jle    11c8 <index_external_put_document+0x88>
mov    %r12d,%r12d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%rsp),%rdx
add    %rbp,%rdx
lea    (%rbx,%rbp,1),%rcx
mov    %r13,%rdi
mov    %r14d,%esi
call   1050 <bnode_insert_string@plt>
test   %eax,%eax
je     11c5 <index_external_put_document+0x85>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    11a0 <index_external_put_document+0x60>
jmp    11c8 <index_external_put_document+0x88>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.index = type { i32 }
%struct.index_external = type { i32 }
define i32 @index_external_put_document(%struct.index* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.index*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.index_external*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store %struct.index* %0, %struct.index** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%12 = load i8*, i8** %7
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %10
%14 = load %struct.index*, %struct.index** %5
%15 = bitcast %struct.index* %14 to %struct.index_external*
store %struct.index_external* %15, %struct.index_external** %8
%16 = load %struct.index_external*, %struct.index_external** %8
%17 = load i8*, i8** %7
%18 = call i32 @index_external_append_string(%struct.index_external* %16, i8* %17, i64* %11)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %3
store i32 0, i32* %4
br label %53
21: ; preds = %3
%22 = load %struct.index_external*, %struct.index_external** %8
%23 = load i64, i64* %11
%24 = load i32, i32* %6
%25 = call i32 @index_external_append_offset_did_pair(%struct.index_external* %22, i64 %23, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %21
store i32 0, i32* %4
br label %53
28: ; preds = %21
store i32 0, i32* %9
br label %29
29: ; preds = %49, %28
%30 = load i32, i32* %9
%31 = load i32, i32* %10
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %52
33: ; preds = %29
%34 = load %struct.index*, %struct.index** %5
%35 = bitcast %struct.index* %34 to %struct.index_external*
%36 = load i32, i32* %6
%37 = load i64, i64* %11
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = add nsw i64 %37, %39
%41 = load i8*, i8** %7
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = call i32 @bnode_insert_string(%struct.index_external* %35, i32 %36, i64 %40, i8* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %33
store i32 0, i32* %4
br label %53
48: ; preds = %33
br label %49
49: ; preds = %48
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %29
52: ; preds = %29
store i32 1, i32* %4
br label %53
53: ; preds = %52, %47, %27, %20
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
%struct.index = type { i32 }
%struct.index_external = type { i32 }
define i32 @index_external_put_document(%struct.index* %0, i32 %1, i8* %2) {
%4 = alloca i64
%5 = tail call i32 @strlen(i8* %2)
%6 = bitcast i64* %4 to i8*
%7 = bitcast %struct.index* %0 to %struct.index_external*
%8 = call i32 @index_external_append_string(%struct.index_external* %7, i8* %2, i64* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %28, label %10
10: ; preds = %3
%11 = load i64, i64* %4
%12 = call i32 @index_external_append_offset_did_pair(%struct.index_external* %7, i64 %11, i32 %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %10
%15 = icmp sgt i32 %5, 0
br i1 %15, label %16, label %28
16: ; preds = %14
%17 = zext i32 %5 to i64
br label %20
18: ; preds = %20
%19 = icmp eq i64 %27, %17
br i1 %19, label %28, label %20
20: ; preds = %16, %18
%21 = phi i64 [ 0, %16 ], [ %27, %18 ]
%22 = load i64, i64* %4
%23 = add nsw i64 %22, %21
%24 = getelementptr inbounds i8, i8* %2, i64 %21
%25 = call i32 @bnode_insert_string(%struct.index_external* %7, i32 %1, i64 %23, i8* %24)
%26 = icmp eq i32 %25, 0
%27 = add nuw nsw i64 %21, 1
br i1 %26, label %28, label %18
28: ; preds = %20, %18, %14, %10, %3
%29 = phi i32 [ 0, %3 ], [ 0, %10 ], [ 1, %14 ], [ 0, %20 ], [ 1, %18 ]
ret i32 %29
}