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

; 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.2 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [61 x i8], align 1
@__PRETTY_FUNCTION__.bgzf_read = external hidden unnamed_addr constant [43 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: nounwind uwtable
declare dso_local i32 @bgzf_read_block(ptr noundef) #1

declare i64 @ftello(ptr noundef) #2

; 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) #3

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

17:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %124

18:                                               ; preds = %3
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load i32, ptr %19, align 8
  %21 = shl i32 %20, 24
  %22 = ashr i32 %21, 24
  %23 = icmp eq i32 %22, 114
  br i1 %23, label %24, label %25

24:                                               ; preds = %18
  br label %26

25:                                               ; preds = %18
  call void @__assert_fail(ptr noundef @.str.2, ptr noundef @.str.3, i32 noundef 368, ptr noundef @__PRETTY_FUNCTION__.bgzf_read) #6
  unreachable

26:                                               ; preds = %24
  br label %27

27:                                               ; preds = %102, %26
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = icmp slt i64 %28, %29
  br i1 %30, label %31, label %103

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.BGZF, ptr %32, i32 0, i32 2
  %34 = load i32, ptr %33, align 8, !tbaa !11
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.BGZF, ptr %35, i32 0, i32 3
  %37 = load i32, ptr %36, align 4, !tbaa !14
  %38 = sub nsw i32 %34, %37
  store i32 %38, ptr %12, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %39 = load i32, ptr %12, align 4, !tbaa !15
  %40 = icmp sle i32 %39, 0
  br i1 %40, label %41, label %58

41:                                               ; preds = %31
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = call i32 @bgzf_read_block(ptr noundef %42)
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %41
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %100

46:                                               ; preds = %41
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.BGZF, ptr %47, i32 0, i32 2
  %49 = load i32, ptr %48, align 8, !tbaa !11
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.BGZF, ptr %50, i32 0, i32 3
  %52 = load i32, ptr %51, align 4, !tbaa !14
  %53 = sub nsw i32 %49, %52
  store i32 %53, ptr %12, align 4, !tbaa !15
  %54 = load i32, ptr %12, align 4, !tbaa !15
  %55 = icmp sle i32 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %46
  store i32 3, ptr %10, align 4
  br label %100

57:                                               ; preds = %46
  br label %58

58:                                               ; preds = %57, %31
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = load i64, ptr %8, align 8, !tbaa !9
  %61 = sub nsw i64 %59, %60
  %62 = load i32, ptr %12, align 4, !tbaa !15
  %63 = sext i32 %62 to i64
  %64 = icmp slt i64 %61, %63
  br i1 %64, label %65, label %69

65:                                               ; preds = %58
  %66 = load i64, ptr %7, align 8, !tbaa !9
  %67 = load i64, ptr %8, align 8, !tbaa !9
  %68 = sub nsw i64 %66, %67
  br label %72

69:                                               ; preds = %58
  %70 = load i32, ptr %12, align 4, !tbaa !15
  %71 = sext i32 %70 to i64
  br label %72

72:                                               ; preds = %69, %65
  %73 = phi i64 [ %68, %65 ], [ %71, %69 ]
  %74 = trunc i64 %73 to i32
  store i32 %74, ptr %11, align 4, !tbaa !15
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.BGZF, ptr %75, i32 0, i32 5
  %77 = load ptr, ptr %76, align 8, !tbaa !16
  store ptr %77, ptr %13, align 8, !tbaa !5
  %78 = load ptr, ptr %9, align 8, !tbaa !5
  %79 = load ptr, ptr %13, align 8, !tbaa !5
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.BGZF, ptr %80, i32 0, i32 3
  %82 = load i32, ptr %81, align 4, !tbaa !14
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds i8, ptr %79, i64 %83
  %85 = load i32, ptr %11, align 4, !tbaa !15
  %86 = sext i32 %85 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %78, ptr align 1 %84, i64 %86, i1 false)
  %87 = load i32, ptr %11, align 4, !tbaa !15
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.BGZF, ptr %88, i32 0, i32 3
  %90 = load i32, ptr %89, align 4, !tbaa !14
  %91 = add nsw i32 %90, %87
  store i32 %91, ptr %89, align 4, !tbaa !14
  %92 = load i32, ptr %11, align 4, !tbaa !15
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  %94 = sext i32 %92 to i64
  %95 = getelementptr inbounds i8, ptr %93, i64 %94
  store ptr %95, ptr %9, align 8, !tbaa !5
  %96 = load i32, ptr %11, align 4, !tbaa !15
  %97 = sext i32 %96 to i64
  %98 = load i64, ptr %8, align 8, !tbaa !9
  %99 = add nsw i64 %98, %97
  store i64 %99, ptr %8, align 8, !tbaa !9
  store i32 0, ptr %10, align 4
  br label %100

100:                                              ; preds = %72, %56, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  %101 = load i32, ptr %10, align 4
  switch i32 %101, label %124 [
    i32 0, label %102
    i32 3, label %103
  ]

102:                                              ; preds = %100
  br label %27, !llvm.loop !17

103:                                              ; preds = %100, %27
  %104 = load ptr, ptr %5, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.BGZF, ptr %104, i32 0, i32 3
  %106 = load i32, ptr %105, align 4, !tbaa !14
  %107 = load ptr, ptr %5, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.BGZF, ptr %107, i32 0, i32 2
  %109 = load i32, ptr %108, align 8, !tbaa !11
  %110 = icmp eq i32 %106, %109
  br i1 %110, label %111, label %122

