; LLVM IR from DionysiosB/CodeForces :: 0300-0399 (304A-PythagoreanTheroem2)

; 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 [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 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 i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  store i64 0, ptr %2, align 8, !tbaa !5
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = load i64, ptr %2, align 8, !tbaa !5
  %13 = mul nsw i64 %11, %12
  store i64 %13, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store i64 1, ptr %5, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %61, %0
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %64

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %20 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %20, ptr %7, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %56, %19
  %22 = load i64, ptr %7, align 8, !tbaa !5
  %23 = load i64, ptr %2, align 8, !tbaa !5
  %24 = icmp slt i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 5, ptr %6, align 4
  br label %59

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = mul nsw i64 %27, %28
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = load i64, ptr %7, align 8, !tbaa !5
  %32 = mul nsw i64 %30, %31
  %33 = add nsw i64 %29, %32
  store i64 %33, ptr %8, align 8, !tbaa !5
  %34 = load i64, ptr %8, align 8, !tbaa !5
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = icmp sgt i64 %34, %35
  br i1 %36, label %37, label %38

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

38:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %39 = load i64, ptr %8, align 8, !tbaa !5
  %40 = sitofp i64 %39 to double
  %41 = call double @sqrt(double noundef %40) #4
  %42 = fptosi double %41 to i32
  store i32 %42, ptr %9, align 4, !tbaa !9
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = mul nsw i32 %43, %44
  %46 = sext i32 %45 to i64
  %47 = load i64, ptr %8, align 8, !tbaa !5
  %48 = icmp eq i64 %46, %47
  br i1 %48, label %49, label %52

49:                                               ; preds = %38
  %50 = load i32, ptr %4, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %4, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %49, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  store i32 0, ptr %6, align 4
  br label %53

53:                                               ; preds = %52, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %54 = load i32, ptr %6, align 4
  switch i32 %54, label %59 [
    i32 0, label %55
  ]

55:                                               ; preds = %53
  br label %56

56:                                               ; preds = %55
  %57 = load i64, ptr %7, align 8, !tbaa !5
  %58 = add nsw i64 %57, 1
  store i64 %58, ptr %7, align 8, !tbaa !5
  br label %21, !llvm.loop !11

59:                                               ; preds = %53, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %60

60:                                               ; preds = %59
  br label %61

61:                                               ; preds = %60
  %62 = load i64, ptr %5, align 8, !tbaa !5
  %63 = add nsw i64 %62, 1
  store i64 %63, ptr %5, align 8, !tbaa !5
  br label %14, !llvm.loop !13

64:                                               ; preds = %18
  %65 = load i32, ptr %4, align 4, !tbaa !9
  %66 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %65)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  %67 = load i32, ptr %1, align 4
  ret i32 %67
}

; 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: nounwind
declare double @sqrt(double noundef) #3

; 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 "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 #4 = { 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", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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 [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 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) #4
  store i64 0, ptr %1, align 8, !tbaa !5
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %3 = load i64, ptr %1, align 8, !tbaa !5
  %4 = mul nsw i64 %3, %3
  %5 = icmp sgt i64 %3, 1
  br i1 %5, label %.preheader, label %._crit_edge

.preheader:                                       ; preds = %0, %.thread
  %.029 = phi i32 [ %.1.lcssa, %.thread ], [ 0, %0 ]
  %.02128 = phi i64 [ %20, %.thread ], [ 1, %0 ]
  %6 = mul nuw nsw i64 %.02128, %.02128
  %7 = icmp slt i64 %.02128, %3
  br i1 %7, label %.lr.ph, label %.thread

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

.lr.ph:                                           ; preds = %.preheader, %11
  %.125 = phi i32 [ %spec.select, %11 ], [ %.029, %.preheader ]
  %.01924 = phi i64 [ %18, %11 ], [ %.02128, %.preheader ]
  %9 = mul nuw nsw i64 %.01924, %.01924
  %10 = add nuw nsw i64 %9, %6
  %.not = icmp ugt i64 %10, %4
  br i1 %.not, label %.thread, label %11

11:                                               ; preds = %.lr.ph
  %12 = uitofp nneg i64 %10 to double
  %sqrt = call double @llvm.sqrt.f64(double %12)
  %13 = fptosi double %sqrt to i32
  %14 = mul nsw i32 %13, %13
  %15 = zext nneg i32 %14 to i64
  %16 = icmp eq i64 %10, %15
  %17 = zext i1 %16 to i32
  %spec.select = add nsw i32 %.125, %17
  %18 = add nuw nsw i64 %.01924, 1
  %19 = icmp slt i64 %18, %3
  br i1 %19, label %.lr.ph, label %.thread, !llvm.loop !9

.thread:                                          ; preds = %11, %.lr.ph, %.preheader
  %.1.lcssa = phi i32 [ %.029, %.preheader ], [ %.125, %.lr.ph ], [ %spec.select, %11 ]
  %20 = add nuw nsw i64 %.02128, 1
  %exitcond.not = icmp eq i64 %20, %3
  br i1 %exitcond.not, label %._crit_edge, label %.preheader, !llvm.loop !11
}

; 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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.sqrt.f64(double) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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