; LLVM IR from ravireddy07/cpp-book :: src/Code-Forces/#693_cards-for-friends.ll (Z5harryv)

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

@cards = external dso_local global i64, align 8
@n = external dso_local global i64, align 8
@w = external dso_local global i64, align 8
@h = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5harryv() #0 {
  %1 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @w, ptr noundef @h)
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @n)
  %3 = load i64, ptr @w, align 8, !tbaa !5
  %4 = srem i64 %3, 2
  %5 = icmp eq i64 %4, 1
  br i1 %5, label %6, label %15

6:                                                ; preds = %0
  %7 = load i64, ptr @h, align 8, !tbaa !5
  %8 = srem i64 %7, 2
  %9 = icmp eq i64 %8, 1
  br i1 %9, label %10, label %15

10:                                               ; preds = %6
  %11 = load i64, ptr @n, align 8, !tbaa !5
  %12 = icmp sgt i64 %11, 1
  br i1 %12, label %13, label %15

13:                                               ; preds = %10
  %14 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %83

15:                                               ; preds = %10, %6, %0
  store i64 1, ptr @cards, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %72, %15
  %17 = load i64, ptr @w, align 8, !tbaa !5
  %18 = srem i64 %17, 2
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %24, label %20

20:                                               ; preds = %16
  %21 = load i64, ptr @h, align 8, !tbaa !5
  %22 = srem i64 %21, 2
  %23 = icmp eq i64 %22, 0
  br label %24

24:                                               ; preds = %20, %16
  %25 = phi i1 [ true, %16 ], [ %23, %20 ]
  br i1 %25, label %26, label %73

26:                                               ; preds = %24
  %27 = load i64, ptr @cards, align 8, !tbaa !5
  %28 = load i64, ptr @n, align 8, !tbaa !5
  %29 = icmp sge i64 %27, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  br label %73

31:                                               ; preds = %26
  %32 = load i64, ptr @w, align 8, !tbaa !5
  %33 = srem i64 %32, 2
  %34 = icmp eq i64 %33, 0
  br i1 %34, label %35, label %52

35:                                               ; preds = %31
  %36 = load i64, ptr @h, align 8, !tbaa !5
  %37 = srem i64 %36, 2
  %38 = icmp eq i64 %37, 0
  br i1 %38, label %39, label %52

39:                                               ; preds = %35
  %40 = load i64, ptr @w, align 8, !tbaa !5
  %41 = load i64, ptr @h, align 8, !tbaa !5
  %42 = icmp sge i64 %40, %41
  br i1 %42, label %43, label %46

43:                                               ; preds = %39
  %44 = load i64, ptr @w, align 8, !tbaa !5
  %45 = sdiv i64 %44, 2
  store i64 %45, ptr @w, align 8, !tbaa !5
  br label %49

46:                                               ; preds = %39
  %47 = load i64, ptr @h, align 8, !tbaa !5
  %48 = sdiv i64 %47, 2
  store i64 %48, ptr @h, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %46, %43
  %50 = load i64, ptr @cards, align 8, !tbaa !5
  %51 = mul nsw i64 %50, 2
  store i64 %51, ptr @cards, align 8, !tbaa !5
  br label %72

52:                                               ; preds = %35, %31
  %53 = load i64, ptr @w, align 8, !tbaa !5
  %54 = srem i64 %53, 2
  %55 = icmp eq i64 %54, 0
  br i1 %55, label %56, label %61

56:                                               ; preds = %52
  %57 = load i64, ptr @w, align 8, !tbaa !5
  %58 = sdiv i64 %57, 2
  store i64 %58, ptr @w, align 8, !tbaa !5
  %59 = load i64, ptr @cards, align 8, !tbaa !5
  %60 = mul nsw i64 %59, 2
  store i64 %60, ptr @cards, align 8, !tbaa !5
  br label %71

61:                                               ; preds = %52
  %62 = load i64, ptr @h, align 8, !tbaa !5
  %63 = srem i64 %62, 2
  %64 = icmp eq i64 %63, 0
  br i1 %64, label %65, label %70

65:                                               ; preds = %61
  %66 = load i64, ptr @h, align 8, !tbaa !5
  %67 = sdiv i64 %66, 2
  store i64 %67, ptr @h, align 8, !tbaa !5
  %68 = load i64, ptr @cards, align 8, !tbaa !5
  %69 = mul nsw i64 %68, 2
  store i64 %69, ptr @cards, align 8, !tbaa !5
  br label %70

70:                                               ; preds = %65, %61
  br label %71

71:                                               ; preds = %70, %56
  br label %72

72:                                               ; preds = %71, %49
  br label %16, !llvm.loop !9

73:                                               ; preds = %30, %24
  %74 = load i64, ptr @cards, align 8, !tbaa !5
  %75 = load i64, ptr @n, align 8, !tbaa !5
  %76 = icmp sge i64 %74, %75
  br i1 %76, label %77, label %79

77:                                               ; preds = %73
  %78 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  br label %81

79:                                               ; preds = %73
  %80 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %81

81:                                               ; preds = %79, %77
  %82 = phi i32 [ %78, %77 ], [ %80, %79 ]
  br label %83

83:                                               ; preds = %81, %13
  ret void
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

declare i32 @printf(ptr noundef, ...) #1

attributes #0 = { mustprogress 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 = { "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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@cards = external dso_local local_unnamed_addr global i64, align 8
@n = external dso_local global i64, align 8
@w = external dso_local global i64, align 8
@h = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z5harryv() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @w, ptr noundef nonnull @h)
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @n)
  %3 = load i64, ptr @w, align 8, !tbaa !5
  %4 = and i64 %3, -9223372036854775807
  %5 = icmp eq i64 %4, 1
  %6 = load i64, ptr @h, align 8, !tbaa !5
  br i1 %5, label %7, label %._crit_edge

._crit_edge:                                      ; preds = %0
  %.pre = load i64, ptr @n, align 8
  br label %12

7:                                                ; preds = %0
  %8 = and i64 %6, -9223372036854775807
  %9 = icmp eq i64 %8, 1
  %10 = load i64, ptr @n, align 8
  %11 = icmp sgt i64 %10, 1
  %or.cond = select i1 %9, i1 %11, i1 false
  br i1 %or.cond, label %39, label %12

12:                                               ; preds = %._crit_edge, %7
  %13 = phi i64 [ %.pre, %._crit_edge ], [ %10, %7 ]
  br label %14

14:                                               ; preds = %34, %12
  %.sink = phi i64 [ %37, %34 ], [ 1, %12 ]
  %15 = phi i64 [ %35, %34 ], [ %6, %12 ]
  %16 = phi i64 [ %36, %34 ], [ %3, %12 ]
  %17 = and i64 %16, 1
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %.critedge, label %19

19:                                               ; preds = %14
  %20 = and i64 %15, 1
  %21 = icmp eq i64 %20, 0
  %.not5 = icmp slt i64 %.sink, %13
  %or.cond7 = select i1 %21, i1 %.not5, i1 false
  br i1 %or.cond7, label %32, label %38

.critedge:                                        ; preds = %14
  %.not = icmp slt i64 %.sink, %13
  br i1 %.not, label %22, label %38

22:                                               ; preds = %.critedge
  %23 = and i64 %15, 1
  %24 = icmp eq i64 %23, 0
  br i1 %24, label %25, label %30

25:                                               ; preds = %22
  %.not3 = icmp slt i64 %16, %15
  br i1 %.not3, label %28, label %26

26:                                               ; preds = %25
  %27 = ashr exact i64 %16, 1
  store i64 %27, ptr @w, align 8, !tbaa !5
  br label %34

28:                                               ; preds = %25
  %29 = ashr exact i64 %15, 1
  store i64 %29, ptr @h, align 8, !tbaa !5
  br label %34

30:                                               ; preds = %22
  %31 = ashr exact i64 %16, 1
  store i64 %31, ptr @w, align 8, !tbaa !5
  br label %34

32:                                               ; preds = %19
  %33 = ashr exact i64 %15, 1
  store i64 %33, ptr @h, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %26, %28, %30, %32
  %35 = phi i64 [ %15, %30 ], [ %33, %32 ], [ %29, %28 ], [ %15, %26 ]
  %36 = phi i64 [ %31, %30 ], [ %16, %32 ], [ %16, %28 ], [ %27, %26 ]
  %37 = shl nsw i64 %.sink, 1
  br label %14, !llvm.loop !9

38:                                               ; preds = %.critedge, %19
  store i64 %.sink, ptr @cards, align 8, !tbaa !5
  %.not4 = icmp slt i64 %.sink, %13
  %.str.2..str.3 = select i1 %.not4, ptr @.str.2, ptr @.str.3
  br label %39

39:                                               ; preds = %38, %7
  %.str.3.sink = phi ptr [ @.str.2, %7 ], [ %.str.2..str.3, %38 ]
  %40 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.3.sink)
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nofree 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 = { nofree nounwind "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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SimplifyCFGPass,TailCallElimPass