#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 rewrite_batch {
  int dummy;
};
struct fsm {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ fsm_concat(struct fsm *, struct fsm *);
int /*<<< orphan*/ fsm_copy(struct fsm *);
int /*<<< orphan*/ fsm_intersect(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fsm_lower(int /*<<< orphan*/);
struct fsm *fsm_minimize(int /*<<< orphan*/);
int /*<<< orphan*/ fsm_upper(struct fsm *);
struct fsm *rewrite_any_4tape(struct rewrite_batch *);
struct fsm *rewrite_lower(struct rewrite_batch *, struct fsm *);
struct fsm *rewrite_upper(struct rewrite_batch *, struct fsm *);
struct fsm *rewrite_two_level(struct rewrite_batch *rb, struct fsm *lang,
                              int rightside) {
  struct fsm *Lower, *Upper, *Result;
  Lower = rewrite_lower(rb, fsm_minimize(fsm_lower(fsm_copy(lang))));
  Upper = rewrite_upper(rb, fsm_minimize(fsm_upper(lang)));
  if (rightside == 1) {
    Result =
        fsm_minimize(fsm_intersect(fsm_concat(Lower, rewrite_any_4tape(rb)),
                                   fsm_concat(Upper, rewrite_any_4tape(rb))));
  } else {
    Result =
        fsm_minimize(fsm_intersect(fsm_concat(rewrite_any_4tape(rb), Lower),
                                   fsm_concat(rewrite_any_4tape(rb), Upper)));
  }
  return Result;
}


x86 assembly (clang, -O0):
<rewrite_two_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fsm_copy@plt>
mov    %eax,%edi
call   1070 <fsm_lower@plt>
mov    %eax,%edi
call   1030 <fsm_minimize@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
call   1050 <rewrite_lower@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <fsm_upper@plt>
mov    %eax,%edi
call   1030 <fsm_minimize@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   10a0 <rewrite_upper@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    125b <rewrite_two_level+0xcb>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <rewrite_any_4tape@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rsi
call   1060 <fsm_concat@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <rewrite_any_4tape@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
call   1060 <fsm_concat@plt>
mov    -0x44(%rbp),%edi
mov    %eax,%esi
call   1090 <fsm_intersect@plt>
mov    %eax,%edi
call   1030 <fsm_minimize@plt>
mov    %rax,-0x30(%rbp)
jmp    129d <rewrite_two_level+0x10d>
mov    -0x8(%rbp),%rdi
call   1080 <rewrite_any_4tape@plt>
mov    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1060 <fsm_concat@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <rewrite_any_4tape@plt>
mov    %rax,%rdi
mov    -0x28(%rbp),%rsi
call   1060 <fsm_concat@plt>
mov    -0x5c(%rbp),%edi
mov    %eax,%esi
call   1090 <fsm_intersect@plt>
mov    %eax,%edi
call   1030 <fsm_minimize@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite_two_level>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <fsm_copy@plt>
mov    %eax,%edi
call   1070 <fsm_lower@plt>
mov    %eax,%edi
call   1030 <fsm_minimize@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <rewrite_lower@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   10b0 <fsm_upper@plt>
mov    %eax,%edi
call   1030 <fsm_minimize@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   10a0 <rewrite_upper@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1080 <rewrite_any_4tape@plt>
cmp    $0x1,%r15d
jne    120a <rewrite_two_level+0x7a>
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <fsm_concat@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <rewrite_any_4tape@plt>
mov    %rax,%rsi
jmp    1225 <rewrite_two_level+0x95>
mov    %rax,%rdi
mov    %r12,%rsi
call   1060 <fsm_concat@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <rewrite_any_4tape@plt>
mov    %r14,%rsi
mov    %rax,%r14
mov    %r14,%rdi
call   1060 <fsm_concat@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1090 <fsm_intersect@plt>
mov    %eax,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fsm_minimize@plt>

LLVM IR (-O0):
%struct.rewrite_batch = type { i32 }
%struct.fsm = type { i32 }
define %struct.fsm* @rewrite_two_level(%struct.rewrite_batch* %0, %struct.fsm* %1, i32 %2) {
%4 = alloca %struct.rewrite_batch*
%5 = alloca %struct.fsm*
%6 = alloca i32
%7 = alloca %struct.fsm*
%8 = alloca %struct.fsm*
%9 = alloca %struct.fsm*
store %struct.rewrite_batch* %0, %struct.rewrite_batch** %4
store %struct.fsm* %1, %struct.fsm** %5
store i32 %2, i32* %6
%10 = load %struct.rewrite_batch*, %struct.rewrite_batch** %4
%11 = load %struct.fsm*, %struct.fsm** %5
%12 = call i32 @fsm_copy(%struct.fsm* %11)
%13 = call i32 @fsm_lower(i32 %12)
%14 = call %struct.fsm* @fsm_minimize(i32 %13)
%15 = call %struct.fsm* @rewrite_lower(%struct.rewrite_batch* %10, %struct.fsm* %14)
store %struct.fsm* %15, %struct.fsm** %7
%16 = load %struct.rewrite_batch*, %struct.rewrite_batch** %4
%17 = load %struct.fsm*, %struct.fsm** %5
%18 = call i32 @fsm_upper(%struct.fsm* %17)
%19 = call %struct.fsm* @fsm_minimize(i32 %18)
%20 = call %struct.fsm* @rewrite_upper(%struct.rewrite_batch* %16, %struct.fsm* %19)
store %struct.fsm* %20, %struct.fsm** %8
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %34
23: ; preds = %3
%24 = load %struct.fsm*, %struct.fsm** %7
%25 = load %struct.rewrite_batch*, %struct.rewrite_batch** %4
%26 = call %struct.fsm* @rewrite_any_4tape(%struct.rewrite_batch* %25)
%27 = call i32 @fsm_concat(%struct.fsm* %24, %struct.fsm* %26)
%28 = load %struct.fsm*, %struct.fsm** %8
%29 = load %struct.rewrite_batch*, %struct.rewrite_batch** %4
%30 = call %struct.fsm* @rewrite_any_4tape(%struct.rewrite_batch* %29)
%31 = call i32 @fsm_concat(%struct.fsm* %28, %struct.fsm* %30)
%32 = call i32 @fsm_intersect(i32 %27, i32 %31)
%33 = call %struct.fsm* @fsm_minimize(i32 %32)
store %struct.fsm* %33, %struct.fsm** %9
br label %45
34: ; preds = %3
%35 = load %struct.rewrite_batch*, %struct.rewrite_batch** %4
%36 = call %struct.fsm* @rewrite_any_4tape(%struct.rewrite_batch* %35)
%37 = load %struct.fsm*, %struct.fsm** %7
%38 = call i32 @fsm_concat(%struct.fsm* %36, %struct.fsm* %37)
%39 = load %struct.rewrite_batch*, %struct.rewrite_batch** %4
%40 = call %struct.fsm* @rewrite_any_4tape(%struct.rewrite_batch* %39)
%41 = load %struct.fsm*, %struct.fsm** %8
%42 = call i32 @fsm_concat(%struct.fsm* %40, %struct.fsm* %41)
%43 = call i32 @fsm_intersect(i32 %38, i32 %42)
%44 = call %struct.fsm* @fsm_minimize(i32 %43)
store %struct.fsm* %44, %struct.fsm** %9
br label %45
45: ; preds = %34, %23
%46 = load %struct.fsm*, %struct.fsm** %9
ret %struct.fsm* %46
}

LLVM IR (-O2):
%struct.rewrite_batch = type { i32 }
%struct.fsm = type { i32 }
define %struct.fsm* @rewrite_two_level(%struct.rewrite_batch* %0, %struct.fsm* %1, i32 %2) {
%4 = tail call i32 @fsm_copy(%struct.fsm* %1)
%5 = tail call i32 @fsm_lower(i32 %4)
%6 = tail call %struct.fsm* @fsm_minimize(i32 %5)
%7 = tail call %struct.fsm* @rewrite_lower(%struct.rewrite_batch* %0, %struct.fsm* %6)
%8 = tail call i32 @fsm_upper(%struct.fsm* %1)
%9 = tail call %struct.fsm* @fsm_minimize(i32 %8)
%10 = tail call %struct.fsm* @rewrite_upper(%struct.rewrite_batch* %0, %struct.fsm* %9)
%11 = icmp eq i32 %2, 1
%12 = tail call %struct.fsm* @rewrite_any_4tape(%struct.rewrite_batch* %0)
br i1 %11, label %13, label %16
13: ; preds = %3
%14 = tail call i32 @fsm_concat(%struct.fsm* %7, %struct.fsm* %12)
%15 = tail call %struct.fsm* @rewrite_any_4tape(%struct.rewrite_batch* %0)
br label %19
16: ; preds = %3
%17 = tail call i32 @fsm_concat(%struct.fsm* %12, %struct.fsm* %7)
%18 = tail call %struct.fsm* @rewrite_any_4tape(%struct.rewrite_batch* %0)
br label %19
19: ; preds = %16, %13
%20 = phi %struct.fsm* [ %10, %16 ], [ %15, %13 ]
%21 = phi %struct.fsm* [ %18, %16 ], [ %10, %13 ]
%22 = phi i32 [ %17, %16 ], [ %14, %13 ]
%23 = tail call i32 @fsm_concat(%struct.fsm* %21, %struct.fsm* %20)
%24 = tail call i32 @fsm_intersect(i32 %22, i32 %23)
%25 = tail call %struct.fsm* @fsm_minimize(i32 %24)
ret %struct.fsm* %25
}