; LLVM IR from ybubnov/libhuffman :: src (bufio)

; 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.__huf_bufio_read_writer = type { ptr, i64, i64, i64, i64, ptr }
%struct.__huf_read_writer = type { 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
declare dso_local i32 @huf_bufio_read_writer_flush(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @huf_bufio_write(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  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 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %12
  store i32 2, ptr %8, align 4, !tbaa !11
  br label %113

16:                                               ; preds = %12
  br label %17

17:                                               ; preds = %16
  br label %18

18:                                               ; preds = %17
  br label %19

19:                                               ; preds = %18
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %23

22:                                               ; preds = %19
  store i32 2, ptr %8, align 4, !tbaa !11
  br label %113

23:                                               ; preds = %19
  br label %24

24:                                               ; preds = %23
  br label %25

25:                                               ; preds = %24
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = call i32 @__huf_bufio_read_writer_flush(ptr noundef %26)
  store i32 %27, ptr %9, align 4, !tbaa !11
  %28 = load i32, ptr %9, align 4, !tbaa !11
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %35

30:                                               ; preds = %25
  br label %31

31:                                               ; preds = %30
  %32 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %32, ptr %8, align 4, !tbaa !11
  br label %113

33:                                               ; No predecessors!
  br label %34

34:                                               ; preds = %33
  br label %35

35:                                               ; preds = %34, %25
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %36, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !13
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %39, i32 0, i32 3
  %41 = load i64, ptr %40, align 8, !tbaa !15
  %42 = sub i64 %38, %41
  store i64 %42, ptr %10, align 8, !tbaa !9
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %43, i32 0, i32 2
  %45 = load i64, ptr %44, align 8, !tbaa !13
  %46 = icmp ne i64 %45, 0
  br i1 %46, label %47, label %71

47:                                               ; preds = %35
  %48 = load i64, ptr %7, align 8, !tbaa !9
  %49 = load i64, ptr %10, align 8, !tbaa !9
  %50 = icmp ule i64 %48, %49
  br i1 %50, label %51, label %71

51:                                               ; preds = %47
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !16
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %55, i32 0, i32 3
  %57 = load i64, ptr %56, align 8, !tbaa !15
  %58 = getelementptr inbounds i8, ptr %54, i64 %57
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %58, ptr align 1 %59, i64 %60, i1 false)
  %61 = load i64, ptr %7, align 8, !tbaa !9
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %62, i32 0, i32 3
  %64 = load i64, ptr %63, align 8, !tbaa !15
  %65 = add i64 %64, %61
  store i64 %65, ptr %63, align 8, !tbaa !15
  %66 = load i64, ptr %7, align 8, !tbaa !9
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %67, i32 0, i32 4
  %69 = load i64, ptr %68, align 8, !tbaa !17
  %70 = add i64 %69, %66
  store i64 %70, ptr %68, align 8, !tbaa !17
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %71

71:                                               ; preds = %51, %47, %35
  %72 = load i64, ptr %7, align 8, !tbaa !9
  %73 = icmp ugt i64 %72, 0
  br i1 %73, label %74, label %111

74:                                               ; preds = %71
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = call i32 @huf_bufio_read_writer_flush(ptr noundef %75)
  store i32 %76, ptr %9, align 4, !tbaa !11
  %77 = load i32, ptr %9, align 4, !tbaa !11
  %78 = icmp ne i32 %77, 0
  br i1 %78, label %79, label %84

79:                                               ; preds = %74
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %81, ptr %8, align 4, !tbaa !11
  br label %113

82:                                               ; No predecessors!
  br label %83

83:                                               ; preds = %82
  br label %84

84:                                               ; preds = %83, %74
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %85, i32 0, i32 5
  %87 = load ptr, ptr %86, align 8, !tbaa !18
  %88 = getelementptr inbounds %struct.__huf_read_writer, ptr %87, i32 0, i32 1
  %89 = load ptr, ptr %88, align 8, !tbaa !19
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %90, i32 0, i32 5
  %92 = load ptr, ptr %91, align 8, !tbaa !18
  %93 = getelementptr inbounds %struct.__huf_read_writer, ptr %92, i32 0, i32 0
  %94 = load ptr, ptr %93, align 8, !tbaa !21
  %95 = load ptr, ptr %6, align 8, !tbaa !5
  %96 = load i64, ptr %7, align 8, !tbaa !9
  %97 = call i32 %89(ptr noundef %94, ptr noundef %95, i64 noundef %96)
  store i32 %97, ptr %9, align 4, !tbaa !11
  %98 = load i32, ptr %9, align 4, !tbaa !11
  %99 = icmp ne i32 %98, 0
  br i1 %99, label %100, label %105

100:                                              ; preds = %84
  br label %101

101:                                              ; preds = %100
  %102 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %102, ptr %8, align 4, !tbaa !11
  br label %113

103:                                              ; No predecessors!
  br label %104

104:                                              ; preds = %103
  br label %105

105:                                              ; preds = %104, %84
  %106 = load i64, ptr %7, align 8, !tbaa !9
  %107 = load ptr, ptr %5, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.__huf_bufio_read_writer, ptr %107, i32 0, i32 4
  %109 = load i64, ptr %108, align 8, !tbaa !17
  %110 = add i64 %109, %106
  store i64 %110, ptr %108, align 8, !tbaa !17
  br label %111

111:                                              ; preds = %105, %71
  br label %112

112:                                              ; preds = %111
  br label %113

113:                                              ; preds = %112, %101, %80, %31, %22, %15
  %114 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %114, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %117

115:                                              ; No predecessors!
  br label %116

116:                                              ; preds = %115
  store i32 0, ptr %11, align 4
  br label %117

117:                                              ; preds = %116, %113
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %118 = load i32, ptr %11, align 4
  switch i32 %118, label %121 [
    i32 0, label %119
    i32 1, label %119
  ]

119:                                              ; preds = %117, %117
  %120 = load i32, ptr %4, align 4
  ret i32 %120

121:                                              ; preds = %117
  unreachable
}

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

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

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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, !10, i64 16}
!14 = !{!"__huf_bufio_read_writer", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !6, i64 40}
!15 = !{!14, !10, i64 24}
!16 = !{!14, !6, i64 0}
!17 = !{!14, !10, i64 32}
!18 = !{!14, !6, i64 40}
!19 = !{!20, !6, i64 8}
!20 = !{!"__huf_read_writer", !6, i64 0, !6, i64 8, !6, i64 16}
!21 = !{!20, !6, i64 0}


; 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: nounwind uwtable
declare dso_local i32 @huf_bufio_read_writer_flush(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @huf_bufio_write(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %1, null
  %or.cond43 = or i1 %4, %5
  br i1 %or.cond43, label %31, label %6

6:                                                ; preds = %3
  %7 = tail call i32 @__huf_bufio_read_writer_flush(ptr noundef nonnull %0)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %31

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %.not38 = icmp eq i64 %10, 0
  %13 = sub i64 %10, %12
  %.not39 = icmp ult i64 %13, %2
  %or.cond = select i1 %.not38, i1 true, i1 %.not39
  br i1 %or.cond, label %18, label %.thread

.thread:                                          ; preds = %8
  %14 = load ptr, ptr %0, align 8, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %14, i64 %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %15, ptr nonnull align 1 %1, i64 %2, i1 false)
  %16 = load i64, ptr %11, align 8, !tbaa !11
  %17 = add i64 %16, %2
  store i64 %17, ptr %11, align 8, !tbaa !11
  br label %.sink.split

18:                                               ; preds = %8
  %.not40 = icmp eq i64 %2, 0
  br i1 %.not40, label %31, label %19

19:                                               ; preds = %18
  %20 = tail call i32 @huf_bufio_read_writer_flush(ptr noundef nonnull %0)
  %.not41 = icmp eq i32 %20, 0
  br i1 %.not41, label %21, label %31

21:                                               ; preds = %19
  %22 = getelementptr inbounds i8, ptr %0, i64 40
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = load ptr, ptr %23, align 8, !tbaa !16
  %27 = tail call i32 %25(ptr noundef %26, ptr noundef nonnull %1, i64 noundef %2) #2
  %.not42 = icmp eq i32 %27, 0
  br i1 %.not42, label %.sink.split, label %31

.sink.split:                                      ; preds = %21, %.thread
  %28 = getelementptr inbounds i8, ptr %0, i64 32
  %29 = load i64, ptr %28, align 8, !tbaa !17
  %30 = add i64 %29, %2
  store i64 %30, ptr %28, align 8, !tbaa !17
  br label %31

31:                                               ; preds = %.sink.split, %21, %19, %6, %3, %18
  %.031 = phi i32 [ 0, %18 ], [ 2, %3 ], [ %7, %6 ], [ %20, %19 ], [ %27, %21 ], [ 0, %.sink.split ]
  ret i32 %.031
}

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

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

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 16}
!6 = !{!"__huf_bufio_read_writer", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !7, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !7, i64 40}
!14 = !{!15, !7, i64 8}
!15 = !{!"__huf_read_writer", !7, i64 0, !7, i64 8, !7, i64 16}
!16 = !{!15, !7, i64 0}
!17 = !{!6, !10, i64 32}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass