#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 string {
  int dummy;
};
struct dict {
  int dummy;
};

/* Variables and functions */
struct string *dict_get(struct dict *, struct string *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t load_macro_name(char const *, size_t, char, struct string *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ string_append_char(struct string *, char const);
int /*<<< orphan*/ string_clear(struct string *);
int /*<<< orphan*/ string_destroy(struct string *);
char *string_get_cstr(struct string const *);
size_t string_get_size(struct string const *);
struct string *string_init(char *);
int expand_macros_impl(const struct string *line, struct dict *macros,
                       struct string *output, size_t depth) {
  const char *cstr = string_get_cstr(line);
  size_t size = string_get_size(line);
  size_t i;
  struct string *macro_name;
  const struct string *macro_value;
  if (depth > 200) {
    fprintf(stderr, "Maximal macro recursion depth exceeded\n");
    return (-1);
  }
  macro_name = string_init("");
  for (i = 0; i < size; ++i) {
    if (cstr[i] != '$') {
      string_append_char(output, cstr[i]);
    } else {
      ++i;
      if (i == size) {
        fprintf(stderr, "Dollar sign "
                        "at the end of a line\n");
        string_destroy(macro_name);
        return (-1);
      }
      if (cstr[i] == '(') {
        i = load_macro_name(cstr, i + 1, ')', macro_name);
      } else if (cstr[i] == '{') {
        i = load_macro_name(cstr, i + 1, '}', macro_name);
      } else if (cstr[i] == '$') {
        string_append_char(output, '$');
        continue;
      } else {
        string_append_char(macro_name, cstr[i]);
      }
      if (i == 0) {
        string_destroy(macro_name);
        return (-1);
      }
      macro_value = dict_get(macros, macro_name);
      if (macro_value != NULL) {
        if (expand_macros_impl(macro_value, macros, output, depth + 1)) {
          string_destroy(macro_name);
          return (-1);
        }
      }
      string_clear(macro_name);
    }
  }
  string_destroy(macro_name);
  return (0);
}


x86 assembly (clang, -O0):
<expand_macros_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <string_get_cstr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <string_get_size@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xc8,-0x28(%rbp)
jbe    1201 <expand_macros_impl+0x61>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f8 <expand_macros_impl+0x258>
lea    0xe1f(%rip),%rdi
call   1030 <string_init@plt>
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    13e8 <expand_macros_impl+0x248>
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x24,%eax
je     1256 <expand_macros_impl+0xb6>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
call   1040 <string_append_char@plt>
jmp    13d2 <expand_macros_impl+0x232>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    129a <expand_macros_impl+0xfa>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1050 <string_destroy@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f8 <expand_macros_impl+0x258>
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x28,%eax
jne    12d2 <expand_macros_impl+0x132>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
add    $0x1,%rsi
mov    -0x48(%rbp),%rcx
mov    $0x29,%edx
call   10a0 <load_macro_name@plt>
mov    %rax,-0x40(%rbp)
jmp    1351 <expand_macros_impl+0x1b1>
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x7b,%eax
jne    130a <expand_macros_impl+0x16a>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
add    $0x1,%rsi
mov    -0x48(%rbp),%rcx
mov    $0x7d,%edx
call   10a0 <load_macro_name@plt>
mov    %rax,-0x40(%rbp)
jmp    134c <expand_macros_impl+0x1ac>
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x24,%eax
jne    1332 <expand_macros_impl+0x192>
mov    -0x20(%rbp),%rdi
mov    $0x24,%esi
call   1040 <string_append_char@plt>
jmp    13d7 <expand_macros_impl+0x237>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
call   1040 <string_append_char@plt>
jmp    134c <expand_macros_impl+0x1ac>
jmp    1351 <expand_macros_impl+0x1b1>
cmpq   $0x0,-0x40(%rbp)
jne    1371 <expand_macros_impl+0x1d1>
mov    -0x48(%rbp),%rdi
call   1050 <string_destroy@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f8 <expand_macros_impl+0x258>
mov    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1090 <dict_get@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     13c9 <expand_macros_impl+0x229>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
call   1080 <expand_macros_impl@plt>
cmp    $0x0,%eax
je     13c4 <expand_macros_impl+0x224>
mov    -0x48(%rbp),%rdi
call   1050 <string_destroy@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f8 <expand_macros_impl+0x258>
jmp    13c9 <expand_macros_impl+0x229>
mov    -0x48(%rbp),%rdi
call   10c0 <string_clear@plt>
jmp    13d7 <expand_macros_impl+0x237>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1219 <expand_macros_impl+0x79>
mov    -0x48(%rbp),%rdi
call   1050 <string_destroy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_macros_impl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
call   10b0 <string_get_cstr@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1060 <string_get_size@plt>
mov    %r15,0x10(%rsp)
cmp    $0xc9,%r15
jb     11f2 <expand_macros_impl+0x52>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1307 <expand_macros_impl+0x167>
mov    %rax,%rbp
lea    0xe2b(%rip),%rdi
call   1030 <string_init@plt>
mov    %rax,%r13
test   %rbp,%rbp
mov    %r14,0x8(%rsp)
je     12de <expand_macros_impl+0x13e>
addq   $0x1,0x10(%rsp)
xor    %r14d,%r14d
jmp    123a <expand_macros_impl+0x9a>
nopl   (%rax)
movsbl %al,%esi
mov    0x8(%rsp),%rdi
call   1040 <string_append_char@plt>
add    $0x1,%r14
cmp    %rbp,%r14
jae    12de <expand_macros_impl+0x13e>
movzbl (%r12,%r14,1),%eax
cmp    $0x24,%al
jne    1220 <expand_macros_impl+0x80>
lea    0x1(%r14),%r15
cmp    %rbp,%r15
je     12ea <expand_macros_impl+0x14a>
movzbl (%r12,%r15,1),%eax
cmp    $0x24,%al
je     1265 <expand_macros_impl+0xc5>
cmp    $0x28,%al
je     1279 <expand_macros_impl+0xd9>
cmp    $0x7b,%al
jne    129a <expand_macros_impl+0xfa>
mov    $0x7d,%al
jmp    127b <expand_macros_impl+0xdb>
mov    0x8(%rsp),%rdi
mov    $0x24,%esi
call   1040 <string_append_char@plt>
mov    %r15,%r14
jmp    122d <expand_macros_impl+0x8d>
mov    $0x29,%al
add    $0x2,%r14
movzbl %al,%edx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rcx
call   10a0 <load_macro_name@plt>
mov    %rax,%r14
test   %rax,%rax
jne    12a8 <expand_macros_impl+0x108>
jmp    12ff <expand_macros_impl+0x15f>
movsbl %al,%esi
mov    %r13,%rdi
call   1040 <string_append_char@plt>
mov    %r15,%r14
mov    %rbx,%rdi
mov    %r13,%rsi
call   1090 <dict_get@plt>
test   %rax,%rax
je     12d1 <expand_macros_impl+0x131>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    0x8(%rsp),%rdx
mov    0x10(%rsp),%rcx
call   1080 <expand_macros_impl@plt>
test   %eax,%eax
jne    12ff <expand_macros_impl+0x15f>
mov    %r13,%rdi
call   10c0 <string_clear@plt>
jmp    122d <expand_macros_impl+0x8d>
mov    %r13,%rdi
call   1050 <string_destroy@plt>
xor    %eax,%eax
jmp    130c <expand_macros_impl+0x16c>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd2e(%rip),%rsi
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1050 <string_destroy@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dict = type { i32 }
%struct.string = type { i32 }
@stderr = global i32 0
@.str = [40 x i8] c"Maximal macro recursion depth exceeded\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [34 x i8] c"Dollar sign at the end of a line\0A\00"
define i32 @expand_macros_impl(%struct.string* %0, %struct.dict* %1, %struct.string* %2, i64 %3) {
%5 = alloca i32
%6 = alloca %struct.string*
%7 = alloca %struct.dict*
%8 = alloca %struct.string*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca %struct.string*
%14 = alloca %struct.string*
store %struct.string* %0, %struct.string** %6
store %struct.dict* %1, %struct.dict** %7
store %struct.string* %2, %struct.string** %8
store i64 %3, i64* %9
%15 = load %struct.string*, %struct.string** %6
%16 = call i8* @string_get_cstr(%struct.string* %15)
store i8* %16, i8** %10
%17 = load %struct.string*, %struct.string** %6
%18 = call i64 @string_get_size(%struct.string* %17)
store i64 %18, i64* %11
%19 = load i64, i64* %9
%20 = icmp ugt i64 %19, 200
br i1 %20, label %21, label %24
21: ; preds = %4
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %134
24: ; preds = %4
%25 = call %struct.string* @string_init(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store %struct.string* %25, %struct.string** %13
store i64 0, i64* %12
br label %26
26: ; preds = %128, %24
%27 = load i64, i64* %12
%28 = load i64, i64* %11
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %131
30: ; preds = %26
%31 = load i8*, i8** %10
%32 = load i64, i64* %12
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 36
br i1 %36, label %37, label %44
37: ; preds = %30
%38 = load %struct.string*, %struct.string** %8
%39 = load i8*, i8** %10
%40 = load i64, i64* %12
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i8, i8* %41
%43 = call i32 @string_append_char(%struct.string* %38, i8 signext %42)
br label %127
44: ; preds = %30
%45 = load i64, i64* %12
%46 = add i64 %45, 1
store i64 %46, i64* %12
%47 = load i64, i64* %12
%48 = load i64, i64* %11
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %44
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%53 = load %struct.string*, %struct.string** %13
%54 = call i32 @string_destroy(%struct.string* %53)
store i32 -1, i32* %5
br label %134
55: ; preds = %44
%56 = load i8*, i8** %10
%57 = load i64, i64* %12
%58 = getelementptr inbounds i8, i8* %56, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 40
br i1 %61, label %62, label %68
62: ; preds = %55
%63 = load i8*, i8** %10
%64 = load i64, i64* %12
%65 = add i64 %64, 1
%66 = load %struct.string*, %struct.string** %13
%67 = call i64 @load_macro_name(i8* %63, i64 %65, i8 signext 41, %struct.string* %66)
store i64 %67, i64* %12
br label %100
68: ; preds = %55
%69 = load i8*, i8** %10
%70 = load i64, i64* %12
%71 = getelementptr inbounds i8, i8* %69, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp eq i32 %73, 123
br i1 %74, label %75, label %81
75: ; preds = %68
%76 = load i8*, i8** %10
%77 = load i64, i64* %12
%78 = add i64 %77, 1
%79 = load %struct.string*, %struct.string** %13
%80 = call i64 @load_macro_name(i8* %76, i64 %78, i8 signext 125, %struct.string* %79)
store i64 %80, i64* %12
br label %99
81: ; preds = %68
%82 = load i8*, i8** %10
%83 = load i64, i64* %12
%84 = getelementptr inbounds i8, i8* %82, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 36
br i1 %87, label %88, label %91
88: ; preds = %81
%89 = load %struct.string*, %struct.string** %8
%90 = call i32 @string_append_char(%struct.string* %89, i8 signext 36)
br label %128
91: ; preds = %81
%92 = load %struct.string*, %struct.string** %13
%93 = load i8*, i8** %10
%94 = load i64, i64* %12
%95 = getelementptr inbounds i8, i8* %93, i64 %94
%96 = load i8, i8* %95
%97 = call i32 @string_append_char(%struct.string* %92, i8 signext %96)
br label %98
98: ; preds = %91
br label %99
99: ; preds = %98, %75
br label %100
100: ; preds = %99, %62
%101 = load i64, i64* %12
%102 = icmp eq i64 %101, 0
br i1 %102, label %103, label %106
103: ; preds = %100
%104 = load %struct.string*, %struct.string** %13
%105 = call i32 @string_destroy(%struct.string* %104)
store i32 -1, i32* %5
br label %134
106: ; preds = %100
%107 = load %struct.dict*, %struct.dict** %7
%108 = load %struct.string*, %struct.string** %13
%109 = call %struct.string* @dict_get(%struct.dict* %107, %struct.string* %108)
store %struct.string* %109, %struct.string** %14
%110 = load %struct.string*, %struct.string** %14
%111 = icmp ne %struct.string* %110, null
br i1 %111, label %112, label %124
112: ; preds = %106
%113 = load %struct.string*, %struct.string** %14
%114 = load %struct.dict*, %struct.dict** %7
%115 = load %struct.string*, %struct.string** %8
%116 = load i64, i64* %9
%117 = add i64 %116, 1
%118 = call i32 @expand_macros_impl(%struct.string* %113, %struct.dict* %114, %struct.string* %115, i64 %117)
%119 = icmp ne i32 %118, 0
br i1 %119, label %120, label %123
120: ; preds = %112
%121 = load %struct.string*, %struct.string** %13
%122 = call i32 @string_destroy(%struct.string* %121)
store i32 -1, i32* %5
br label %134
123: ; preds = %112
br label %124
124: ; preds = %123, %106
%125 = load %struct.string*, %struct.string** %13
%126 = call i32 @string_clear(%struct.string* %125)
br label %127
127: ; preds = %124, %37
br label %128
128: ; preds = %127, %88
%129 = load i64, i64* %12
%130 = add i64 %129, 1
store i64 %130, i64* %12
br label %26
131: ; preds = %26
%132 = load %struct.string*, %struct.string** %13
%133 = call i32 @string_destroy(%struct.string* %132)
store i32 0, i32* %5
br label %134
134: ; preds = %131, %120, %103, %50, %21
%135 = load i32, i32* %5
ret i32 %135
}

LLVM IR (-O2):
%struct.dict = type { i32 }
%struct.string = type { i32 }
@stderr = global i32 0
@.str = [40 x i8] c"Maximal macro recursion depth exceeded\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [34 x i8] c"Dollar sign at the end of a line\0A\00"
define i32 @expand_macros_impl(%struct.string* %0, %struct.dict* %1, %struct.string* %2, i64 %3) {
%5 = tail call i8* @string_get_cstr(%struct.string* %0)
%6 = tail call i64 @string_get_size(%struct.string* %0)
%7 = icmp ugt i64 %3, 200
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %62
11: ; preds = %4
%12 = tail call %struct.string* @string_init(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %6, 0
br i1 %13, label %60, label %14
14: ; preds = %11
%15 = add nuw nsw i64 %3, 1
br label %16
16: ; preds = %14, %56
%17 = phi i64 [ 0, %14 ], [ %58, %56 ]
%18 = getelementptr inbounds i8, i8* %5, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 36
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = tail call i32 @string_append_char(%struct.string* %2, i8 signext %19)
br label %56
23: ; preds = %16
%24 = add nuw i64 %17, 1
%25 = icmp eq i64 %24, %6
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @string_destroy(%struct.string* %12)
br label %62
30: ; preds = %23
%31 = getelementptr inbounds i8, i8* %5, i64 %24
%32 = load i8, i8* %31
switch i8 %32, label %36 [
i8 40, label %38
i8 123, label %33
i8 36, label %34
]
33: ; preds = %30
br label %38
34: ; preds = %30
%35 = tail call i32 @string_append_char(%struct.string* %2, i8 signext 36)
br label %56
36: ; preds = %30
%37 = tail call i32 @string_append_char(%struct.string* %12, i8 signext %32)
br label %45
38: ; preds = %30, %33
%39 = phi i8 [ 125, %33 ], [ 41, %30 ]
%40 = add i64 %17, 2
%41 = tail call i64 @load_macro_name(i8* %5, i64 %40, i8 signext %39, %struct.string* %12)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = tail call i32 @string_destroy(%struct.string* %12)
br label %62
45: ; preds = %36, %38
%46 = phi i64 [ %24, %36 ], [ %41, %38 ]
%47 = tail call %struct.string* @dict_get(%struct.dict* %1, %struct.string* %12)
%48 = icmp eq %struct.string* %47, null
br i1 %48, label %54, label %49
49: ; preds = %45
%50 = tail call i32 @expand_macros_impl(%struct.string* %47, %struct.dict* %1, %struct.string* %2, i64 %15)
%51 = icmp eq i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %49
%53 = tail call i32 @string_destroy(%struct.string* %12)
br label %62
54: ; preds = %49, %45
%55 = tail call i32 @string_clear(%struct.string* %12)
br label %56
56: ; preds = %21, %54, %34
%57 = phi i64 [ %17, %21 ], [ %46, %54 ], [ %24, %34 ]
%58 = add i64 %57, 1
%59 = icmp ult i64 %58, %6
br i1 %59, label %16, label %60
60: ; preds = %56, %11
%61 = tail call i32 @string_destroy(%struct.string* %12)
br label %62
62: ; preds = %60, %52, %43, %26, %8
%63 = phi i32 [ -1, %8 ], [ -1, %26 ], [ -1, %43 ], [ -1, %52 ], [ 0, %60 ]
ret i32 %63
}