#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 /*<<< orphan*/ fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fsetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int parens_match(FILE *in) {
  int open_parens = 0;
  int close_parens = 0;
  int c;
  fpos_t file_start;
  fgetpos(in, &file_start);
  while ((c = fgetc(in)) != EOF) {
    if (c == '[') {
      ++open_parens;
    } else if (c == ']') {
      ++close_parens;
    }
  }
  fsetpos(in, &file_start);
  return open_parens == close_parens;
}


x86 assembly (clang, -O0):
<parens_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <fgetpos@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%eax
je     11a7 <parens_match+0x77>
cmpl   $0x5b,-0x14(%rbp)
jne    118a <parens_match+0x5a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a2 <parens_match+0x72>
cmpl   $0x5d,-0x14(%rbp)
jne    119d <parens_match+0x6d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a2 <parens_match+0x72>
jmp    1157 <parens_match+0x27>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <fsetpos@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parens_match>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x4(%rsp),%rsi
call   1030 <fgetpos@plt>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    0x2e95(%rip),%r15
xor    %r14d,%r14d
mov    $0x0,%ebp
cmp    (%r15),%eax
jne    1180 <parens_match+0x50>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <fsetpos@plt>
xor    %eax,%eax
cmp    %r14d,%ebp
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    11a1 <parens_match+0x71>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14d
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%eax
je     1160 <parens_match+0x30>
cmp    $0x5d,%eax
je     1190 <parens_match+0x60>
cmp    $0x5b,%eax
jne    1194 <parens_match+0x64>
add    $0x1,%ebp
jmp    1194 <parens_match+0x64>

LLVM IR (-O0):
@EOF = global i32 0
define i32 @parens_match(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %3
store i32 0, i32* %4
%7 = load i32*, i32** %2
%8 = call i32 @fgetpos(i32* %7, i32* %6)
br label %9
9: ; preds = %27, %1
%10 = load i32*, i32** %2
%11 = call i32 @fgetc(i32* %10)
store i32 %11, i32* %5
%12 = load i32, i32* @EOF
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %28
14: ; preds = %9
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 91
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
store i32 %19, i32* %3
br label %27
20: ; preds = %14
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 93
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %26
26: ; preds = %23, %20
br label %27
27: ; preds = %26, %17
br label %9
28: ; preds = %9
%29 = load i32*, i32** %2
%30 = call i32 @fsetpos(i32* %29, i32* %6)
%31 = load i32, i32* %3
%32 = load i32, i32* %4
%33 = icmp eq i32 %31, %32
%34 = zext i1 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
@EOF = global i32 0
define i32 @parens_match(i32* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @fgetpos(i32* %0, i32* %2)
%5 = tail call i32 @fgetc(i32* %0)
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %22, label %8
8: ; preds = %1, %16
%9 = phi i32 [ %19, %16 ], [ %5, %1 ]
%10 = phi i32 [ %18, %16 ], [ 0, %1 ]
%11 = phi i32 [ %17, %16 ], [ 0, %1 ]
switch i32 %9, label %16 [
i32 91, label %12
i32 93, label %14
]
12: ; preds = %8
%13 = add nsw i32 %10, 1
br label %16
14: ; preds = %8
%15 = add nsw i32 %11, 1
br label %16
16: ; preds = %8, %14, %12
%17 = phi i32 [ %11, %12 ], [ %15, %14 ], [ %11, %8 ]
%18 = phi i32 [ %13, %12 ], [ %10, %14 ], [ %10, %8 ]
%19 = tail call i32 @fgetc(i32* %0)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %8
22: ; preds = %16, %1
%23 = phi i32 [ 0, %1 ], [ %17, %16 ]
%24 = phi i32 [ 0, %1 ], [ %18, %16 ]
%25 = call i32 @fsetpos(i32* %0, i32* %2)
%26 = icmp eq i32 %24, %23
%27 = zext i1 %26 to i32
ret i32 %27
}