#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ **elements;
  int /*<<< orphan*/ type;
  scalar_t__ alignment;
  scalar_t__ size;
};
typedef TYPE_1__ ffi_type;

/* Variables and functions */
int /*<<< orphan*/ FFI_TYPE_STRUCT;
int Parrot_str_indexed(int, char *, size_t);
int /*<<< orphan*/ **calloc(size_t, int);
TYPE_1__ *malloc(int);
size_t parse_identifier(int, char *, size_t, size_t, int /*<<< orphan*/ **,
                        int /*<<< orphan*/ *);
size_t structure_length(int, char *, size_t, size_t);
size_t parse_structure(int interp, char *sig, size_t start, size_t sig_length,
                       ffi_type **type_obj, void *pmc_type) {
  size_t i = start;
  size_t struct_len = structure_length(interp, sig, start, sig_length);
  *type_obj = malloc(sizeof(ffi_type));
  (*type_obj)->elements = calloc(struct_len + 1, sizeof(ffi_type *));
  (*type_obj)->size = (*type_obj)->alignment = 0;
  (*type_obj)->type = FFI_TYPE_STRUCT;
  size_t element_counter = 0;
  int c = Parrot_str_indexed(interp, sig, i);
  while (i < sig_length && (c != ')')) {
    i = parse_identifier(interp, sig, i, sig_length,
                         &(*type_obj)->elements[element_counter], NULL);
    element_counter++;
    c = Parrot_str_indexed(interp, sig, i);
  }
  (*type_obj)->elements[struct_len] = NULL;
  return i;
}


