#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 */
typedef int uint;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int UINT_MAX;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printf(char *, ...);
int *xmalloc(int);
void xvienna_to_pairs(uint n, const char *vienna, uint *pairs) {
  uint i, j;
  uint *stack, idx_stack = 0;
  stack = xmalloc(n * sizeof(*stack));
  for (i = 0; i < n; i++) {
    switch (vienna[i]) {
    case '(':
      stack[idx_stack++] = i;
      break;
    case ')':
      if (idx_stack > 0) {
        j = stack[--idx_stack];
        pairs[i] = j;
        pairs[j] = i;
      } else {
        printf("ERROR: too many closing parentheses in vienna"
               " string %s\n",
               vienna);
        exit(EXIT_FAILURE);
      }
      break;
    case '.':
      pairs[i] = (UINT_MAX / 2);
      break;
    default:
      printf("ERROR: illegal character '%c' in vienna string '%s'\n", vienna[i],
             vienna);
      exit(EXIT_FAILURE);
      break;
    }
  }
  if (idx_stack != 0) {
    printf("ERROR: not enough closing parentheses in vienna string %s\n",
           vienna);
    exit(EXIT_FAILURE);
  }
  free(stack);
}


x86 assembly (clang, -O0):
<xvienna_to_pairs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12a4 <xvienna_to_pairs+0x164>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x30(%rbp)
sub    $0x28,%eax
je     11bf <xvienna_to_pairs+0x7f>
jmp    119d <xvienna_to_pairs+0x5d>
mov    -0x30(%rbp),%eax
sub    $0x29,%eax
je     11dc <xvienna_to_pairs+0x9c>
jmp    11ae <xvienna_to_pairs+0x6e>
mov    -0x30(%rbp),%eax
sub    $0x2e,%eax
je     1242 <xvienna_to_pairs+0x102>
jmp    1265 <xvienna_to_pairs+0x125>
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1291 <xvienna_to_pairs+0x151>
cmpl   $0x0,-0x2c(%rbp)
jle    121d <xvienna_to_pairs+0xdd>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x2c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    123d <xvienna_to_pairs+0xfd>
mov    -0x10(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1291 <xvienna_to_pairs+0x151>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1291 <xvienna_to_pairs+0x151>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
mov    -0x10(%rbp),%rdx
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1296 <xvienna_to_pairs+0x156>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1174 <xvienna_to_pairs+0x34>
cmpl   $0x0,-0x2c(%rbp)
je     12ce <xvienna_to_pairs+0x18e>
mov    -0x10(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xvienna_to_pairs>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x0(,%r15,4),%edi
call   1050 <xmalloc@plt>
test   %r15d,%r15d
jle    11d9 <xvienna_to_pairs+0x99>
mov    %r15d,%ecx
xor    %edx,%edx
mov    0x2e79(%rip),%r8
xor    %edi,%edi
jmp    1199 <xvienna_to_pairs+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r8),%ebx
mov    %ebx,%esi
shr    $0x1f,%esi
add    %ebx,%esi
sar    %esi
mov    %esi,(%r12,%rdx,4)
add    $0x1,%rdx
cmp    %rdx,%rcx
je     11d5 <xvienna_to_pairs+0x95>
movsbl (%r14,%rdx,1),%esi
cmp    $0x2e,%esi
je     1180 <xvienna_to_pairs+0x40>
cmp    $0x29,%esi
je     11c0 <xvienna_to_pairs+0x80>
cmp    $0x28,%esi
jne    11ec <xvienna_to_pairs+0xac>
movslq %edi,%rsi
add    $0x1,%edi
mov    %edx,(%rax,%rsi,4)
jmp    1190 <xvienna_to_pairs+0x50>
nopl   0x0(%rax,%rax,1)
test   %edi,%edi
jle    11ff <xvienna_to_pairs+0xbf>
add    $0xffffffff,%edi
movslq (%rax,%rdi,4),%rsi
mov    %esi,(%r12,%rdx,4)
mov    %edx,(%r12,%rsi,4)
jmp    1190 <xvienna_to_pairs+0x50>
test   %edi,%edi
jne    121e <xvienna_to_pairs+0xde>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>
lea    0xe46(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1210 <xvienna_to_pairs+0xd0>
lea    0xdfa(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe49(%rip),%rdi
jmp    1206 <xvienna_to_pairs+0xc6>

LLVM IR (-O0):
@.str = [57 x i8] c"ERROR: too many closing parentheses in vienna string %s\0A\00"
@EXIT_FAILURE = global i32 0
@UINT_MAX = global i32 0
@.str.1 = [53 x i8] c"ERROR: illegal character '%c' in vienna string '%s'\0A\00"
@.str.2 = [59 x i8] c"ERROR: not enough closing parentheses in vienna string %s\0A\00"
define void @xvienna_to_pairs(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i32 0, i32* %10
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 4
%14 = trunc i64 %13 to i32
%15 = call i32* @xmalloc(i32 %14)
store i32* %15, i32** %9
store i32 0, i32* %7
br label %16
16: ; preds = %79, %3
%17 = load i32, i32* %7
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %82
20: ; preds = %16
%21 = load i8*, i8** %5
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
switch i32 %26, label %67 [
i32 40, label %27
i32 41, label %34
i32 46, label %60
]
27: ; preds = %20
%28 = load i32, i32* %7
%29 = load i32*, i32** %9
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i32, i32* %29, i64 %32
store i32 %28, i32* %33
br label %78
34: ; preds = %20
%35 = load i32, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %54
37: ; preds = %34
%38 = load i32*, i32** %9
%39 = load i32, i32* %10
%40 = add nsw i32 %39, -1
store i32 %40, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %38, i64 %41
%43 = load i32, i32* %42
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = load i32*, i32** %6
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %44, i32* %48
%49 = load i32, i32* %7
%50 = load i32*, i32** %6
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
store i32 %49, i32* %53
br label %59
54: ; preds = %34
%55 = load i8*, i8** %5
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %37
br label %78
60: ; preds = %20
%61 = load i32, i32* @UINT_MAX
%62 = sdiv i32 %61, 2
%63 = load i32*, i32** %6
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
store i32 %62, i32* %66
br label %78
67: ; preds = %20
%68 = load i8*, i8** %5
%69 = load i32, i32* %7
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = load i8*, i8** %5
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %73, i8* %74)
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %60, %59, %27
br label %79
79: ; preds = %78
%80 = load i32, i32* %7
%81 = add nsw i32 %80, 1
store i32 %81, i32* %7
br label %16
82: ; preds = %16
%83 = load i32, i32* %10
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %90
85: ; preds = %82
%86 = load i8*, i8** %5
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %86)
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %82
%91 = load i32*, i32** %9
%92 = call i32 @free(i32* %91)
ret void
}

LLVM IR (-O2):
@.str = [57 x i8] c"ERROR: too many closing parentheses in vienna string %s\0A\00"
@EXIT_FAILURE = global i32 0
@UINT_MAX = global i32 0
@.str.1 = [53 x i8] c"ERROR: illegal character '%c' in vienna string '%s'\0A\00"
@.str.2 = [59 x i8] c"ERROR: not enough closing parentheses in vienna string %s\0A\00"
define void @xvienna_to_pairs(i32 %0, i8* %1, i32* nocapture writeonly %2) {
%4 = shl i32 %0, 2
%5 = tail call i32* @xmalloc(i32 %4)
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %54
7: ; preds = %3
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %7, %44
%10 = phi i64 [ 0, %7 ], [ %46, %44 ]
%11 = phi i32 [ 0, %7 ], [ %45, %44 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %10
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
switch i32 %14, label %39 [
i32 40, label %15
i32 41, label %20
i32 46, label %35
]
15: ; preds = %9
%16 = add nsw i32 %11, 1
%17 = sext i32 %11 to i64
%18 = getelementptr inbounds i32, i32* %5, i64 %17
%19 = trunc i64 %10 to i32
store i32 %19, i32* %18
br label %44
20: ; preds = %9
%21 = icmp sgt i32 %11, 0
br i1 %21, label %22, label %31
22: ; preds = %20
%23 = add nsw i32 %11, -1
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %5, i64 %24
%26 = load i32, i32* %25
%27 = getelementptr inbounds i32, i32* %2, i64 %10
store i32 %26, i32* %27
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i32, i32* %2, i64 %28
%30 = trunc i64 %10 to i32
store i32 %30, i32* %29
br label %44
31: ; preds = %20
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %1)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %9
%36 = load i32, i32* @UINT_MAX
%37 = sdiv i32 %36, 2
%38 = getelementptr inbounds i32, i32* %2, i64 %10
store i32 %37, i32* %38
br label %44
39: ; preds = %9
%40 = sext i8 %13 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* %1)
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %15, %22, %35
%45 = phi i32 [ %11, %35 ], [ %23, %22 ], [ %16, %15 ]
%46 = add nuw nsw i64 %10, 1
%47 = icmp eq i64 %46, %8
br i1 %47, label %48, label %9
48: ; preds = %44
%49 = icmp eq i32 %45, 0
br i1 %49, label %54, label %50
50: ; preds = %48
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%52 = load i32, i32* @EXIT_FAILURE
%53 = tail call i32 @exit(i32 %52)
unreachable
54: ; preds = %3, %48
%55 = tail call i32 @free(i32* %5)
ret void
}