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

; 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_memstream = type { ptr, i64, i64, i64 }
%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

declare i32 @huf_malloc(ptr noundef, i64 noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @memwrite(ptr noundef, ptr noundef, i64 noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @huf_memopen(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %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 ptr, align 8
  %11 = alloca ptr, align 8
  %12 = 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  br label %13

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

16:                                               ; preds = %13
  store i32 2, ptr %8, align 4, !tbaa !11
  br label %77

17:                                               ; preds = %13
  br label %18

18:                                               ; preds = %17
  br label %19

19:                                               ; preds = %18
  br label %20

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

23:                                               ; preds = %20
  store i32 2, ptr %8, align 4, !tbaa !11
  br label %77

24:                                               ; preds = %20
  br label %25

25:                                               ; preds = %24
  br label %26

26:                                               ; preds = %25
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = call i32 @huf_malloc(ptr noundef %27, i64 noundef 1, i64 noundef %28)
  store i32 %29, ptr %9, align 4, !tbaa !11
  %30 = load i32, ptr %9, align 4, !tbaa !11
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %37

32:                                               ; preds = %26
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %34, ptr %8, align 4, !tbaa !11
  br label %77

35:                                               ; No predecessors!
  br label %36

36:                                               ; preds = %35
  br label %37

37:                                               ; preds = %36, %26
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = call i32 @huf_malloc(ptr noundef %38, i64 noundef 24, i64 noundef 1)
  store i32 %39, ptr %9, align 4, !tbaa !11
  %40 = load i32, ptr %9, align 4, !tbaa !11
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %47

42:                                               ; preds = %37
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %44, ptr %8, align 4, !tbaa !11
  br label %77

45:                                               ; No predecessors!
  br label %46

46:                                               ; preds = %45
  br label %47

47:                                               ; preds = %46, %37
  %48 = call i32 @huf_malloc(ptr noundef %10, i64 noundef 32, i64 noundef 1)
  store i32 %48, ptr %9, align 4, !tbaa !11
  %49 = load i32, ptr %9, align 4, !tbaa !11
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %56

51:                                               ; preds = %47
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %53, ptr %8, align 4, !tbaa !11
  br label %77

54:                                               ; No predecessors!
  br label %55

55:                                               ; preds = %54
  br label %56

56:                                               ; preds = %55, %47
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.__huf_memstream, ptr %58, i32 0, i32 0
  store ptr %57, ptr %59, align 8, !tbaa !13
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.__huf_memstream, ptr %60, i32 0, i32 2
  store i64 0, ptr %61, align 8, !tbaa !15
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.__huf_memstream, ptr %62, i32 0, i32 1
  store i64 0, ptr %63, align 8, !tbaa !16
  %64 = load i64, ptr %7, align 8, !tbaa !9
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.__huf_memstream, ptr %65, i32 0, i32 3
  store i64 %64, ptr %66, align 8, !tbaa !17
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = load ptr, ptr %67, align 8, !tbaa !5
  store ptr %68, ptr %11, align 8, !tbaa !5
  %69 = load ptr, ptr %10, align 8, !tbaa !5
  %70 = load ptr, ptr %11, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.__huf_read_writer, ptr %70, i32 0, i32 0
  store ptr %69, ptr %71, align 8, !tbaa !18
  %72 = load ptr, ptr %11, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.__huf_read_writer, ptr %72, i32 0, i32 1
  store ptr @memwrite, ptr %73, align 8, !tbaa !20
  %74 = load ptr, ptr %11, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.__huf_read_writer, ptr %74, i32 0, i32 2
  store ptr @memread, ptr %75, align 8, !tbaa !21
  br label %76

76:                                               ; preds = %56
  br label %77

77:                                               ; preds = %76, %52, %43, %33, %23, %16
  %78 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %78, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %81

79:                                               ; No predecessors!
  br label %80

80:                                               ; preds = %79
  store i32 0, ptr %12, align 4
  br label %81

81:                                               ; preds = %80, %77
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %82 = load i32, ptr %12, align 4
  switch i32 %82, label %85 [
    i32 0, label %83
    i32 1, label %83
  ]

83:                                               ; preds = %81, %81
  %84 = load i32, ptr %4, align 4
  ret i32 %84

85:                                               ; preds = %81
  unreachable
}

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

declare i32 @huf_malloc(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @memwrite(ptr noundef, ptr noundef, i64 noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @huf_memopen(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  %5 = icmp eq ptr %0, null
  %6 = icmp eq ptr %1, null
  %or.cond = or i1 %5, %6
  br i1 %or.cond, label %21, label %7

7:                                                ; preds = %3
  %8 = tail call i32 @huf_malloc(ptr noundef nonnull %1, i64 noundef 1, i64 noundef %2) #4
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %9, label %21

9:                                                ; preds = %7
  %10 = tail call i32 @huf_malloc(ptr noundef nonnull %0, i64 noundef 24, i64 noundef 1) #4
  %.not23 = icmp eq i32 %10, 0
  br i1 %.not23, label %11, label %21

11:                                               ; preds = %9
  %12 = call i32 @huf_malloc(ptr noundef nonnull %4, i64 noundef 32, i64 noundef 1) #4
  %.not24 = icmp eq i32 %12, 0
  br i1 %.not24, label %13, label %21

13:                                               ; preds = %11
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %14, align 8, !tbaa !9
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 8
  %17 = getelementptr inbounds i8, ptr %15, i64 24
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %16, i8 0, i64 16, i1 false)
  store i64 %2, ptr %17, align 8, !tbaa !12
  %18 = load ptr, ptr %0, align 8, !tbaa !5
  store ptr %15, ptr %18, align 8, !tbaa !13
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  store ptr @memwrite, ptr %19, align 8, !tbaa !15
  %20 = getelementptr inbounds i8, ptr %18, i64 16
  store ptr @memread, ptr %20, align 8, !tbaa !16
  br label %21

21:                                               ; preds = %11, %9, %7, %3, %13
  %.0 = phi i32 [ 0, %13 ], [ 2, %3 ], [ %8, %7 ], [ %10, %9 ], [ %12, %11 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #4
  ret i32 %.0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 nounwind willreturn memory(argmem: write) }
attributes #4 = { 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, !6, i64 0}
!10 = !{!"__huf_memstream", !6, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 24}
!13 = !{!14, !6, i64 0}
!14 = !{!"__huf_read_writer", !6, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !6, i64 16}


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