; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Google APAC/2015 APAC Test Round A/Problem D Cut Tiles/Cut Tiles.ll (Z8findAreaPiiiS_)

; 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: mustprogress uwtable
define dso_local void @_Z8findAreaPiiiS_(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %20

16:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %17 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %17, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %18, ptr %7, align 4, !tbaa !9
  %19 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %19, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %20

20:                                               ; preds = %16, %4
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %26, label %23

23:                                               ; preds = %20
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %23, %20
  br label %78

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = call noundef double @_ZSt3logIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %28)
  %30 = call noundef double @_ZSt3logIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef 2)
  %31 = fdiv double %29, %30
  %32 = fptosi double %31 to i32
  store i32 %32, ptr %10, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %45, %27
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = icmp sge i32 %34, 0
  br i1 %35, label %36, label %43

36:                                               ; preds = %33
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %37, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = icmp eq i32 %41, 0
  br label %43

43:                                               ; preds = %36, %33
  %44 = phi i1 [ false, %33 ], [ %42, %36 ]
  br i1 %44, label %45, label %48

45:                                               ; preds = %43
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %10, align 4, !tbaa !9
  br label %33, !llvm.loop !11

48:                                               ; preds = %43
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = icmp eq i32 %49, -1
  br i1 %50, label %51, label %52

51:                                               ; preds = %48
  store i32 1, ptr %11, align 4
  br label %76

52:                                               ; preds = %48
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i32, ptr %53, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !9
  %58 = add nsw i32 %57, -1
  store i32 %58, ptr %56, align 4, !tbaa !9
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = load i32, ptr %59, align 4, !tbaa !9
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %59, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %62 = load i32, ptr %10, align 4, !tbaa !9
  %63 = shl i32 1, %62
  store i32 %63, ptr %12, align 4, !tbaa !9
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = load i32, ptr %12, align 4, !tbaa !9
  %67 = sub nsw i32 %65, %66
  %68 = load i32, ptr %12, align 4, !tbaa !9
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  call void @_Z8findAreaPiiiS_(ptr noundef %64, i32 noundef %67, i32 noundef %68, ptr noundef %69)
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = load i32, ptr %6, align 4, !tbaa !9
  %72 = load i32, ptr %12, align 4, !tbaa !9
  %73 = sub nsw i32 %71, %72
  %74 = load i32, ptr %7, align 4, !tbaa !9
  %75 = load ptr, ptr %8, align 8, !tbaa !5
  call void @_Z8findAreaPiiiS_(ptr noundef %70, i32 noundef %73, i32 noundef %74, ptr noundef %75)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  store i32 0, ptr %11, align 4
  br label %76

76:                                               ; preds = %52, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %77 = load i32, ptr %11, align 4
  switch i32 %77, label %79 [
    i32 0, label %78
    i32 1, label %78
  ]

78:                                               ; preds = %76, %76, %26
  ret void

79:                                               ; preds = %76
  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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3logIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"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: mustprogress nounwind uwtable
define dso_local void @_Z8findAreaPiiiS_(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  %spec.select39 = tail call i32 @llvm.smin.i32(i32 %1, i32 %2)
  %spec.select3240 = tail call i32 @llvm.smax.i32(i32 %1, i32 %2)
  %5 = icmp eq i32 %spec.select3240, 0
  %6 = icmp eq i32 %spec.select39, 0
  %or.cond41 = or i1 %5, %6
  br i1 %or.cond41, label %.critedge._crit_edge, label %.lr.ph44

.lr.ph44:                                         ; preds = %4, %.critedge.thread
  %spec.select3243 = phi i32 [ %spec.select32, %.critedge.thread ], [ %spec.select3240, %4 ]
  %spec.select42 = phi i32 [ %spec.select, %.critedge.thread ], [ %spec.select39, %4 ]
  %7 = tail call noundef double @_ZSt3logIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %spec.select42)
  %8 = tail call noundef double @_ZSt3logIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef 2)
  %9 = fdiv double %7, %8
  %10 = fptosi double %9 to i32
  %11 = icmp sgt i32 %10, -1
  br i1 %11, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %.lr.ph44, %16
  %.02738 = phi i32 [ %17, %16 ], [ %10, %.lr.ph44 ]
  %12 = zext nneg i32 %.02738 to i64
  %13 = getelementptr inbounds i32, ptr %0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %.critedge.thread

16:                                               ; preds = %.lr.ph
  %17 = add nsw i32 %.02738, -1
  %18 = icmp sgt i32 %.02738, 0
  br i1 %18, label %.lr.ph, label %.critedge._crit_edge, !llvm.loop !9

.critedge:                                        ; preds = %.lr.ph44
  %19 = icmp eq i32 %10, -1
  br i1 %19, label %.critedge._crit_edge, label %.critedge.thread

.critedge.thread:                                 ; preds = %.lr.ph, %.critedge
  %.02737 = phi i32 [ %10, %.critedge ], [ %.02738, %.lr.ph ]
  %20 = sext i32 %.02737 to i64
  %21 = getelementptr inbounds i32, ptr %0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %22, -1
  store i32 %23, ptr %21, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %3, align 4, !tbaa !5
  %26 = shl nuw i32 1, %.02737
  %27 = sub nsw i32 %spec.select42, %26
  tail call void @_Z8findAreaPiiiS_(ptr noundef %0, i32 noundef %27, i32 noundef %26, ptr noundef nonnull %3)
  %28 = sub nsw i32 %spec.select3243, %26
  %spec.select = tail call i32 @llvm.smin.i32(i32 %28, i32 %spec.select42)
  %spec.select32 = tail call i32 @llvm.smax.i32(i32 %28, i32 %spec.select42)
  %29 = icmp eq i32 %spec.select32, 0
  %30 = icmp eq i32 %spec.select, 0
  %or.cond = or i1 %29, %30
  br i1 %or.cond, label %.critedge._crit_edge, label %.lr.ph44

.critedge._crit_edge:                             ; preds = %.critedge.thread, %.critedge, %16, %4
  ret void
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3logIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

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 = { inlinehint 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"int", !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,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass