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

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

; 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
define dso_local i32 @bgzf_read_block(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca [18 x i8], align 16
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 18, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.BGZF, ptr %12, i32 0, i32 8
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = call i64 @ftello(ptr noundef %14)
  store i64 %15, ptr %10, align 8, !tbaa !14
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i64, ptr %10, align 8, !tbaa !14
  %18 = call i32 @load_block_from_cache(ptr noundef %16, i64 noundef %17)
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  store i32 1, ptr %11, align 4
  br label %115

21:                                               ; preds = %1
  %22 = getelementptr inbounds [18 x i8], ptr %4, i64 0, i64 0
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.BGZF, ptr %23, i32 0, i32 8
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = call i64 @fread(ptr noundef %22, i64 noundef 1, i64 noundef 18, ptr noundef %25)
  %27 = trunc i64 %26 to i32
  store i32 %27, ptr %6, align 4, !tbaa !9
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %33

30:                                               ; preds = %21
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.BGZF, ptr %31, i32 0, i32 2
  store i32 0, ptr %32, align 8, !tbaa !15
  store i32 0, ptr %2, align 4
  store i32 1, ptr %11, align 4
  br label %115

33:                                               ; preds = %21
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = icmp ne i64 %35, 18
  br i1 %36, label %41, label %37

37:                                               ; preds = %33
  %38 = getelementptr inbounds [18 x i8], ptr %4, i64 0, i64 0
  %39 = call i32 @check_header(ptr noundef %38)
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %53, label %41

41:                                               ; preds = %37, %33
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = load i32, ptr %42, align 8
  %44 = ashr i32 %43, 16
  %45 = or i32 %44, 2
  %46 = load i32, ptr %42, align 8
  %47 = and i32 %45, 65535
  %48 = shl i32 %47, 16
  %49 = and i32 %46, 65535
  %50 = or i32 %49, %48
  store i32 %50, ptr %42, align 8
  %51 = shl i32 %47, 16
  %52 = ashr i32 %51, 16
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %11, align 4
  br label %115

53:                                               ; preds = %37
  %54 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %54, ptr %7, align 4, !tbaa !9
  %55 = getelementptr inbounds [18 x i8], ptr %4, i64 0, i64 16
  %56 = call i32 @unpackInt16(ptr noundef %55)
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %8, align 4, !tbaa !9
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.BGZF, ptr %58, i32 0, i32 6
  %60 = load ptr, ptr %59, align 8, !tbaa !16
  store ptr %60, ptr %5, align 8, !tbaa !5
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds [18 x i8], ptr %4, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %61, ptr align 16 %62, i64 18, i1 false)
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = sub nsw i32 %63, 18
  store i32 %64, ptr %9, align 4, !tbaa !9
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds i8, ptr %65, i64 18
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.BGZF, ptr %69, i32 0, i32 8
  %71 = load ptr, ptr %70, align 8, !tbaa !11
  %72 = call i64 @fread(ptr noundef %66, i64 noundef 1, i64 noundef %68, ptr noundef %71)
  %73 = trunc i64 %72 to i32
  store i32 %73, ptr %6, align 4, !tbaa !9
  %74 = load i32, ptr %6, align 4, !tbaa !9
  %75 = load i32, ptr %9, align 4, !tbaa !9
  %76 = icmp ne i32 %74, %75
  br i1 %76, label %77, label %89

77:                                               ; preds = %53
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = load i32, ptr %78, align 8
  %80 = ashr i32 %79, 16
  %81 = or i32 %80, 4
  %82 = load i32, ptr %78, align 8
  %83 = and i32 %81, 65535
  %84 = shl i32 %83, 16
  %85 = and i32 %82, 65535
  %86 = or i32 %85, %84
  store i32 %86, ptr %78, align 8
  %87 = shl i32 %83, 16
  %88 = ashr i32 %87, 16
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %11, align 4
  br label %115

89:                                               ; preds = %53
  %90 = load i32, ptr %6, align 4, !tbaa !9
  %91 = load i32, ptr %7, align 4, !tbaa !9
  %92 = add nsw i32 %91, %90
  store i32 %92, ptr %7, align 4, !tbaa !9
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = load i32, ptr %8, align 4, !tbaa !9
  %95 = call i32 @inflate_block(ptr noundef %93, i32 noundef %94)
  store i32 %95, ptr %6, align 4, !tbaa !9
  %96 = icmp slt i32 %95, 0
  br i1 %96, label %97, label %98

97:                                               ; preds = %89
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %11, align 4
  br label %115

98:                                               ; preds = %89
  %99 = load ptr, ptr %3, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.BGZF, ptr %99, i32 0, i32 2
  %101 = load i32, ptr %100, align 8, !tbaa !15
  %102 = icmp ne i32 %101, 0
  br i1 %102, label %103, label %106

103:                                              ; preds = %98
  %104 = load ptr, ptr %3, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.BGZF, ptr %104, i32 0, i32 3
  store i32 0, ptr %105, align 4, !tbaa !17
  br label %106

106:                                              ; preds = %103, %98
  %107 = load i64, ptr %10, align 8, !tbaa !14
  %108 = load ptr, ptr %3, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.BGZF, ptr %108, i32 0, i32 4
  store i64 %107, ptr %109, align 8, !tbaa !18
  %110 = load i32, ptr %6, align 4, !tbaa !9
  %111 = load ptr, ptr %3, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.BGZF, ptr %111, i32 0, i32 2
  store i32 %110, ptr %112, align 8, !tbaa !15
  %113 = load ptr, ptr %3, align 8, !tbaa !5
  %114 = load i32, ptr %7, align 4, !tbaa !9
  call void @cache_block(ptr noundef %113, i32 noundef %114)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %11, align 4
  br label %115

115:                                              ; preds = %106, %97, %77, %41, %30, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 18, ptr %4) #5
  %116 = load i32, ptr %2, align 4
  ret i32 %116
}

declare i64 @ftello(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @load_block_from_cache(ptr noundef, i64 noundef) #1

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @check_header(ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @unpackInt16(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
declare hidden i32 @inflate_block(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @cache_block(ptr noundef, i32 noundef) #1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @bgzf_read_block(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca [18 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 18, ptr nonnull %2) #5
  %3 = getelementptr inbounds i8, ptr %0, i64 48
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = tail call i64 @ftello(ptr noundef %4)
  %6 = tail call i32 @load_block_from_cache(ptr noundef %0, i64 noundef %5)
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %7, label %46

7:                                                ; preds = %1
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call i64 @fread(ptr noundef nonnull %2, i64 noundef 1, i64 noundef 18, ptr noundef %8)
  %10 = trunc i64 %9 to i32
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %14

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 0, ptr %13, align 8, !tbaa !12
  br label %46

14:                                               ; preds = %7
  %sext.mask = and i64 %9, 4294967295
  %.not33 = icmp eq i64 %sext.mask, 18
  br i1 %.not33, label %15, label %17

15:                                               ; preds = %14
  %16 = call i32 @check_header(ptr noundef nonnull %2)
  %.not34 = icmp eq i32 %16, 0
  br i1 %.not34, label %17, label %20

17:                                               ; preds = %15, %14
  %18 = load i32, ptr %0, align 8
  %19 = or i32 %18, 131072
  store i32 %19, ptr %0, align 8
  br label %46

20:                                               ; preds = %15
  %21 = getelementptr inbounds i8, ptr %2, i64 16
  %22 = call i32 @unpackInt16(ptr noundef nonnull %21)
  %23 = getelementptr inbounds i8, ptr %0, i64 32
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(18) %24, ptr noundef nonnull align 16 dereferenceable(18) %2, i64 18, i1 false)
  %25 = add nsw i32 %22, -17
  %26 = getelementptr inbounds i8, ptr %24, i64 18
  %27 = sext i32 %25 to i64
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = call i64 @fread(ptr noundef nonnull %26, i64 noundef 1, i64 noundef %27, ptr noundef %28)
  %30 = trunc i64 %29 to i32
  %.not35 = icmp eq i32 %25, %30
  br i1 %.not35, label %34, label %31

31:                                               ; preds = %20
  %32 = load i32, ptr %0, align 8
  %33 = or i32 %32, 262144
  store i32 %33, ptr %0, align 8
  br label %46

34:                                               ; preds = %20
  %35 = add nsw i32 %22, 1
  %36 = add nsw i32 %25, %10
  %37 = call i32 @inflate_block(ptr noundef nonnull %0, i32 noundef %35)
  %38 = icmp slt i32 %37, 0
  br i1 %38, label %46, label %39

39:                                               ; preds = %34
  %40 = getelementptr inbounds i8, ptr %0, i64 8
  %41 = load i32, ptr %40, align 8, !tbaa !12
  %.not36 = icmp eq i32 %41, 0
  br i1 %.not36, label %44, label %42

42:                                               ; preds = %39
  %43 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 0, ptr %43, align 4, !tbaa !14
  br label %44

44:                                               ; preds = %42, %39
  %45 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %5, ptr %45, align 8, !tbaa !15
  store i32 %37, ptr %40, align 8, !tbaa !12
  call void @cache_block(ptr noundef nonnull %0, i32 noundef %36)
  br label %46

46:                                               ; preds = %34, %1, %44, %31, %17, %12
  %.0 = phi i32 [ 0, %12 ], [ -1, %17 ], [ -1, %31 ], [ 0, %44 ], [ 0, %1 ], [ -1, %34 ]
  call void @llvm.lifetime.end.p0(i64 18, ptr nonnull %2) #5
  ret i32 %.0
}

; Function Attrs: nofree nounwind
declare noundef i64 @ftello(ptr nocapture noundef) local_unnamed_addr #2

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

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @check_header(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @unpackInt16(ptr noundef) local_unnamed_addr #3

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

; Function Attrs: nounwind uwtable
declare hidden i32 @inflate_block(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @cache_block(ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { 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 #3 = { 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 48}
!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 32}
!14 = !{!6, !7, i64 12}
!15 = !{!6, !10, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass