; LLVM IR from clunietp/libgmsd :: src (sample)

; 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: nounwind uwtable
define dso_local zeroext i1 @downsample_2x2(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = call i64 @kernel_size(ptr noundef %15)
  store i64 %16, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = call i64 @matrix_rows(ptr noundef %18)
  %20 = udiv i64 %19, 2
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = call i64 @matrix_cols(ptr noundef %21)
  %23 = udiv i64 %22, 2
  %24 = call zeroext i1 @matrix_reset(ptr noundef %17, i64 noundef %20, i64 noundef %23)
  br i1 %24, label %26, label %25

25:                                               ; preds = %3
  store i1 false, ptr %4, align 1
  store i32 1, ptr %14, align 4
  br label %65

26:                                               ; preds = %3
  store i64 0, ptr %9, align 8, !tbaa !9
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %58, %26
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = call i64 @matrix_rows(ptr noundef %29)
  %31 = icmp ult i64 %28, %30
  br i1 %31, label %32, label %64

32:                                               ; preds = %27
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = call ptr @matrix_crow(ptr noundef %33, i64 noundef %34)
  store ptr %35, ptr %12, align 8, !tbaa !5
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i64, ptr %11, align 8, !tbaa !9
  %38 = call ptr @matrix_row(ptr noundef %36, i64 noundef %37)
  store ptr %38, ptr %13, align 8, !tbaa !5
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %48, %32
  %40 = load i64, ptr %10, align 8, !tbaa !9
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = call i64 @matrix_cols(ptr noundef %41)
  %43 = icmp ult i64 %40, %42
  br i1 %43, label %44, label %57

44:                                               ; preds = %39
  %45 = load ptr, ptr %12, align 8, !tbaa !5
  %46 = load float, ptr %45, align 4, !tbaa !11
  %47 = load ptr, ptr %13, align 8, !tbaa !5
  store float %46, ptr %47, align 4, !tbaa !11
  br label %48

48:                                               ; preds = %44
  %49 = load i64, ptr %8, align 8, !tbaa !9
  %50 = load i64, ptr %10, align 8, !tbaa !9
  %51 = add i64 %50, %49
  store i64 %51, ptr %10, align 8, !tbaa !9
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = getelementptr inbounds float, ptr %52, i32 1
  store ptr %53, ptr %13, align 8, !tbaa !5
  %54 = load i64, ptr %8, align 8, !tbaa !9
  %55 = load ptr, ptr %12, align 8, !tbaa !5
  %56 = getelementptr inbounds float, ptr %55, i64 %54
  store ptr %56, ptr %12, align 8, !tbaa !5
  br label %39, !llvm.loop !13

57:                                               ; preds = %39
  br label %58

58:                                               ; preds = %57
  %59 = load i64, ptr %8, align 8, !tbaa !9
  %60 = load i64, ptr %9, align 8, !tbaa !9
  %61 = add i64 %60, %59
  store i64 %61, ptr %9, align 8, !tbaa !9
  %62 = load i64, ptr %11, align 8, !tbaa !9
  %63 = add i64 %62, 1
  store i64 %63, ptr %11, align 8, !tbaa !9
  br label %27, !llvm.loop !15

64:                                               ; preds = %27
  store i1 true, ptr %4, align 1
  store i32 1, ptr %14, align 4
  br label %65

65:                                               ; preds = %64, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %66 = load i1, ptr %4, align 1
  ret i1 %66
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @kernel_size(ptr noundef) #2

declare zeroext i1 @matrix_reset(ptr noundef, i64 noundef, i64 noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @matrix_rows(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @matrix_cols(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @matrix_crow(ptr noundef, i64 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @matrix_row(ptr noundef, i64 noundef) #2

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

attributes #0 = { 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 = { inlinehint 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 = { "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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: nounwind uwtable
define dso_local noundef zeroext i1 @downsample_2x2(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = tail call i64 @kernel_size(ptr noundef %2)
  %5 = tail call i64 @matrix_rows(ptr noundef %1)
  %6 = lshr i64 %5, 1
  %7 = tail call i64 @matrix_cols(ptr noundef %1)
  %8 = lshr i64 %7, 1
  %9 = tail call zeroext i1 @matrix_reset(ptr noundef %0, i64 noundef %6, i64 noundef %8) #3
  br i1 %9, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %3
  %10 = tail call i64 @matrix_rows(ptr noundef %1)
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %.loopexit, label %.lr.ph32

.lr.ph32:                                         ; preds = %.preheader, %._crit_edge
  %.02331 = phi i64 [ %21, %._crit_edge ], [ 0, %.preheader ]
  %.02530 = phi i64 [ %20, %._crit_edge ], [ 0, %.preheader ]
  %11 = tail call ptr @matrix_crow(ptr noundef %1, i64 noundef %.02530)
  %12 = tail call ptr @matrix_row(ptr noundef %0, i64 noundef %.02331)
  %13 = tail call i64 @matrix_cols(ptr noundef %1)
  %.not33 = icmp eq i64 %13, 0
  br i1 %.not33, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph32, %.lr.ph
  %.029 = phi ptr [ %16, %.lr.ph ], [ %12, %.lr.ph32 ]
  %.02228 = phi ptr [ %17, %.lr.ph ], [ %11, %.lr.ph32 ]
  %.02427 = phi i64 [ %15, %.lr.ph ], [ 0, %.lr.ph32 ]
  %14 = load float, ptr %.02228, align 4, !tbaa !5
  store float %14, ptr %.029, align 4, !tbaa !5
  %15 = add i64 %.02427, %4
  %16 = getelementptr inbounds i8, ptr %.029, i64 4
  %17 = getelementptr inbounds float, ptr %.02228, i64 %4
  %18 = tail call i64 @matrix_cols(ptr noundef %1)
  %19 = icmp ult i64 %15, %18
  br i1 %19, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph32
  %20 = add i64 %.02530, %4
  %21 = add i64 %.02331, 1
  %22 = tail call i64 @matrix_rows(ptr noundef %1)
  %23 = icmp ult i64 %20, %22
  br i1 %23, label %.lr.ph32, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %._crit_edge, %.preheader, %3
  ret i1 %9
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @kernel_size(ptr noundef) local_unnamed_addr #1

declare zeroext i1 @matrix_reset(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @matrix_rows(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @matrix_cols(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @matrix_crow(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @matrix_row(ptr noundef, i64 noundef) local_unnamed_addr #1

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


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass