#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*/ brackets_checker(char *, int);
int /*<<< orphan*/ free(char *);
char getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int scanf(char *, int *);
scalar_t__ strchr(char *, char);
int main(void) {
  char *input_string, valid_chars[] = {'{', '}', '[', ']', '(', ')'}, tmp_char;
  int ele_num = 0, array_dim, scanf_result = 0;
  do {
    printf("Number of brackets: ");
    scanf_result = scanf("%d", &array_dim);
    if ((scanf_result != 1) || (array_dim <= 0))
      printf("Wrong input!\n");
    do {
      tmp_char = getchar();
    } while (tmp_char != '\n');
  } while ((scanf_result != 1) || (array_dim <= 0));
  input_string = (char *)malloc(sizeof(char) * array_dim);
  printf("Insert a sequence of parentheses, square and curly brackets.\n"
         "(All invalid and exceeding characters will be ignored)\n"
         "Insert: ");
  do {
    tmp_char = getchar();
    if (strchr(valid_chars, tmp_char) && ele_num < array_dim) {
      input_string[ele_num] = tmp_char;
      ele_num++;
    }
  } while (tmp_char != '\n');
  printf("\nLongest prefix: ");
  brackets_checker(input_string, ele_num);
  printf("\n");
  free(input_string);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7b(%rip),%eax
mov    %eax,-0x16(%rbp)
mov    0xe75(%rip),%ax
mov    %ax,-0x12(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe5e(%rip),%rdi
call   1060 <printf@plt>
lea    0xe67(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <scanf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    11d4 <main+0x64>
cmpl   $0x0,-0x20(%rbp)
jg     11e0 <main+0x70>
lea    0xe43(%rip),%rdi
call   1060 <printf@plt>
jmp    11e5 <main+0x75>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    %al,-0x17(%rbp)
movsbl -0x17(%rbp),%eax
cmp    $0xa,%eax
jne    11e5 <main+0x75>
jmp    1201 <main+0x91>
mov    $0x1,%al
cmpl   $0x1,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jne    121a <main+0xaa>
cmpl   $0x0,-0x20(%rbp)
setle  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11a1 <main+0x31>
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    0xded(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    %al,-0x17(%rbp)
lea    -0x16(%rbp),%rdi
movsbl -0x17(%rbp),%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     1288 <main+0x118>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1288 <main+0x118>
mov    -0x17(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    128d <main+0x11d>
movsbl -0x17(%rbp),%eax
cmp    $0xa,%eax
jne    1244 <main+0xd4>
lea    0xe08(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <brackets_checker@plt>
lea    0xd71(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movw   $0x2928,0xc(%rsp)
movl   $0x5d5b7d7b,0x8(%rsp)
5d
lea    0xe54(%rip),%r14
lea    0xe62(%rip),%r15
lea    0x4(%rsp),%rbx
lea    0xee8(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1080 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <scanf@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    11f1 <main+0x61>
cmpl   $0x0,0x4(%rsp)
jg     1200 <main+0x70>
mov    %r12,%rdi
call   1060 <puts@plt>
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    $0xa,%al
jne    1200 <main+0x70>
cmp    $0x1,%ebp
jne    11d0 <main+0x40>
mov    0x4(%rsp),%edi
test   %edi,%edi
jle    11d0 <main+0x40>
call   10a0 <malloc@plt>
mov    %rax,%r14
lea    0xdf7(%rip),%rdi
call   1080 <printf@plt>
lea    0x8(%rsp),%r15
xor    %r12d,%r12d
jmp    1245 <main+0xb5>
cs nopw 0x0(%rax,%rax,1)
cmp    $0xa,%bl
je     1272 <main+0xe2>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %eax,%ebx
movsbl %al,%esi
mov    %r15,%rdi
call   1070 <strchr@plt>
test   %rax,%rax
je     1240 <main+0xb0>
cmp    0x4(%rsp),%r12d
jge    1240 <main+0xb0>
movslq %r12d,%r12
mov    %bl,(%r14,%r12,1)
add    $0x1,%r12d
jmp    1240 <main+0xb0>
lea    0xe22(%rip),%rdi
call   1080 <printf@plt>
mov    %r14,%rdi
mov    %r12d,%esi
call   1030 <brackets_checker@plt>
mov    $0xa,%edi
call   1050 <putchar@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.valid_chars = [6 x i8] c"{}[]()"
@.str = [21 x i8] c"Number of brackets: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"Wrong input!\0A\00"
@.str.3 = [125 x i8] c"Insert a sequence of parentheses, square and curly brackets.\0A(All invalid and exceeding characters will be ignored)\0AInsert: \00"
@.str.4 = [18 x i8] c"\0ALongest prefix: \00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [6 x i8]
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [6 x i8]* %3 to i8*
store i32 0, i32* %5
store i32 0, i32* %7
br label %9
9: ; preds = %33, %0
%10 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = load i32, i32* %6
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14, %9
%18 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
br label %20
20: ; preds = %22, %19
%21 = call signext i8 (...) @getchar()
store i8 %21, i8* %4
br label %22
22: ; preds = %20
%23 = load i8, i8* %4
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 10
br i1 %25, label %20, label %26
26: ; preds = %22
br label %27
27: ; preds = %26
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 1
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = load i32, i32* %6
%32 = icmp sle i32 %31, 0
br label %33
33: ; preds = %30, %27
%34 = phi i1 [ true, %27 ], [ %32, %30 ]
br i1 %34, label %9, label %35
35: ; preds = %33
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = mul i64 1, %37
%39 = trunc i64 %38 to i32
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %2
%42 = call i32 @printf(i8* getelementptr inbounds ([125 x i8], [125 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %62, %35
%44 = call signext i8 (...) @getchar()
store i8 %44, i8* %4
%45 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%46 = load i8, i8* %4
%47 = call i64 @strchr(i8* %45, i8 signext %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %61
49: ; preds = %43
%50 = load i32, i32* %5
%51 = load i32, i32* %6
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %61
53: ; preds = %49
%54 = load i8, i8* %4
%55 = load i8*, i8** %2
%56 = load i32, i32* %5
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 %54, i8* %58
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %61
61: ; preds = %53, %49, %43
br label %62
62: ; preds = %61
%63 = load i8, i8* %4
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 10
br i1 %65, label %43, label %66
66: ; preds = %62
%67 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%68 = load i8*, i8** %2
%69 = load i32, i32* %5
%70 = call i32 @brackets_checker(i8* %68, i32 %69)
%71 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%72 = load i8*, i8** %2
%73 = call i32 @free(i8* %72)
ret i32 0
}

LLVM IR (-O2):
@__const.main.valid_chars = [6 x i8] c"{}[]()"
@.str = [21 x i8] c"Number of brackets: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [125 x i8] c"Insert a sequence of parentheses, square and curly brackets.\0A(All invalid and exceeding characters will be ignored)\0AInsert: \00"
@.str.4 = [18 x i8] c"\0ALongest prefix: \00"
@str = [13 x i8] c"Wrong input!\00"
define i32 @main() {
%1 = alloca [6 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %18, %0
%6 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%8 = icmp ne i32 %7, 1
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 1
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %14
14: ; preds = %5, %12
br label %15
15: ; preds = %14, %15
%16 = call signext i8 (...) @getchar()
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %15
18: ; preds = %15
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 1
%21 = select i1 %8, i1 true, i1 %20
br i1 %21, label %5, label %22
22: ; preds = %18
%23 = call i64 @malloc(i32 %19)
%24 = inttoptr i64 %23 to i8*
%25 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([125 x i8], [125 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %38, %22
%27 = phi i32 [ 0, %22 ], [ %39, %38 ]
%28 = call signext i8 (...) @getchar()
%29 = call i64 @strchr(i8* %3, i8 signext %28)
%30 = icmp ne i64 %29, 0
%31 = load i32, i32* %2
%32 = icmp slt i32 %27, %31
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %34, label %38
34: ; preds = %26
%35 = sext i32 %27 to i64
%36 = getelementptr inbounds i8, i8* %24, i64 %35
store i8 %28, i8* %36
%37 = add nsw i32 %27, 1
br label %38
38: ; preds = %26, %34
%39 = phi i32 [ %37, %34 ], [ %27, %26 ]
%40 = icmp eq i8 %28, 10
br i1 %40, label %41, label %26
41: ; preds = %38
%42 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @brackets_checker(i8* %24, i32 %39)
%44 = call i32 @putchar(i32 10)
%45 = call i32 @free(i8* %24)
ret i32 0
}