x86 assembly (clang, -O0):
<parse_structure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <structure_length@plt>
mov    %rax,-0x40(%rbp)
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movq   $0x0,0x10(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movq   $0x0,0x18(%rax)
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %ecx,0x8(%rax)
movq   $0x0,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <Parrot_str_indexed@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x4d(%rbp)
jae    122b <parse_structure+0xdb>
cmpl   $0x29,-0x4c(%rbp)
setne  %al
mov    %al,-0x4d(%rbp)
mov    -0x4d(%rbp),%al
test   $0x1,%al
jne    123b <parse_structure+0xeb>
jmp    1291 <parse_structure+0x141>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%r8
mov    -0x48(%rbp),%rax
shl    $0x3,%rax
add    %rax,%r8
xor    %eax,%eax
mov    %eax,%r9d
call   1030 <parse_identifier@plt>
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <Parrot_str_indexed@plt>
mov    %eax,-0x4c(%rbp)
jmp    120e <parse_structure+0xbe>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    -0x40(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_structure>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %edi,%ebp
call   1070 <structure_length@plt>
mov    %rax,%r15
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
mov    %r15,%rdi
add    $0x1,%rdi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    (%r14),%rcx
mov    %rax,(%rcx)
mov    (%r14),%rax
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rax)
mov    0x2e32(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rax)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <Parrot_str_indexed@plt>
cmp    %r12,%rbx
mov    %r15,(%rsp)
jae    120a <parse_structure+0xba>
cmp    $0x29,%eax
je     120a <parse_structure+0xba>
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    (%r14),%rax
mov    (%rax),%r8
add    %r15,%r8
mov    %ebp,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
xor    %r9d,%r9d
call   1030 <parse_identifier@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1060 <Parrot_str_indexed@plt>
cmp    %r12,%rbx
jae    120a <parse_structure+0xba>
add    $0x8,%r15
cmp    $0x29,%eax
jne    11d0 <parse_structure+0x80>
mov    (%r14),%rax
mov    (%rax),%rax
mov    (%rsp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32**, i32, i64, i64 }
@FFI_TYPE_STRUCT = global i32 0
define i64 @parse_structure(i32 %0, i8* %1, i64 %2, i64 %3, %struct.TYPE_4__** %4, i8* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca %struct.TYPE_4__**
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store %struct.TYPE_4__** %4, %struct.TYPE_4__*** %11
store i8* %5, i8** %12
%17 = load i64, i64* %9
store i64 %17, i64* %13
%18 = load i32, i32* %7
%19 = load i8*, i8** %8
%20 = load i64, i64* %9
%21 = load i64, i64* %10
%22 = call i64 @structure_length(i32 %18, i8* %19, i64 %20, i64 %21)
store i64 %22, i64* %14
%23 = call %struct.TYPE_4__* @malloc(i32 32)
%24 = load %struct.TYPE_4__**, %struct.TYPE_4__*** %11
store %struct.TYPE_4__* %23, %struct.TYPE_4__** %24
%25 = load i64, i64* %14
%26 = add i64 %25, 1
%27 = call i32** @calloc(i64 %26, i32 8)
%28 = load %struct.TYPE_4__**, %struct.TYPE_4__*** %11
%29 = load %struct.TYPE_4__*, %struct.TYPE_4__** %28
%30 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %29, i32 0, i32 0
store i32** %27, i32*** %30
%31 = load %struct.TYPE_4__**, %struct.TYPE_4__*** %11
%32 = load %struct.TYPE_4__*, %struct.TYPE_4__** %31
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %32, i32 0, i32 2
store i64 0, i64* %33
%34 = load %struct.TYPE_4__**, %struct.TYPE_4__*** %11
%35 = load %struct.TYPE_4__*, %struct.TYPE_4__** %34
%36 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %35, i32 0, i32 3
store i64 0, i64* %36
%37 = load i32, i32* @FFI_TYPE_STRUCT
%38 = load %struct.TYPE_4__**, %struct.TYPE_4__*** %11
%39 = load %struct.TYPE_4__*, %struct.TYPE_4__** %38
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %39, i32 0, i32 1
store i32 %37, i32* %40
store i64 0, i64* %15
%41 = load i32, i32* %7
%42 = load i8*, i8** %8
%43 = load i64, i64* %13
%44 = call i32 @Parrot_str_indexed(i32 %41, i8* %42, i64 %43)
store i32 %44, i32* %16
br label %45
45: ; preds = %54, %6
%46 = load i64, i64* %13
%47 = load i64, i64* %10
%48 = icmp ult i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* %16
%51 = icmp ne i32 %50, 41
br label %52
52: ; preds = %49, %45
%53 = phi i1 [ false, %45 ], [ %51, %49 ]
br i1 %53, label %54, label %72
54: ; preds = %52
%55 = load i32, i32* %7
%56 = load i8*, i8** %8
%57 = load i64, i64* %13
%58 = load i64, i64* %10
%59 = load %struct.TYPE_4__**, %struct.TYPE_4__*** %11
%60 = load %struct.TYPE_4__*, %struct.TYPE_4__** %59
%61 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %60, i32 0, i32 0
%62 = load i32**, i32*** %61
%63 = load i64, i64* %15
%64 = getelementptr inbounds i32*, i32** %62, i64 %63
%65 = call i64 @parse_identifier(i32 %55, i8* %56, i64 %57, i64 %58, i32** %64, i32* null)
store i64 %65, i64* %13
%66 = load i64, i64* %15
%67 = add i64 %66, 1
store i64 %67, i64* %15
%68 = load i32, i32* %7
%69 = load i8*, i8** %8
%70 = load i64, i64* %13
%71 = call i32 @Parrot_str_indexed(i32 %68, i8* %69, i64 %70)
store i32 %71, i32* %16
br label %45
72: ; preds = %52
%73 = load %struct.TYPE_4__**, %struct.TYPE_4__*** %11
%74 = load %struct.TYPE_4__*, %struct.TYPE_4__** %73
%75 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %74, i32 0, i32 0
%76 = load i32**, i32*** %75
%77 = load i64, i64* %14
%78 = getelementptr inbounds i32*, i32** %76, i64 %77
store i32* null, i32** %78
%79 = load i64, i64* %13
ret i64 %79
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32**, i32, i64, i64 }
@FFI_TYPE_STRUCT = global i32 0
define i64 @parse_structure(i32 %0, i8* %1, i64 %2, i64 %3, %struct.TYPE_4__** nocapture %4, i8* nocapture readnone %5) {
%7 = tail call i64 @structure_length(i32 %0, i8* %1, i64 %2, i64 %3)
%8 = tail call %struct.TYPE_4__* @malloc(i32 32)
store %struct.TYPE_4__* %8, %struct.TYPE_4__** %4
%9 = add i64 %7, 1
%10 = tail call i32** @calloc(i64 %9, i32 8)
%11 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i64 0, i32 0
store i32** %10, i32*** %12
%13 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %13, i64 0, i32 2
%15 = bitcast i64* %14 to i8*
%16 = load i32, i32* @FFI_TYPE_STRUCT
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %13, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @Parrot_str_indexed(i32 %0, i8* %1, i64 %2)
%19 = icmp ult i64 %2, %3
%20 = icmp ne i32 %18, 41
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %22, label %35
22: ; preds = %6, %22
%23 = phi i64 [ %30, %22 ], [ 0, %6 ]
%24 = phi i64 [ %29, %22 ], [ %2, %6 ]
%25 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %25, i64 0, i32 0
%27 = load i32**, i32*** %26
%28 = getelementptr inbounds i32*, i32** %27, i64 %23
%29 = tail call i64 @parse_identifier(i32 %0, i8* %1, i64 %24, i64 %3, i32** %28, i32* null)
%30 = add i64 %23, 1
%31 = tail call i32 @Parrot_str_indexed(i32 %0, i8* %1, i64 %29)
%32 = icmp ult i64 %29, %3
%33 = icmp ne i32 %31, 41
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %22, label %35
35: ; preds = %22, %6
%36 = phi i64 [ %2, %6 ], [ %29, %22 ]
%37 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%38 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %37, i64 0, i32 0
%39 = load i32**, i32*** %38
%40 = getelementptr inbounds i32*, i32** %39, i64 %7
store i32* null, i32** %40
ret i64 %36
}