#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 */

/* Variables and functions */
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
void expand_spec(char *buf, char *bufend, char *str) {
  int ch;
  char *p = buf;
  while ((ch = *str++)) {
    if (p + 10 >= bufend)
      error("Buffer overflow in fidlib expand_spec()");
    if (ch == '#') {
      switch (*str++) {
      case 'o':
        p += sprintf(p, "<optional-order>");
        break;
      case 'O':
        p += sprintf(p, "<order>");
        break;
      case 'F':
        p += sprintf(p, "<freq>");
        break;
      case 'R':
        p += sprintf(p, "<range>");
        break;
      case 'V':
        p += sprintf(p, "<value>");
        break;
      default:
        p += sprintf(p, "<%c>", str[-1]);
        break;
      }
    } else {
      *p++ = ch;
    }
  }
  *p = 0;
}


x86 assembly (clang, -O0):
<expand_spec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     12c7 <expand_spec+0x1a7>
mov    -0x28(%rbp),%rax
add    $0xa,%rax
cmp    -0x10(%rbp),%rax
jb     1178 <expand_spec+0x58>
lea    0xf35(%rip),%rdi
call   1030 <error@plt>
cmpl   $0x23,-0x1c(%rbp)
jne    12ac <expand_spec+0x18c>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%eax
add    $0xffffffba,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x29,%eax
ja     127d <expand_spec+0x15d>
mov    -0x30(%rbp),%rax
lea    0xe4f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x28(%rbp),%rdi
lea    0xf0b(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    12a7 <expand_spec+0x187>
mov    -0x28(%rbp),%rdi
lea    0xef5(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    12a7 <expand_spec+0x187>
mov    -0x28(%rbp),%rdi
lea    0xed6(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    12a7 <expand_spec+0x187>
mov    -0x28(%rbp),%rdi
lea    0xeb6(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    12a7 <expand_spec+0x187>
mov    -0x28(%rbp),%rdi
lea    0xe97(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    12a7 <expand_spec+0x187>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%edx
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    12c2 <expand_spec+0x1a2>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    113c <expand_spec+0x1c>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_spec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdx),%r12b
test   %r12b,%r12b
je     120a <expand_spec+0xea>
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xeb9(%rip),%rbp
lea    0xf5a(%rip),%r13
jmp    1168 <expand_spec+0x48>
add    $0x1,%r14
mov    %r12b,(%rbx)
add    $0x1,%rbx
movzbl (%r14),%r12d
test   %r12b,%r12b
je     120a <expand_spec+0xea>
lea    0xa(%rbx),%rax
cmp    %r15,%rax
jb     1179 <expand_spec+0x59>
mov    %r13,%rdi
call   1030 <error@plt>
cmp    $0x23,%r12b
jne    1150 <expand_spec+0x30>
movsbl 0x1(%r14),%edx
add    $0x2,%r14
lea    -0x46(%rdx),%eax
cmp    $0x29,%eax
ja     11ad <expand_spec+0x8d>
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
movl   $0x3e7165,0x3(%rbx)
movl   $0x6572663c,(%rbx)
add    $0x6,%rbx
jmp    115b <expand_spec+0x3b>
mov    %rbx,%rdi
lea    0xf31(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
cltq
add    %rax,%rbx
jmp    115b <expand_spec+0x3b>
movabs $0x3e726564726f3c,%rax
jmp    11e7 <expand_spec+0xc7>
movabs $0x3e65676e61723c,%rax
jmp    11e7 <expand_spec+0xc7>
movabs $0x3e65756c61763c,%rax
mov    %rax,(%rbx)
add    $0x7,%rbx
jmp    115b <expand_spec+0x3b>
movups 0xed6(%rip),%xmm0
movups %xmm0,(%rbx)
movb   $0x0,0x10(%rbx)
add    $0x10,%rbx
jmp    115b <expand_spec+0x3b>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Buffer overflow in fidlib expand_spec()\00"
@.str.1 = [17 x i8] c"<optional-order>\00"
@.str.2 = [8 x i8] c"<order>\00"
@.str.3 = [7 x i8] c"<freq>\00"
@.str.4 = [8 x i8] c"<range>\00"
@.str.5 = [8 x i8] c"<value>\00"
@.str.6 = [5 x i8] c"<%c>\00"
define void @expand_spec(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
store i8* %9, i8** %8
br label %10
10: ; preds = %77, %3
%11 = load i8*, i8** %6
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %6
%13 = load i8, i8* %11
%14 = sext i8 %13 to i32
store i32 %14, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %78
16: ; preds = %10
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i64 10
%19 = load i8*, i8** %5
%20 = icmp uge i8* %18, %19
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 35
br i1 %25, label %26, label %72
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %6
%29 = load i8, i8* %27
%30 = sext i8 %29 to i32
switch i32 %30, label %61 [
i32 111, label %31
i32 79, label %37
i32 70, label %43
i32 82, label %49
i32 86, label %55
]
31: ; preds = %26
%32 = load i8*, i8** %8
%33 = call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%34 = load i8*, i8** %8
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %8
br label %71
37: ; preds = %26
%38 = load i8*, i8** %8
%39 = call i32 (i8*, i8*, ...) @sprintf(i8* %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%40 = load i8*, i8** %8
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %8
br label %71
43: ; preds = %26
%44 = load i8*, i8** %8
%45 = call i32 (i8*, i8*, ...) @sprintf(i8* %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%46 = load i8*, i8** %8
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %8
br label %71
49: ; preds = %26
%50 = load i8*, i8** %8
%51 = call i32 (i8*, i8*, ...) @sprintf(i8* %50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%52 = load i8*, i8** %8
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %8
br label %71
55: ; preds = %26
%56 = load i8*, i8** %8
%57 = call i32 (i8*, i8*, ...) @sprintf(i8* %56, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%58 = load i8*, i8** %8
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8* %60, i8** %8
br label %71
61: ; preds = %26
%62 = load i8*, i8** %8
%63 = load i8*, i8** %6
%64 = getelementptr inbounds i8, i8* %63, i64 -1
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = call i32 (i8*, i8*, ...) @sprintf(i8* %62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 %66)
%68 = load i8*, i8** %8
%69 = sext i32 %67 to i64
%70 = getelementptr inbounds i8, i8* %68, i64 %69
store i8* %70, i8** %8
br label %71
71: ; preds = %61, %55, %49, %43, %37, %31
br label %77
72: ; preds = %23
%73 = load i32, i32* %7
%74 = trunc i32 %73 to i8
%75 = load i8*, i8** %8
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %8
store i8 %74, i8* %75
br label %77
77: ; preds = %72, %71
br label %10
78: ; preds = %10
%79 = load i8*, i8** %8
store i8 0, i8* %79
ret void
}

LLVM IR (-O2):
@.str = [40 x i8] c"Buffer overflow in fidlib expand_spec()\00"
@.str.1 = [17 x i8] c"<optional-order>\00"
@.str.3 = [7 x i8] c"<freq>\00"
@.str.6 = [5 x i8] c"<%c>\00"
define void @expand_spec(i8* writeonly %0, i8* readnone %1, i8* nocapture %2) {
%4 = load i8, i8* %2
%5 = icmp eq i8 %4, 0
br i1 %5, label %45, label %6
6: ; preds = %3, %40
%7 = phi i8 [ %43, %40 ], [ %4, %3 ]
%8 = phi i8* [ %42, %40 ], [ %0, %3 ]
%9 = phi i8* [ %41, %40 ], [ %2, %3 ]
%10 = getelementptr inbounds i8, i8* %9, i64 1
%11 = getelementptr inbounds i8, i8* %8, i64 10
%12 = icmp ult i8* %11, %1
br i1 %12, label %15, label %13
13: ; preds = %6
%14 = tail call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %6
%16 = icmp eq i8 %7, 35
br i1 %16, label %17, label %38
17: ; preds = %15
%18 = getelementptr inbounds i8, i8* %9, i64 2
%19 = load i8, i8* %10
%20 = sext i8 %19 to i32
switch i32 %20, label %34 [
i32 111, label %21
i32 79, label %23
i32 70, label %26
i32 82, label %28
i32 86, label %31
]
21: ; preds = %17
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(17) %8, i8* align 1 dereferenceable(17) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 17, i1 false)
%22 = getelementptr inbounds i8, i8* %8, i64 16
br label %40
23: ; preds = %17
%24 = bitcast i8* %8 to i64*
store i64 17577228358545212, i64* %24
%25 = getelementptr inbounds i8, i8* %8, i64 7
br label %40
26: ; preds = %17
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(7) %8, i8* align 1 dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64 7, i1 false)
%27 = getelementptr inbounds i8, i8* %8, i64 6
br label %40
28: ; preds = %17
%29 = bitcast i8* %8 to i64*
store i64 17562943463977532, i64* %29
%30 = getelementptr inbounds i8, i8* %8, i64 7
br label %40
31: ; preds = %17
%32 = bitcast i8* %8 to i64*
store i64 17563003559966268, i64* %32
%33 = getelementptr inbounds i8, i8* %8, i64 7
br label %40
34: ; preds = %17
%35 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 %20)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %8, i64 %36
br label %40
38: ; preds = %15
%39 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %7, i8* %8
br label %40
40: ; preds = %21, %23, %26, %28, %31, %34, %38
%41 = phi i8* [ %18, %34 ], [ %18, %31 ], [ %18, %28 ], [ %18, %26 ], [ %18, %23 ], [ %18, %21 ], [ %10, %38 ]
%42 = phi i8* [ %37, %34 ], [ %33, %31 ], [ %30, %28 ], [ %27, %26 ], [ %25, %23 ], [ %22, %21 ], [ %39, %38 ]
%43 = load i8, i8* %41
%44 = icmp eq i8 %43, 0
br i1 %44, label %45, label %6
45: ; preds = %40, %3
%46 = phi i8* [ %0, %3 ], [ %42, %40 ]
store i8 0, i8* %46
ret void
}