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

; 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_flush = external hidden unnamed_addr constant [23 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: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @bgzf_flush(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = load i32, ptr %6, align 8
  %8 = shl i32 %7, 24
  %9 = ashr i32 %8, 24
  %10 = icmp eq i32 %9, 119
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  br label %13

12:                                               ; preds = %1
  call void @__assert_fail(ptr noundef @.str.4, ptr noundef @.str.3, i32 noundef 393, ptr noundef @__PRETTY_FUNCTION__.bgzf_flush) #4
  unreachable

13:                                               ; preds = %11
  br label %14

14:                                               ; preds = %62, %13
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.BGZF, ptr %15, i32 0, i32 3
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %19, label %63

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.BGZF, ptr %21, i32 0, i32 3
  %23 = load i32, ptr %22, align 4, !tbaa !9
  %24 = call i32 @deflate_block(ptr noundef %20, i32 noundef %23)
  store i32 %24, ptr %4, align 4, !tbaa !13
  %25 = load i32, ptr %4, align 4, !tbaa !13
  %26 = icmp slt i32 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %19
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %60

28:                                               ; preds = %19
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.BGZF, ptr %29, i32 0, i32 6
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  %32 = load i32, ptr %4, align 4, !tbaa !13
  %33 = sext i32 %32 to i64
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.BGZF, ptr %34, i32 0, i32 8
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = call i64 @fwrite(ptr noundef %31, i64 noundef 1, i64 noundef %33, ptr noundef %36)
  %38 = load i32, ptr %4, align 4, !tbaa !13
  %39 = sext i32 %38 to i64
  %40 = icmp ne i64 %37, %39
  br i1 %40, label %41, label %53

41:                                               ; preds = %28
  %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, 4
  %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 %5, align 4
  br label %60

53:                                               ; preds = %28
  %54 = load i32, ptr %4, align 4, !tbaa !13
  %55 = sext i32 %54 to i64
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.BGZF, ptr %56, i32 0, i32 4
  %58 = load i64, ptr %57, align 8, !tbaa !16
  %59 = add nsw i64 %58, %55
  store i64 %59, ptr %57, align 8, !tbaa !16
  store i32 0, ptr %5, align 4
  br label %60

60:                                               ; preds = %53, %41, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  %61 = load i32, ptr %5, align 4
  switch i32 %61, label %66 [
    i32 0, label %62
    i32 1, label %64
  ]

62:                                               ; preds = %60
  br label %14, !llvm.loop !17

63:                                               ; preds = %14
  store i32 0, ptr %2, align 4
  br label %64

64:                                               ; preds = %63, %60
  %65 = load i32, ptr %2, align 4
  ret i32 %65

66:                                               ; preds = %60
  unreachable
}

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

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync 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 = { "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 = { noreturn nounwind }
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, !11, i64 12}
!10 = !{!"", !11, i64 0, !11, i64 1, !11, i64 2, !11, i64 4, !11, i64 8, !11, i64 12, !12, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 32}
!15 = !{!10, !6, i64 48}
!16 = !{!10, !12, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"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_flush = external hidden unnamed_addr constant [23 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @bgzf_flush(ptr noundef %0) local_unnamed_addr #1 {
  %2 = load i32, ptr %0, align 8
  %.mask = and i32 %2, 255
  %3 = icmp eq i32 %.mask, 119
  br i1 %3, label %.preheader, label %10

.preheader:                                       ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %.preheader
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  br label %11

10:                                               ; preds = %1
  tail call void @__assert_fail(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.3, i32 noundef 393, ptr noundef nonnull @__PRETTY_FUNCTION__.bgzf_flush) #3
  unreachable

11:                                               ; preds = %.lr.ph, %23
  %12 = phi i32 [ %5, %.lr.ph ], [ %26, %23 ]
  %13 = tail call i32 @deflate_block(ptr noundef nonnull %0, i32 noundef %12)
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %.thread, label %15

15:                                               ; preds = %11
  %16 = load ptr, ptr %7, align 8, !tbaa !12
  %17 = zext nneg i32 %13 to i64
  %18 = load ptr, ptr %8, align 8, !tbaa !13
  %19 = tail call i64 @fwrite(ptr noundef %16, i64 noundef 1, i64 noundef %17, ptr noundef %18)
  %.not = icmp eq i64 %19, %17
  br i1 %.not, label %23, label %20

20:                                               ; preds = %15
  %21 = load i32, ptr %0, align 8
  %22 = or i32 %21, 262144
  store i32 %22, ptr %0, align 8
  br label %.thread

23:                                               ; preds = %15
  %24 = load i64, ptr %9, align 8, !tbaa !14
  %25 = add nsw i64 %24, %17
  store i64 %25, ptr %9, align 8, !tbaa !14
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = icmp sgt i32 %26, 0
  br i1 %27, label %11, label %.thread

.thread:                                          ; preds = %23, %11, %.preheader, %20
  %.2 = phi i32 [ -1, %20 ], [ 0, %.preheader ], [ 0, %23 ], [ -1, %11 ]
  ret i32 %.2
}

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

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

attributes #0 = { 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 #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 = { 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, !11, i64 32}
!13 = !{!6, !11, i64 48}
!14 = !{!6, !10, i64 16}


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