111:                                              ; preds = %103
  %112 = load ptr, ptr %5, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.BGZF, ptr %112, i32 0, i32 8
  %114 = load ptr, ptr %113, align 8, !tbaa !19
  %115 = call i64 @ftello(ptr noundef %114)
  %116 = load ptr, ptr %5, align 8, !tbaa !5
  %117 = getelementptr inbounds %struct.BGZF, ptr %116, i32 0, i32 4
  store i64 %115, ptr %117, align 8, !tbaa !20
  %118 = load ptr, ptr %5, align 8, !tbaa !5
  %119 = getelementptr inbounds %struct.BGZF, ptr %118, i32 0, i32 2
  store i32 0, ptr %119, align 8, !tbaa !11
  %120 = load ptr, ptr %5, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct.BGZF, ptr %120, i32 0, i32 3
  store i32 0, ptr %121, align 4, !tbaa !14
  br label %122

122:                                              ; preds = %111, %103
  %123 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %123, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %124

124:                                              ; preds = %122, %100, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %125 = load i64, ptr %4, align 8
  ret i64 %125
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { 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, !13, i64 8}
!12 = !{!"", !13, i64 0, !13, i64 1, !13, i64 2, !13, i64 4, !13, i64 8, !13, i64 12, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 12}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !6, i64 24}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!12, !6, i64 48}
!20 = !{!12, !10, i64 16}


; 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.2 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [61 x i8], align 1
@__PRETTY_FUNCTION__.bgzf_read = external hidden unnamed_addr constant [43 x i8], align 1

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

; Function Attrs: nofree nounwind
declare noundef i64 @ftello(ptr nocapture noundef) local_unnamed_addr #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) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @bgzf_read(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp slt i64 %2, 1
  br i1 %4, label %.thread, label %5

5:                                                ; preds = %3
  %6 = load i32, ptr %0, align 8
  %.mask = and i32 %6, 255
  %7 = icmp eq i32 %.mask, 114
  br i1 %7, label %.lr.ph, label %11

.lr.ph:                                           ; preds = %5
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = getelementptr inbounds i8, ptr %0, i64 12
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %.pre = load i32, ptr %9, align 4, !tbaa !5
  br label %12

11:                                               ; preds = %5
  tail call void @__assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, i32 noundef 368, ptr noundef nonnull @__PRETTY_FUNCTION__.bgzf_read) #5
  unreachable

12:                                               ; preds = %.lr.ph, %24
  %13 = phi i32 [ %.pre, %.lr.ph ], [ %35, %24 ]
  %.03856 = phi i64 [ 0, %.lr.ph ], [ %37, %24 ]
  %.04255 = phi ptr [ %1, %.lr.ph ], [ %36, %24 ]
  %14 = load i32, ptr %8, align 8, !tbaa !12
  %15 = sub nsw i32 %14, %13
  %16 = icmp slt i32 %15, 1
  br i1 %16, label %17, label %24

17:                                               ; preds = %12
  %18 = tail call i32 @bgzf_read_block(ptr noundef nonnull %0)
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %19, label %.thread

19:                                               ; preds = %17
  %20 = load i32, ptr %8, align 8, !tbaa !12
  %21 = load i32, ptr %9, align 4, !tbaa !5
  %22 = sub nsw i32 %20, %21
  %23 = icmp slt i32 %22, 1
  br i1 %23, label %.thread50, label %24

24:                                               ; preds = %12, %19
  %25 = phi i32 [ %21, %19 ], [ %13, %12 ]
  %.037 = phi i32 [ %22, %19 ], [ %15, %12 ]
  %26 = sub nsw i64 %2, %.03856
  %27 = zext nneg i32 %.037 to i64
  %28 = tail call i64 @llvm.smin.i64(i64 %26, i64 %27)
  %29 = trunc i64 %28 to i32
  %30 = load ptr, ptr %10, align 8, !tbaa !13
  %31 = sext i32 %25 to i64
  %32 = getelementptr inbounds i8, ptr %30, i64 %31
  %sext = shl i64 %28, 32
  %33 = ashr exact i64 %sext, 32
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.04255, ptr align 1 %32, i64 %33, i1 false)
  %34 = load i32, ptr %9, align 4, !tbaa !5
  %35 = add nsw i32 %34, %29
  store i32 %35, ptr %9, align 4, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %.04255, i64 %33
  %37 = add nsw i64 %33, %.03856
  %38 = icmp slt i64 %37, %2
  br i1 %38, label %12, label %.thread50

.thread50:                                        ; preds = %24, %19
  %39 = phi i32 [ %35, %24 ], [ %21, %19 ]
  %.038.lcssa.ph = phi i64 [ %37, %24 ], [ %.03856, %19 ]
  %40 = getelementptr inbounds i8, ptr %0, i64 8
  %41 = load i32, ptr %40, align 8, !tbaa !12
  %42 = icmp eq i32 %39, %41
  br i1 %42, label %43, label %.thread

43:                                               ; preds = %.thread50
  %44 = getelementptr inbounds i8, ptr %0, i64 12
  %45 = getelementptr inbounds i8, ptr %0, i64 48
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = tail call i64 @ftello(ptr noundef %46)
  %48 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %47, ptr %48, align 8, !tbaa !15
  store i32 0, ptr %40, align 8, !tbaa !12
  store i32 0, ptr %44, align 4, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %17, %.thread50, %43, %3
  %.2 = phi i64 [ 0, %3 ], [ %.038.lcssa.ph, %43 ], [ %.038.lcssa.ph, %.thread50 ], [ -1, %17 ]
  ret i64 %.2
}

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

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

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 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !7, i64 12}
!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 8}
!13 = !{!6, !11, i64 24}
!14 = !{!6, !11, i64 48}
!15 = !{!6, !10, i64 16}


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