#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*/ printf(char *);
int /*<<< orphan*/ scanf(char *, char **);
int main() {
  char expr[100];
  int bf = 0;
  int i;
  printf("Enter expression: ");
  scanf("%s", &expr);
  i = 0;
  while (expr[i] != '\0') {
    if (expr[i] == '(')
      bf++;
    else if (expr[i] == ')') {
      bf--;
      if (bf < 0)
        break;
    }
    i++;
  }
  if (bf == 0)
    printf("Balanced parenthesis.\n");
  else
    printf("Unbalanced.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x74(%rbp)
lea    0xec0(%rip),%rdi
call   1030 <printf@plt>
lea    -0x70(%rbp),%rsi
lea    0xec3(%rip),%rdi
call   1040 <scanf@plt>
movl   $0x0,-0x78(%rbp)
movslq -0x78(%rbp),%rax
movsbl -0x70(%rbp,%rax,1),%eax
cmp    $0x0,%eax
je     11d0 <main+0xb0>
movslq -0x78(%rbp),%rax
movsbl -0x70(%rbp,%rax,1),%eax
cmp    $0x28,%eax
jne    118e <main+0x6e>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11c2 <main+0xa2>
movslq -0x78(%rbp),%rax
movsbl -0x70(%rbp,%rax,1),%eax
cmp    $0x29,%eax
jne    11bd <main+0x9d>
mov    -0x74(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    11b8 <main+0x98>
jmp    11d0 <main+0xb0>
jmp    11bd <main+0x9d>
jmp    11c2 <main+0xa2>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    115c <main+0x3c>
cmpl   $0x0,-0x74(%rbp)
jne    11eb <main+0xcb>
lea    0xe35(%rip),%rdi
call   1030 <printf@plt>
jmp    11f7 <main+0xd7>
lea    0xe3b(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x70,%rsp
lea    0xec4(%rip),%rdi
call   1040 <printf@plt>
lea    0xecb(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1050 <scanf@plt>
xor    %eax,%eax
jmp    1167 <main+0x37>
nopw   0x0(%rax,%rax,1)
add    $0x1,%eax
add    $0x1,%rbx
movzbl (%rbx),%ecx
cmp    $0x28,%cl
je     1160 <main+0x30>
cmp    $0x29,%cl
je     1180 <main+0x50>
test   %cl,%cl
jne    1187 <main+0x57>
jmp    118d <main+0x5d>
nopw   0x0(%rax,%rax,1)
test   %eax,%eax
jle    1191 <main+0x61>
add    $0xffffffff,%eax
add    $0x1,%rbx
jmp    1167 <main+0x37>
test   %eax,%eax
je     119a <main+0x6a>
lea    0xe7e(%rip),%rdi
jmp    11a1 <main+0x71>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Enter expression: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [23 x i8] c"Balanced parenthesis.\0A\00"
@.str.3 = [13 x i8] c"Unbalanced.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%5 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%6 = bitcast [100 x i8]* %2 to i8**
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %6)
store i32 0, i32* %4
br label %8
8: ; preds = %40, %0
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %8
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 40
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %40
25: ; preds = %15
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 41
br i1 %31, label %32, label %39
32: ; preds = %25
%33 = load i32, i32* %3
%34 = add nsw i32 %33, -1
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32
br label %43
38: ; preds = %32
br label %39
39: ; preds = %38, %25
br label %40
40: ; preds = %39, %22
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %8
43: ; preds = %37, %8
%44 = load i32, i32* %3
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %50
48: ; preds = %43
%49 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %46
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"Enter expression: \00"
@.str.1 = [3 x i8] c"%s\00"
@str = [12 x i8] c"Unbalanced.\00"
@str.4 = [22 x i8] c"Balanced parenthesis.\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%4 = bitcast [100 x i8]* %1 to i8**
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %4)
br label %6
6: ; preds = %16, %0
%7 = phi i64 [ %18, %16 ], [ 0, %0 ]
%8 = phi i32 [ %17, %16 ], [ 0, %0 ]
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %7
%10 = load i8, i8* %9
switch i8 %10, label %16 [
i8 0, label %19
i8 40, label %11
i8 41, label %13
]
11: ; preds = %6
%12 = add nsw i32 %8, 1
br label %16
13: ; preds = %6
%14 = add nsw i32 %8, -1
%15 = icmp slt i32 %8, 1
br i1 %15, label %21, label %16
16: ; preds = %6, %13, %11
%17 = phi i32 [ %12, %11 ], [ %14, %13 ], [ %8, %6 ]
%18 = add nuw i64 %7, 1
br label %6
19: ; preds = %6
%20 = icmp eq i32 %8, 0
br i1 %20, label %22, label %21
21: ; preds = %13, %19
br label %22
22: ; preds = %19, %21
%23 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), %21 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @str.4, i64 0, i64 0), %19 ]
%24 = call i32 @puts(i8* dereferenceable(1) %23)
ret i32 0
}