; LLVM IR from HarshCasper/NeoAlgo :: C/other (remove_outermost_parentheses)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local ptr @remove_outer_parantheses(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %7, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  br label %8

8:                                                ; preds = %37, %1
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i32 1
  store ptr %10, ptr %2, align 8, !tbaa !5
  %11 = load i8, ptr %9, align 1, !tbaa !11
  store i8 %11, ptr %6, align 1, !tbaa !11
  %12 = icmp ne i8 %11, 0
  br i1 %12, label %13, label %38

13:                                               ; preds = %8
  %14 = load i8, ptr %6, align 1, !tbaa !11
  %15 = sext i8 %14 to i32
  %16 = icmp eq i32 %15, 41
  br i1 %16, label %17, label %20

17:                                               ; preds = %13
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = add nsw i32 %18, -1
  store i32 %19, ptr %4, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %17, %13
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = icmp sgt i32 %21, 0
  br i1 %22, label %23, label %30

23:                                               ; preds = %20
  %24 = load i8, ptr %6, align 1, !tbaa !11
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %3, align 4, !tbaa !9
  %28 = sext i32 %26 to i64
  %29 = getelementptr inbounds i8, ptr %25, i64 %28
  store i8 %24, ptr %29, align 1, !tbaa !11
  br label %30

30:                                               ; preds = %23, %20
  %31 = load i8, ptr %6, align 1, !tbaa !11
  %32 = sext i8 %31 to i32
  %33 = icmp eq i32 %32, 40
  br i1 %33, label %34, label %37

34:                                               ; preds = %30
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %4, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %34, %30
  br label %8, !llvm.loop !12

38:                                               ; preds = %8
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = load i32, ptr %3, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i8, ptr %39, i64 %41
  store i8 0, ptr %42, align 1, !tbaa !11
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret ptr %43
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @remove_outer_parantheses(ptr noundef returned %0) local_unnamed_addr #0 {
  br label %2

2:                                                ; preds = %13, %1
  %.013 = phi i32 [ 0, %1 ], [ %spec.select, %13 ]
  %.012 = phi i32 [ 0, %1 ], [ %.1, %13 ]
  %.0 = phi ptr [ %0, %1 ], [ %3, %13 ]
  %3 = getelementptr inbounds i8, ptr %.0, i64 1
  %4 = load i8, ptr %.0, align 1, !tbaa !5
  switch i8 %4, label %7 [
    i8 0, label %16
    i8 41, label %5
  ]

5:                                                ; preds = %2
  %6 = add nsw i32 %.013, -1
  br label %7

7:                                                ; preds = %2, %5
  %.114 = phi i32 [ %6, %5 ], [ %.013, %2 ]
  %8 = icmp sgt i32 %.114, 0
  br i1 %8, label %9, label %13

9:                                                ; preds = %7
  %10 = add nsw i32 %.012, 1
  %11 = sext i32 %.012 to i64
  %12 = getelementptr inbounds i8, ptr %0, i64 %11
  store i8 %4, ptr %12, align 1, !tbaa !5
  br label %13

13:                                               ; preds = %9, %7
  %.1 = phi i32 [ %10, %9 ], [ %.012, %7 ]
  %14 = icmp eq i8 %4, 40
  %15 = zext i1 %14 to i32
  %spec.select = add nsw i32 %.114, %15
  br label %2, !llvm.loop !8

16:                                               ; preds = %2
  %17 = sext i32 %.012 to i64
  %18 = getelementptr inbounds i8, ptr %0, i64 %17
  store i8 0, ptr %18, align 1, !tbaa !5
  ret ptr %0
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass