; LLVM IR from attractivechaos/klib :: bgzf (bgzf_write)

; 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.BGZF = type { i32, i32, i32, i32, i64, ptr, ptr, ptr, ptr }

@.str.3 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [21 x i8], align 1
@__PRETTY_FUNCTION__.bgzf_write = external hidden unnamed_addr constant [50 x i8], align 1

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

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

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @bgzf_flush(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i64 @bgzf_write(ptr noundef %0, ptr noundef %1, i64 noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %13, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 65536, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = load i32, ptr %14, align 8
  %16 = shl i32 %15, 24
  %17 = ashr i32 %16, 24
  %18 = icmp eq i32 %17, 119
  br i1 %18, label %19, label %20

19:                                               ; preds = %3
  br label %21

20:                                               ; preds = %3
  call void @__assert_fail(ptr noundef @.str.4, ptr noundef @.str.3, i32 noundef 418, ptr noundef @__PRETTY_FUNCTION__.bgzf_write) #5
  unreachable

21:                                               ; preds = %19
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %22, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %9, align 4, !tbaa !11
  br label %23

23:                                               ; preds = %92, %21
  %24 = load i32, ptr %9, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = icmp slt i64 %25, %26
  br i1 %27, label %28, label %93

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.BGZF, ptr %29, i32 0, i32 5
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  store ptr %31, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %32 = load i32, ptr %8, align 4, !tbaa !11
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.BGZF, ptr %33, i32 0, i32 3
  %35 = load i32, ptr %34, align 4, !tbaa !15
  %36 = sub nsw i32 %32, %35
  %37 = sext i32 %36 to i64
  %38 = load i64, ptr %6, align 8, !tbaa !9
  %39 = load i32, ptr %9, align 4, !tbaa !11
  %40 = sext i32 %39 to i64
  %41 = sub nsw i64 %38, %40
  %42 = icmp slt i64 %37, %41
  br i1 %42, label %43, label %50

43:                                               ; preds = %28
  %44 = load i32, ptr %8, align 4, !tbaa !11
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.BGZF, ptr %45, i32 0, i32 3
  %47 = load i32, ptr %46, align 4, !tbaa !15
  %48 = sub nsw i32 %44, %47
  %49 = sext i32 %48 to i64
  br label %55

50:                                               ; preds = %28
  %51 = load i64, ptr %6, align 8, !tbaa !9
  %52 = load i32, ptr %9, align 4, !tbaa !11
  %53 = sext i32 %52 to i64
  %54 = sub nsw i64 %51, %53
  br label %55

55:                                               ; preds = %50, %43
  %56 = phi i64 [ %49, %43 ], [ %54, %50 ]
  %57 = trunc i64 %56 to i32
  store i32 %57, ptr %11, align 4, !tbaa !11
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.BGZF, ptr %59, i32 0, i32 3
  %61 = load i32, ptr %60, align 4, !tbaa !15
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i8, ptr %58, i64 %62
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = load i32, ptr %11, align 4, !tbaa !11
  %66 = sext i32 %65 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %63, ptr align 1 %64, i64 %66, i1 false)
  %67 = load i32, ptr %11, align 4, !tbaa !11
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.BGZF, ptr %68, i32 0, i32 3
  %70 = load i32, ptr %69, align 4, !tbaa !15
  %71 = add nsw i32 %70, %67
  store i32 %71, ptr %69, align 4, !tbaa !15
  %72 = load i32, ptr %11, align 4, !tbaa !11
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = sext i32 %72 to i64
  %75 = getelementptr inbounds i8, ptr %73, i64 %74
  store ptr %75, ptr %7, align 8, !tbaa !5
  %76 = load i32, ptr %11, align 4, !tbaa !11
  %77 = load i32, ptr %9, align 4, !tbaa !11
  %78 = add nsw i32 %77, %76
  store i32 %78, ptr %9, align 4, !tbaa !11
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.BGZF, ptr %79, i32 0, i32 3
  %81 = load i32, ptr %80, align 4, !tbaa !15
  %82 = load i32, ptr %8, align 4, !tbaa !11
  %83 = icmp eq i32 %81, %82
  br i1 %83, label %84, label %89

84:                                               ; preds = %55
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = call i32 @bgzf_flush(ptr noundef %85)
  %87 = icmp ne i32 %86, 0
  br i1 %87, label %88, label %89

88:                                               ; preds = %84
  store i32 3, ptr %12, align 4
  br label %90

89:                                               ; preds = %84, %55
  store i32 0, ptr %12, align 4
  br label %90

90:                                               ; preds = %89, %88
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %91 = load i32, ptr %12, align 4
  switch i32 %91, label %96 [
    i32 0, label %92
    i32 3, label %93
  ]

92:                                               ; preds = %90
  br label %23, !llvm.loop !16

93:                                               ; preds = %90, %23
  %94 = load i32, ptr %9, align 4, !tbaa !11
  %95 = sext i32 %94 to i64
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret i64 %95

96:                                               ; preds = %90
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { noreturn 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 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 #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 24}
!14 = !{!"", !12, i64 0, !12, i64 1, !12, i64 2, !12, i64 4, !12, i64 8, !12, i64 12, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!15 = !{!14, !12, i64 12}
!16 = distinct !{!16, !17}
!17 = !{!"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.3 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [21 x i8], align 1
@__PRETTY_FUNCTION__.bgzf_write = external hidden unnamed_addr constant [50 x i8], align 1

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @bgzf_flush(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i64 -2147483648, 2147483648) i64 @bgzf_write(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = load i32, ptr %0, align 8
  %.mask = and i32 %4, 255
  %5 = icmp eq i32 %.mask, 119
  br i1 %5, label %.preheader, label %9

.preheader:                                       ; preds = %3
  %6 = icmp sgt i64 %2, 0
  br i1 %6, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %.preheader
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = getelementptr inbounds i8, ptr %0, i64 12
  br label %.backedge

9:                                                ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.3, i32 noundef 418, ptr noundef nonnull @__PRETTY_FUNCTION__.bgzf_write) #4
  unreachable

.backedge:                                        ; preds = %.backedge.backedge, %.lr.ph
  %10 = phi i64 [ 0, %.lr.ph ], [ %.be, %.backedge.backedge ]
  %.02837 = phi ptr [ %1, %.lr.ph ], [ %22, %.backedge.backedge ]
  %.02936 = phi i32 [ 0, %.lr.ph ], [ %23, %.backedge.backedge ]
  %11 = load ptr, ptr %7, align 8, !tbaa !5
  %12 = load i32, ptr %8, align 4, !tbaa !12
  %13 = sub nsw i32 65536, %12
  %14 = sext i32 %13 to i64
  %15 = sub nsw i64 %2, %10
  %. = tail call i64 @llvm.smin.i64(i64 %15, i64 %14)
  %16 = trunc i64 %. to i32
  %17 = sext i32 %12 to i64
  %18 = getelementptr inbounds i8, ptr %11, i64 %17
  %sext = shl i64 %., 32
  %19 = ashr exact i64 %sext, 32
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %18, ptr align 1 %.02837, i64 %19, i1 false)
  %20 = load i32, ptr %8, align 4, !tbaa !12
  %21 = add nsw i32 %20, %16
  store i32 %21, ptr %8, align 4, !tbaa !12
  %22 = getelementptr inbounds i8, ptr %.02837, i64 %19
  %23 = add nsw i32 %.02936, %16
  %24 = icmp eq i32 %21, 65536
  br i1 %24, label %25, label %29

25:                                               ; preds = %.backedge
  %26 = tail call i32 @bgzf_flush(ptr noundef nonnull %0)
  %.not = icmp eq i32 %26, 0
  %27 = sext i32 %23 to i64
  %28 = icmp slt i64 %27, %2
  %or.cond = select i1 %.not, i1 %28, i1 false
  br i1 %or.cond, label %.backedge.backedge, label %.thread.loopexit

29:                                               ; preds = %.backedge
  %.old = sext i32 %23 to i64
  %.old40 = icmp slt i64 %.old, %2
  br i1 %.old40, label %.backedge.backedge, label %.thread.loopexit

.backedge.backedge:                               ; preds = %29, %25
  %.be = phi i64 [ %.old, %29 ], [ %27, %25 ]
  br label %.backedge

.thread.loopexit:                                 ; preds = %25, %29
  %30 = sext i32 %23 to i64
  br label %.thread

.thread:                                          ; preds = %.thread.loopexit, %.preheader
  %.1 = phi i64 [ 0, %.preheader ], [ %30, %.thread.loopexit ]
  ret i64 %.1
}

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn 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 #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { noreturn 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, !11, i64 24}
!6 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !7, i64 12}


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