#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 */
struct s_stack {
  int dummy;
};

/* Variables and functions */
struct s_stack *createstack(int);
int /*<<< orphan*/ displaystack(struct s_stack *);
int /*<<< orphan*/ isEMPTY(struct s_stack *);
int peek(struct s_stack *);
int /*<<< orphan*/ pop(struct s_stack *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ push(struct s_stack *, char);
int strlen(char *);
void balancecheck(char str[]) {
  int n = strlen(str);
  int i;
  struct s_stack *stack = createstack(n);
  for (i = 0; i < n; i++) {
    if (str[i] == '(') {
      push(stack, str[i]);
      displaystack(stack);
    } else if (str[i] == ')') {
      if (!isEMPTY(stack) && peek(stack) == '(')
        pop(stack);
      else {
        printf("\n%d", peek(stack));
        printf("\nUnbalanced Parenthesis");
      }
    }
  }
  printf("\n%d", peek(stack));
  if (!isEMPTY(stack))
    printf("\nUnbalaced");
}


x86 assembly (clang, -O0):
<balancecheck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   10a0 <createstack@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    127a <balancecheck+0xfa>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x28,%eax
jne    11ef <balancecheck+0x6f>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
call   1090 <push@plt>
mov    -0x18(%rbp),%rdi
call   1080 <displaystack@plt>
jmp    1267 <balancecheck+0xe7>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x29,%eax
jne    1262 <balancecheck+0xe2>
mov    -0x18(%rbp),%rdi
call   1070 <isEMPTY@plt>
cmp    $0x0,%eax
jne    1236 <balancecheck+0xb6>
mov    -0x18(%rbp),%rdi
call   1030 <peek@plt>
cmp    $0x28,%eax
jne    1236 <balancecheck+0xb6>
mov    -0x18(%rbp),%rdi
call   1060 <pop@plt>
jmp    125d <balancecheck+0xdd>
mov    -0x18(%rbp),%rdi
call   1030 <peek@plt>
mov    %eax,%esi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1262 <balancecheck+0xe2>
jmp    1267 <balancecheck+0xe7>
jmp    126c <balancecheck+0xec>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ab <balancecheck+0x2b>
mov    -0x18(%rbp),%rdi
call   1030 <peek@plt>
mov    %eax,%esi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <isEMPTY@plt>
cmp    $0x0,%eax
jne    12b3 <balancecheck+0x133>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<balancecheck>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   10a0 <createstack@plt>
mov    %rax,%rbx
test   %ebp,%ebp
jle    122e <balancecheck+0xae>
mov    %ebp,%r13d
lea    0xe4f(%rip),%r15
lea    0xe4c(%rip),%r12
xor    %ebp,%ebp
jmp    11f3 <balancecheck+0x73>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <isEMPTY@plt>
test   %eax,%eax
je     1217 <balancecheck+0x97>
mov    %rbx,%rdi
call   1030 <peek@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
je     122e <balancecheck+0xae>
movzbl (%r14,%rbp,1),%eax
cmp    $0x29,%al
je     11c0 <balancecheck+0x40>
cmp    $0x28,%al
jne    11ea <balancecheck+0x6a>
mov    %rbx,%rdi
mov    $0x28,%esi
call   1090 <push@plt>
mov    %rbx,%rdi
call   1080 <displaystack@plt>
jmp    11ea <balancecheck+0x6a>
mov    %rbx,%rdi
call   1030 <peek@plt>
cmp    $0x28,%eax
jne    11cc <balancecheck+0x4c>
mov    %rbx,%rdi
call   1060 <pop@plt>
jmp    11ea <balancecheck+0x6a>
mov    %rbx,%rdi
call   1030 <peek@plt>
lea    0xdc3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <isEMPTY@plt>
test   %eax,%eax
je     1261 <balancecheck+0xe1>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
%struct.s_stack = type { i32 }
@.str = [4 x i8] c"\0A%d\00"
@.str.1 = [24 x i8] c"\0AUnbalanced Parenthesis\00"
@.str.2 = [11 x i8] c"\0AUnbalaced\00"
define void @balancecheck(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.s_stack*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = call %struct.s_stack* @createstack(i32 %8)
store %struct.s_stack* %9, %struct.s_stack** %5
store i32 0, i32* %4
br label %10
10: ; preds = %59, %1
%11 = load i32, i32* %4
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %62
14: ; preds = %10
%15 = load i8*, i8** %2
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, 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 %32
22: ; preds = %14
%23 = load %struct.s_stack*, %struct.s_stack** %5
%24 = load i8*, i8** %2
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = call i32 @push(%struct.s_stack* %23, i8 signext %28)
%30 = load %struct.s_stack*, %struct.s_stack** %5
%31 = call i32 @displaystack(%struct.s_stack* %30)
br label %58
32: ; preds = %14
%33 = load i8*, i8** %2
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 41
br i1 %39, label %40, label %57
40: ; preds = %32
%41 = load %struct.s_stack*, %struct.s_stack** %5
%42 = call i32 @isEMPTY(%struct.s_stack* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %40
%45 = load %struct.s_stack*, %struct.s_stack** %5
%46 = call i32 @peek(%struct.s_stack* %45)
%47 = icmp eq i32 %46, 40
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load %struct.s_stack*, %struct.s_stack** %5
%50 = call i32 @pop(%struct.s_stack* %49)
br label %56
51: ; preds = %44, %40
%52 = load %struct.s_stack*, %struct.s_stack** %5
%53 = call i32 @peek(%struct.s_stack* %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %56
56: ; preds = %51, %48
br label %57
57: ; preds = %56, %32
br label %58
58: ; preds = %57, %22
br label %59
59: ; preds = %58
%60 = load i32, i32* %4
%61 = add nsw i32 %60, 1
store i32 %61, i32* %4
br label %10
62: ; preds = %10
%63 = load %struct.s_stack*, %struct.s_stack** %5
%64 = call i32 @peek(%struct.s_stack* %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %64)
%66 = load %struct.s_stack*, %struct.s_stack** %5
%67 = call i32 @isEMPTY(%struct.s_stack* %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %71, label %69
69: ; preds = %62
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %71
71: ; preds = %69, %62
ret void
}

LLVM IR (-O2):
%struct.s_stack = type { i32 }
@.str = [4 x i8] c"\0A%d\00"
@.str.1 = [24 x i8] c"\0AUnbalanced Parenthesis\00"
@.str.2 = [11 x i8] c"\0AUnbalaced\00"
define void @balancecheck(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call %struct.s_stack* @createstack(i32 %2)
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %29
5: ; preds = %1
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %26
%8 = phi i64 [ 0, %5 ], [ %27, %26 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
switch i8 %10, label %26 [
i8 40, label %11
i8 41, label %14
]
11: ; preds = %7
%12 = tail call i32 @push(%struct.s_stack* %3, i8 signext 40)
%13 = tail call i32 @displaystack(%struct.s_stack* %3)
br label %26
14: ; preds = %7
%15 = tail call i32 @isEMPTY(%struct.s_stack* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = tail call i32 @peek(%struct.s_stack* %3)
%19 = icmp eq i32 %18, 40
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @pop(%struct.s_stack* %3)
br label %26
22: ; preds = %17, %14
%23 = tail call i32 @peek(%struct.s_stack* %3)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %7, %11, %20, %22
%27 = add nuw nsw i64 %8, 1
%28 = icmp eq i64 %27, %6
br i1 %28, label %29, label %7
29: ; preds = %26, %1
%30 = tail call i32 @peek(%struct.s_stack* %3)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = tail call i32 @isEMPTY(%struct.s_stack* %3)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
ret void
}