; LLVM IR from tangjz/acm-icpc :: codeforces/gym/103957H.ll (Z4scanPc)

; 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"

@_ZL6Figure = external hidden constant [14 x i8], align 1
@_ZL4Suit = external hidden constant [5 x i8], align 1

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

7:                                                ; preds = %57, %1
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [14 x i8], ptr @_ZL6Figure, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !11
  %12 = icmp ne i8 %11, 0
  br i1 %12, label %14, label %13

13:                                               ; preds = %7
  store i32 2, ptr %5, align 4
  br label %60

14:                                               ; preds = %7
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 0
  %17 = load i8, ptr %16, align 1, !tbaa !11
  %18 = sext i8 %17 to i32
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [14 x i8], ptr @_ZL6Figure, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = sext i8 %22 to i32
  %24 = icmp eq i32 %18, %23
  br i1 %24, label %25, label %56

25:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %50, %25
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [5 x i8], ptr @_ZL4Suit, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = icmp ne i8 %30, 0
  br i1 %31, label %33, label %32

32:                                               ; preds = %26
  store i32 5, ptr %5, align 4
  br label %53

33:                                               ; preds = %26
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i64 1
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = sext i8 %36 to i32
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [5 x i8], ptr @_ZL4Suit, i64 0, i64 %39
  %41 = load i8, ptr %40, align 1, !tbaa !11
  %42 = sext i8 %41 to i32
  %43 = icmp eq i32 %37, %42
  br i1 %43, label %44, label %49

44:                                               ; preds = %33
  %45 = load i32, ptr %4, align 4, !tbaa !9
  %46 = shl i32 %45, 2
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = or i32 %46, %47
  store i32 %48, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %53

49:                                               ; preds = %33
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %6, align 4, !tbaa !9
  br label %26, !llvm.loop !12

53:                                               ; preds = %44, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %54 = load i32, ptr %5, align 4
  switch i32 %54, label %60 [
    i32 5, label %55
  ]

55:                                               ; preds = %53
  br label %56

56:                                               ; preds = %55, %14
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %4, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !14

60:                                               ; preds = %53, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %61 = load i32, ptr %5, align 4
  switch i32 %61, label %65 [
    i32 2, label %62
    i32 1, label %63
  ]

62:                                               ; preds = %60
  store i32 -1, ptr %2, align 4
  br label %63

63:                                               ; preds = %62, %60
  %64 = load i32, ptr %2, align 4
  ret i32 %64

65:                                               ; preds = %60
  unreachable
}

; 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 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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"

@_ZL6Figure = external hidden local_unnamed_addr constant [14 x i8], align 1
@_ZL4Suit = external hidden local_unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i32 @_Z4scanPc(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load i8, ptr @_ZL6Figure, align 1, !tbaa !5
  %.not25 = icmp eq i8 %2, 0
  br i1 %.not25, label %.loopexit, label %.lr.ph28

.lr.ph28:                                         ; preds = %1
  %3 = load i8, ptr %0, align 1, !tbaa !5
  %4 = load i8, ptr @_ZL4Suit, align 1
  %.fr = freeze i8 %4
  %.not1823 = icmp eq i8 %.fr, 0
  %5 = getelementptr inbounds i8, ptr %0, i64 1
  br i1 %.not1823, label %.loopexit, label %.lr.ph28.split

.lr.ph28.split:                                   ; preds = %.lr.ph28, %.thread
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %.thread ], [ 0, %.lr.ph28 ]
  %6 = phi i8 [ %21, %.thread ], [ %2, %.lr.ph28 ]
  %7 = icmp eq i8 %3, %6
  br i1 %7, label %.preheader, label %.thread

.preheader:                                       ; preds = %.lr.ph28.split
  %8 = load i8, ptr %5, align 1, !tbaa !5
  br label %9

9:                                                ; preds = %.preheader, %12
  %indvars.iv = phi i64 [ 0, %.preheader ], [ %indvars.iv.next, %12 ]
  %10 = phi i8 [ %.fr, %.preheader ], [ %14, %12 ]
  %11 = icmp eq i8 %8, %10
  br i1 %11, label %15, label %12

12:                                               ; preds = %9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %13 = getelementptr inbounds [5 x i8], ptr @_ZL4Suit, i64 0, i64 %indvars.iv.next
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %.not18 = icmp eq i8 %14, 0
  br i1 %.not18, label %.thread, label %9, !llvm.loop !8

15:                                               ; preds = %9
  %16 = trunc nuw nsw i64 %indvars.iv32 to i32
  %17 = trunc nuw nsw i64 %indvars.iv to i32
  %18 = shl i32 %16, 2
  %19 = or i32 %18, %17
  br label %.loopexit

.thread:                                          ; preds = %12, %.lr.ph28.split
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %20 = getelementptr inbounds [14 x i8], ptr @_ZL6Figure, i64 0, i64 %indvars.iv.next33
  %21 = load i8, ptr %20, align 1, !tbaa !5
  %.not = icmp eq i8 %21, 0
  br i1 %.not, label %.loopexit, label %.lr.ph28.split, !llvm.loop !10

.loopexit:                                        ; preds = %.thread, %.lr.ph28, %1, %15
  %spec.select = phi i32 [ %19, %15 ], [ -1, %1 ], [ -1, %.lr.ph28 ], [ -1, %.thread ]
  ret i32 %spec.select
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass