#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 gges_bnf_production {
  int dummy;
};
struct gges_bnf_non_terminal {
  int dummy;
};
struct gges_bnf_grammar {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ calculate_non_terminal_depths(struct gges_bnf_grammar *);
int /*<<< orphan*/ calculate_production_depths(struct gges_bnf_grammar *);
int /*<<< orphan*/ calculate_production_recursion(struct gges_bnf_grammar *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ link_non_terminal_tokens(struct gges_bnf_grammar *);
int /*<<< orphan*/ process_token(struct gges_bnf_grammar *,
                                 struct gges_bnf_non_terminal **,
                                 struct gges_bnf_production **, char *, char *);
char *read_token(char const *, char **);
void gges_extend_grammar(struct gges_bnf_grammar *g, const char *bnfstr,
                         bool relink) {
  char *cur, *nxt;
  struct gges_bnf_non_terminal *nt;
  struct gges_bnf_production *p;
  nt = NULL;
  p = NULL;
  cur = NULL;
  nxt = NULL;
  bnfstr = read_token(bnfstr, &nxt);
  do {
    free(cur);
    cur = nxt;
    bnfstr = read_token(bnfstr, &nxt);
    process_token(g, &nt, &p, cur, nxt);
  } while (nxt != NULL);
  free(cur);
  if (relink) {
    link_non_terminal_tokens(g);
    calculate_production_depths(g);
    calculate_production_recursion(g);
    calculate_non_terminal_depths(g);
  }
}


x86 assembly (clang, -O0):
<gges_extend_grammar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <read_token@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <read_token@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <process_token@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11b4 <gges_extend_grammar+0x44>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x14(%rbp)
je     1231 <gges_extend_grammar+0xc1>
mov    -0x8(%rbp),%rdi
call   1070 <link_non_terminal_tokens@plt>
mov    -0x8(%rbp),%rdi
call   1040 <calculate_production_depths@plt>
mov    -0x8(%rbp),%rdi
call   1090 <calculate_production_recursion@plt>
mov    -0x8(%rbp),%rdi
call   1050 <calculate_non_terminal_depths@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gges_extend_grammar>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,0x14(%rsp)
mov    %rdi,%r13
movq   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%r15
mov    %rsi,%rdi
mov    %r15,%rsi
call   1080 <read_token@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
lea    0x20(%rsp),%r12
lea    0x18(%rsp),%r14
nop
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <read_token@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%r8
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %rbp,%rcx
call   1060 <process_token@plt>
cmpq   $0x0,0x8(%rsp)
jne    11c0 <gges_extend_grammar+0x50>
mov    %rbp,%rdi
call   1030 <free@plt>
cmpl   $0x0,0x14(%rsp)
je     1228 <gges_extend_grammar+0xb8>
mov    %r13,%rdi
call   1070 <link_non_terminal_tokens@plt>
mov    %r13,%rdi
call   1040 <calculate_production_depths@plt>
mov    %r13,%rdi
call   1090 <calculate_production_recursion@plt>
mov    %r13,%rdi
call   1050 <calculate_non_terminal_depths@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.gges_bnf_grammar = type { i32 }
%struct.gges_bnf_non_terminal = type { i32 }
%struct.gges_bnf_production = type { i32 }
define void @gges_extend_grammar(%struct.gges_bnf_grammar* %0, i8* %1, i32 %2) {
%4 = alloca %struct.gges_bnf_grammar*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.gges_bnf_non_terminal*
%10 = alloca %struct.gges_bnf_production*
store %struct.gges_bnf_grammar* %0, %struct.gges_bnf_grammar** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store %struct.gges_bnf_non_terminal* null, %struct.gges_bnf_non_terminal** %9
store %struct.gges_bnf_production* null, %struct.gges_bnf_production** %10
store i8* null, i8** %7
store i8* null, i8** %8
%11 = load i8*, i8** %5
%12 = call i8* @read_token(i8* %11, i8** %8)
store i8* %12, i8** %5
br label %13
13: ; preds = %23, %3
%14 = load i8*, i8** %7
%15 = call i32 @free(i8* %14)
%16 = load i8*, i8** %8
store i8* %16, i8** %7
%17 = load i8*, i8** %5
%18 = call i8* @read_token(i8* %17, i8** %8)
store i8* %18, i8** %5
%19 = load %struct.gges_bnf_grammar*, %struct.gges_bnf_grammar** %4
%20 = load i8*, i8** %7
%21 = load i8*, i8** %8
%22 = call i32 @process_token(%struct.gges_bnf_grammar* %19, %struct.gges_bnf_non_terminal** %9, %struct.gges_bnf_production** %10, i8* %20, i8* %21)
br label %23
23: ; preds = %13
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %13, label %26
26: ; preds = %23
%27 = load i8*, i8** %7
%28 = call i32 @free(i8* %27)
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %26
%32 = load %struct.gges_bnf_grammar*, %struct.gges_bnf_grammar** %4
%33 = call i32 @link_non_terminal_tokens(%struct.gges_bnf_grammar* %32)
%34 = load %struct.gges_bnf_grammar*, %struct.gges_bnf_grammar** %4
%35 = call i32 @calculate_production_depths(%struct.gges_bnf_grammar* %34)
%36 = load %struct.gges_bnf_grammar*, %struct.gges_bnf_grammar** %4
%37 = call i32 @calculate_production_recursion(%struct.gges_bnf_grammar* %36)
%38 = load %struct.gges_bnf_grammar*, %struct.gges_bnf_grammar** %4
%39 = call i32 @calculate_non_terminal_depths(%struct.gges_bnf_grammar* %38)
br label %40
40: ; preds = %31, %26
ret void
}

LLVM IR (-O2):
%struct.gges_bnf_grammar = type { i32 }
%struct.gges_bnf_non_terminal = type { i32 }
%struct.gges_bnf_production = type { i32 }
define void @gges_extend_grammar(%struct.gges_bnf_grammar* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca %struct.gges_bnf_non_terminal*
%6 = alloca %struct.gges_bnf_production*
%7 = bitcast i8** %4 to i8*
%8 = bitcast %struct.gges_bnf_non_terminal** %5 to i8*
%9 = bitcast %struct.gges_bnf_production** %6 to i8*
store %struct.gges_bnf_non_terminal* null, %struct.gges_bnf_non_terminal** %5
store %struct.gges_bnf_production* null, %struct.gges_bnf_production** %6
store i8* null, i8** %4
%10 = call i8* @read_token(i8* %1, i8** %4)
br label %11
11: ; preds = %11, %3
%12 = phi i8* [ %10, %3 ], [ %16, %11 ]
%13 = phi i8* [ null, %3 ], [ %15, %11 ]
%14 = call i32 @free(i8* %13)
%15 = load i8*, i8** %4
%16 = call i8* @read_token(i8* %12, i8** %4)
%17 = load i8*, i8** %4
%18 = call i32 @process_token(%struct.gges_bnf_grammar* %0, %struct.gges_bnf_non_terminal** %5, %struct.gges_bnf_production** %6, i8* %15, i8* %17)
%19 = load i8*, i8** %4
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %11
21: ; preds = %11
%22 = call i32 @free(i8* %15)
%23 = icmp eq i32 %2, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = call i32 @link_non_terminal_tokens(%struct.gges_bnf_grammar* %0)
%26 = call i32 @calculate_production_depths(%struct.gges_bnf_grammar* %0)
%27 = call i32 @calculate_production_recursion(%struct.gges_bnf_grammar* %0)
%28 = call i32 @calculate_non_terminal_depths(%struct.gges_bnf_grammar* %0)
br label %29
29: ; preds = %24, %21
ret void
}