#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*/ memmove(char *, char *, scalar_t__);
scalar_t__ strlen(char *);
char *strstr(char *, char *);
char *expand_trigraphs(char *line) {
  static const struct {
    char from, to;
  } map[] = {{'(', '['},  {')', ']'},  {'<', '{'}, {'>', '}'}, {'=', '#'},
             {'/', '\\'}, {'\'', '^'}, {'!', '|'}, {'-', '~'}, {0, 0}};
  int i;
  char *anchor = line;
  for (;;) {
    char *qmark = strstr(anchor, "??");
    if (!qmark)
      break;
    anchor = qmark + 1;
    for (i = 0; map[i].from; i++) {
      if (map[i].from == qmark[2]) {
        qmark[0] = map[i].to;
        memmove(qmark + 1, qmark + 3, strlen(qmark + 3) + 1);
        break;
      }
    }
  }
  return line;
}


x86 assembly (clang, -O0):
<expand_trigraphs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1168 <expand_trigraphs+0x38>
jmp    121a <expand_trigraphs+0xea>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rcx
lea    0xe7a(%rip),%rax
cmpb   $0x0,(%rax,%rcx,2)
je     1215 <expand_trigraphs+0xe5>
movslq -0xc(%rbp),%rcx
lea    0xe65(%rip),%rax
movsbl (%rax,%rcx,2),%eax
mov    -0x20(%rbp),%rcx
movsbl 0x2(%rcx),%ecx
cmp    %ecx,%eax
jne    1202 <expand_trigraphs+0xd2>
movslq -0xc(%rbp),%rcx
lea    0xe46(%rip),%rax
mov    0x1(%rax,%rcx,2),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x3,%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <memmove@plt>
jmp    1215 <expand_trigraphs+0xe5>
jmp    1207 <expand_trigraphs+0xd7>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117b <expand_trigraphs+0x4b>
jmp    1144 <expand_trigraphs+0x14>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_trigraphs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xf4f(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1206 <expand_trigraphs+0xd6>
mov    %rax,%rbx
lea    0xf37(%rip),%r15
lea    0xe9c(%rip),%r13
lea    0xf15(%rip),%rbp
jmp    11b5 <expand_trigraphs+0x85>
mov    $0x8,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x1(%rbp,%rax,2),%eax
mov    %al,(%rbx)
add    $0x3,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <memmove@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1206 <expand_trigraphs+0xd6>
lea    0x1(%rbx),%r12
movzbl 0x2(%rbx),%eax
add    $0xdf,%al
cmp    $0x1d,%al
ja     11a2 <expand_trigraphs+0x72>
movzbl %al,%eax
movslq 0x0(%r13,%rax,4),%rcx
add    %r13,%rcx
xor    %eax,%eax
jmp    *%rcx
mov    $0x7,%eax
jmp    1180 <expand_trigraphs+0x50>
mov    $0x6,%eax
jmp    1180 <expand_trigraphs+0x50>
mov    $0x1,%eax
jmp    1180 <expand_trigraphs+0x50>
mov    $0x5,%eax
jmp    1180 <expand_trigraphs+0x50>
mov    $0x2,%eax
jmp    1180 <expand_trigraphs+0x50>
mov    $0x4,%eax
jmp    1180 <expand_trigraphs+0x50>
mov    $0x3,%eax
jmp    1180 <expand_trigraphs+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.anon = type { i8, i8 }
@expand_trigraphs.map = internal constant [10 x %struct.anon] [%struct.anon { i8 40, i8 91 }, %struct.anon { i8 41, i8 93 }, %struct.anon { i8 60, i8 123 }, %struct.anon { i8 62, i8 125 }, %struct.anon { i8 61, i8 35 }, %struct.anon { i8 47, i8 92 }, %struct.anon { i8 39, i8 94 }, %struct.anon { i8 33, i8 124 }, %struct.anon { i8 45, i8 126 }, %struct.anon zeroinitializer]
@.str = [3 x i8] c"??\00"
define i8* @expand_trigraphs(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %56, %1
%8 = load i8*, i8** %4
%9 = call i8* @strstr(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %7
br label %57
13: ; preds = %7
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8* %15, i8** %4
store i32 0, i32* %3
br label %16
16: ; preds = %53, %13
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [10 x %struct.anon], [10 x %struct.anon]* @expand_trigraphs.map, i64 0, i64 %18
%20 = getelementptr inbounds %struct.anon, %struct.anon* %19, i32 0, i32 0
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %23, label %56
23: ; preds = %16
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [10 x %struct.anon], [10 x %struct.anon]* @expand_trigraphs.map, i64 0, i64 %25
%27 = getelementptr inbounds %struct.anon, %struct.anon* %26, i32 0, i32 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 2
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %29, %33
br i1 %34, label %35, label %52
35: ; preds = %23
%36 = load i32, i32* %3
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [10 x %struct.anon], [10 x %struct.anon]* @expand_trigraphs.map, i64 0, i64 %37
%39 = getelementptr inbounds %struct.anon, %struct.anon* %38, i32 0, i32 1
%40 = load i8, i8* %39
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 0
store i8 %40, i8* %42
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = load i8*, i8** %5
%46 = getelementptr inbounds i8, i8* %45, i64 3
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i64 3
%49 = call i64 @strlen(i8* %48)
%50 = add nsw i64 %49, 1
%51 = call i32 @memmove(i8* %44, i8* %46, i64 %50)
br label %56
52: ; preds = %23
br label %53
53: ; preds = %52
%54 = load i32, i32* %3
%55 = add nsw i32 %54, 1
store i32 %55, i32* %3
br label %16
56: ; preds = %35, %16
br label %7
57: ; preds = %12
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
%struct.anon = type { i8, i8 }
@expand_trigraphs.map = internal unnamed_addr constant [10 x %struct.anon] [%struct.anon { i8 40, i8 91 }, %struct.anon { i8 41, i8 93 }, %struct.anon { i8 60, i8 123 }, %struct.anon { i8 62, i8 125 }, %struct.anon { i8 61, i8 35 }, %struct.anon { i8 47, i8 92 }, %struct.anon { i8 39, i8 94 }, %struct.anon { i8 33, i8 124 }, %struct.anon { i8 45, i8 126 }, %struct.anon zeroinitializer]
@.str = [3 x i8] c"??\00"
define i8* @expand_trigraphs(i8* returned %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %28, label %4
4: ; preds = %1, %25
%5 = phi i8* [ %26, %25 ], [ %2, %1 ]
%6 = getelementptr inbounds i8, i8* %5, i64 1
%7 = getelementptr inbounds i8, i8* %5, i64 2
%8 = load i8, i8* %7
switch i8 %8, label %25 [
i8 40, label %17
i8 41, label %9
i8 60, label %10
i8 62, label %11
i8 61, label %12
i8 47, label %13
i8 39, label %14
i8 33, label %15
i8 45, label %16
]
9: ; preds = %4
br label %17
10: ; preds = %4
br label %17
11: ; preds = %4
br label %17
12: ; preds = %4
br label %17
13: ; preds = %4
br label %17
14: ; preds = %4
br label %17
15: ; preds = %4
br label %17
16: ; preds = %4
br label %17
17: ; preds = %4, %16, %15, %14, %13, %12, %11, %10, %9
%18 = phi i64 [ 0, %4 ], [ 1, %9 ], [ 2, %10 ], [ 3, %11 ], [ 4, %12 ], [ 5, %13 ], [ 6, %14 ], [ 7, %15 ], [ 8, %16 ]
%19 = getelementptr inbounds [10 x %struct.anon], [10 x %struct.anon]* @expand_trigraphs.map, i64 0, i64 %18, i32 1
%20 = load i8, i8* %19
store i8 %20, i8* %5
%21 = getelementptr inbounds i8, i8* %5, i64 3
%22 = tail call i64 @strlen(i8* %21)
%23 = add nsw i64 %22, 1
%24 = tail call i32 @memmove(i8* %6, i8* %21, i64 %23)
br label %25
25: ; preds = %4, %17
%26 = tail call i8* @strstr(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %4
28: ; preds = %25, %1
ret i8* %0
}