; LLVM IR from ryanlayer/giggle :: lib/htslib (bcf_sr_sort)

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

%struct.sr_sort_t = type { [256 x i8], i32, i32, ptr, i32, i32, i32, ptr, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.__kstring_t, i32, i32, ptr, i32, ptr, ptr, i32, i32, i32, i32 }
%struct.__kstring_t = type { i64, i64, ptr }
%struct.vcf_buf_t = type { i32, i32, ptr }

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @bcf_sr_sort_remove_reader(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %7 = load ptr, ptr %5, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.sr_sort_t, ptr %7, i32 0, i32 14
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds %struct.vcf_buf_t, ptr %9, i64 %11
  %13 = getelementptr inbounds %struct.vcf_buf_t, ptr %12, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  call void @free(ptr noundef %14) #4
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = add nsw i32 %15, 1
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.sr_sort_t, ptr %17, i32 0, i32 26
  %19 = load i32, ptr %18, align 4, !tbaa !17
  %20 = icmp slt i32 %16, %19
  br i1 %20, label %21, label %43

21:                                               ; preds = %3
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.sr_sort_t, ptr %22, i32 0, i32 14
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds %struct.vcf_buf_t, ptr %24, i64 %26
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.sr_sort_t, ptr %28, i32 0, i32 14
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.vcf_buf_t, ptr %30, i64 %33
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.sr_sort_t, ptr %35, i32 0, i32 26
  %37 = load i32, ptr %36, align 4, !tbaa !17
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = sub nsw i32 %37, %38
  %40 = sub nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = mul i64 %41, 16
  call void @llvm.memmove.p0.p0.i64(ptr align 8 %27, ptr align 8 %34, i64 %42, i1 false)
  br label %43

43:                                               ; preds = %21, %3
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.sr_sort_t, ptr %44, i32 0, i32 14
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.sr_sort_t, ptr %47, i32 0, i32 26
  %49 = load i32, ptr %48, align 4, !tbaa !17
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds %struct.vcf_buf_t, ptr %46, i64 %50
  %52 = getelementptr inbounds %struct.vcf_buf_t, ptr %51, i64 -1
  call void @llvm.memset.p0.i64(ptr align 8 %52, i8 0, i64 16, i1 false)
  ret void
}

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 "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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 336}
!12 = !{!"", !7, i64 0, !10, i64 256, !10, i64 260, !6, i64 264, !10, i64 272, !10, i64 276, !10, i64 280, !6, i64 288, !10, i64 296, !10, i64 300, !10, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !13, i64 368, !10, i64 392, !10, i64 396, !6, i64 400, !10, i64 408, !6, i64 416, !6, i64 424, !10, i64 432, !10, i64 436, !10, i64 440, !10, i64 444}
!13 = !{!"__kstring_t", !14, i64 0, !14, i64 8, !6, i64 16}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !6, i64 8}
!16 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!17 = !{!12, !10, i64 436}


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

%struct.vcf_buf_t = type { i32, i32, ptr }

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: mustprogress nounwind willreturn uwtable
define dso_local void @bcf_sr_sort_remove_reader(ptr nocapture noundef readnone %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %1, i64 336
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = sext i32 %2 to i64
  %7 = getelementptr inbounds %struct.vcf_buf_t, ptr %5, i64 %6, i32 2
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  tail call void @free(ptr noundef %8) #4
  %9 = add nsw i32 %2, 1
  %10 = getelementptr inbounds i8, ptr %1, i64 436
  %11 = load i32, ptr %10, align 4, !tbaa !15
  %12 = icmp slt i32 %9, %11
  br i1 %12, label %13, label %22

13:                                               ; preds = %3
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.vcf_buf_t, ptr %14, i64 %6
  %16 = sext i32 %9 to i64
  %17 = getelementptr inbounds %struct.vcf_buf_t, ptr %14, i64 %16
  %18 = xor i32 %2, -1
  %19 = add i32 %11, %18
  %20 = sext i32 %19 to i64
  %21 = shl nsw i64 %20, 4
  tail call void @llvm.memmove.p0.p0.i64(ptr align 8 %15, ptr align 8 %17, i64 %21, i1 false)
  %.pre = load i32, ptr %10, align 4, !tbaa !15
  br label %22

22:                                               ; preds = %13, %3
  %23 = phi i32 [ %.pre, %13 ], [ %11, %3 ]
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = sext i32 %23 to i64
  %26 = getelementptr inbounds %struct.vcf_buf_t, ptr %24, i64 %25
  %27 = getelementptr inbounds i8, ptr %26, i64 -16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %27, i8 0, i64 16, i1 false)
  ret void
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { mustprogress nounwind willreturn 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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !10, i64 336}
!6 = !{!"", !7, i64 0, !9, i64 256, !9, i64 260, !10, i64 264, !9, i64 272, !9, i64 276, !9, i64 280, !10, i64 288, !9, i64 296, !9, i64 300, !9, i64 304, !10, i64 312, !10, i64 320, !10, i64 328, !10, i64 336, !10, i64 344, !10, i64 352, !10, i64 360, !11, i64 368, !9, i64 392, !9, i64 396, !10, i64 400, !9, i64 408, !10, i64 416, !10, i64 424, !9, i64 432, !9, i64 436, !9, i64 440, !9, i64 444}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"__kstring_t", !12, i64 0, !12, i64 8, !10, i64 16}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"", !9, i64 0, !9, i64 4, !10, i64 8}
!15 = !{!6, !9, i64 436}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass