; LLVM IR from DionysiosB/CodeForces :: 1200-1299 (1243C-TilePainting)

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

@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

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

9:                                                ; preds = %42, %0
  %10 = load i64, ptr %5, align 8, !tbaa !5
  %11 = load i64, ptr %5, align 8, !tbaa !5
  %12 = mul nsw i64 %10, %11
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = icmp sle i64 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %9
  store i32 2, ptr %6, align 4
  br label %45

16:                                               ; preds = %9
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = load i64, ptr %5, align 8, !tbaa !5
  %19 = srem i64 %17, %18
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %16
  br label %42

22:                                               ; preds = %16
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = icmp slt i64 %23, 0
  br i1 %24, label %25, label %27

25:                                               ; preds = %22
  %26 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %26, ptr %3, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %25, %22
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = srem i64 %28, %29
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %39, label %32

32:                                               ; preds = %27
  %33 = load i64, ptr %2, align 8, !tbaa !5
  %34 = load i64, ptr %5, align 8, !tbaa !5
  %35 = sdiv i64 %33, %34
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = srem i64 %35, %36
  %38 = icmp ne i64 %37, 0
  br i1 %38, label %39, label %41

39:                                               ; preds = %32, %27
  %40 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %40, ptr %4, align 8, !tbaa !5
  store i32 2, ptr %6, align 4
  br label %45

41:                                               ; preds = %32
  br label %42

42:                                               ; preds = %41, %21
  %43 = load i64, ptr %5, align 8, !tbaa !5
  %44 = add nsw i64 %43, 1
  store i64 %44, ptr %5, align 8, !tbaa !5
  br label %9, !llvm.loop !9

45:                                               ; preds = %39, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %46

46:                                               ; preds = %45
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %47 = load i64, ptr %2, align 8, !tbaa !5
  store i64 %47, ptr %7, align 8, !tbaa !5
  %48 = load i64, ptr %3, align 8, !tbaa !5
  %49 = icmp slt i64 %48, 0
  br i1 %49, label %50, label %52

50:                                               ; preds = %46
  %51 = load i64, ptr %2, align 8, !tbaa !5
  store i64 %51, ptr %7, align 8, !tbaa !5
  br label %59

52:                                               ; preds = %46
  %53 = load i64, ptr %4, align 8, !tbaa !5
  %54 = icmp slt i64 %53, 0
  br i1 %54, label %55, label %57

55:                                               ; preds = %52
  %56 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %56, ptr %7, align 8, !tbaa !5
  br label %58

57:                                               ; preds = %52
  store i64 1, ptr %7, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %57, %55
  br label %59

59:                                               ; preds = %58, %50
  %60 = load i64, ptr %7, align 8, !tbaa !5
  %61 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %60)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  %62 = load i32, ptr %1, align 4
  ret i32 %62
}

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

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

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

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

attributes #0 = { mustprogress norecurse 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 = { "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 #3 = { 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 = !{!"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"

@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #3
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %3 = load i64, ptr %1, align 8
  %.not23 = icmp slt i64 %3, 4
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %11
  %.01625 = phi i64 [ %12, %11 ], [ 2, %0 ]
  %.01824 = phi i64 [ %.2, %11 ], [ -1, %0 ]
  %4 = urem i64 %3, %.01625
  %5 = udiv i64 %3, %.01625
  %.not20 = icmp eq i64 %4, 0
  br i1 %.not20, label %6, label %11

6:                                                ; preds = %.lr.ph
  %7 = icmp slt i64 %.01824, 0
  %spec.select = select i1 %7, i64 %.01625, i64 %.01824
  %8 = urem i64 %.01625, %spec.select
  %.not21 = icmp eq i64 %8, 0
  br i1 %.not21, label %9, label %._crit_edge.loopexit

9:                                                ; preds = %6
  %10 = srem i64 %5, %spec.select
  %.not22 = icmp eq i64 %10, 0
  br i1 %.not22, label %11, label %._crit_edge.loopexit

11:                                               ; preds = %9, %.lr.ph
  %.2 = phi i64 [ %.01824, %.lr.ph ], [ %spec.select, %9 ]
  %12 = add nuw nsw i64 %.01625, 1
  %13 = mul nuw nsw i64 %12, %12
  %.not = icmp sgt i64 %13, %3
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !5

._crit_edge.loopexit:                             ; preds = %6, %9, %11
  %.3..ph = phi i64 [ %.2, %11 ], [ 1, %9 ], [ 1, %6 ]
  %.3.ph = phi i64 [ %.2, %11 ], [ %spec.select, %9 ], [ %spec.select, %6 ]
  %14 = icmp slt i64 %.3.ph, 0
  %15 = select i1 %14, i64 %3, i64 %.3..ph
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %.3 = phi i64 [ %3, %0 ], [ %15, %._crit_edge.loopexit ]
  %16 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %.3)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #3
  ret i32 0
}

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

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

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

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

attributes #0 = { mustprogress nofree norecurse 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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" }
attributes #3 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: DivRemPairsPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass