; LLVM IR from specinfo-ua/UAPKI :: library/uapki/src/content-hasher.ll (ZN7UapkiNS13ContentHasher10digestFileE7HashAlg)

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

%"class.UapkiNS::ContentHasher" = type { i32, i32, ptr, i8, %"class.std::__cxx11::basic_string", ptr, i64, %"class.UapkiNS::SmartBA" }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }
%"class.UapkiNS::SmartBA" = type { ptr }
%struct.ByteArray_st = type { ptr, i64 }

@.str = external hidden unnamed_addr constant [25 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: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7UapkiNS7SmartBAadEv(ptr noundef nonnull align 8 dereferenceable(8)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7UapkiNS13ContentHasher10digestFileE7HashAlg(ptr noundef nonnull align 8 dereferenceable(80) %0, i32 noundef %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %9 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 0, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  store ptr null, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store ptr null, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds %"class.UapkiNS::ContentHasher", ptr %9, i32 0, i32 4
  %11 = call noundef ptr @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv(ptr noundef nonnull align 8 dereferenceable(32) %10) #5
  %12 = call ptr @fopen_utf8(ptr noundef %11, i32 noundef 0)
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %20, label %15

15:                                               ; preds = %2
  br label %16

16:                                               ; preds = %15
  store i32 4128, ptr %5, align 4, !tbaa !11
  %17 = load i32, ptr %5, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 185, i32 noundef %17, ptr noundef null)
  br label %83

18:                                               ; No predecessors!
  br label %19

19:                                               ; preds = %18
  br label %20

20:                                               ; preds = %19, %2
  br label %21

21:                                               ; preds = %20
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = call ptr @hash_alloc(i32 noundef %22)
  store ptr %23, ptr %6, align 8, !tbaa !5
  %24 = icmp eq ptr null, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %21
  store i32 2, ptr %5, align 4, !tbaa !11
  %26 = load i32, ptr %5, align 4, !tbaa !11
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 188, i32 noundef %26)
  br label %83

27:                                               ; preds = %21
  br label %28

28:                                               ; preds = %27
  br label %29

29:                                               ; preds = %28
  br label %30

30:                                               ; preds = %29
  %31 = call ptr @ba_alloc_by_len(i64 noundef 10485760)
  store ptr %31, ptr %7, align 8, !tbaa !5
  %32 = icmp eq ptr null, %31
  br i1 %32, label %33, label %35

33:                                               ; preds = %30
  store i32 2, ptr %5, align 4, !tbaa !11
  %34 = load i32, ptr %5, align 4, !tbaa !11
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 190, i32 noundef %34)
  br label %83

35:                                               ; preds = %30
  br label %36

36:                                               ; preds = %35
  br label %37

37:                                               ; preds = %36
  br label %38

38:                                               ; preds = %56, %37
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = call ptr @ba_get_buf(ptr noundef %39)
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = call i64 @fread(ptr noundef %40, i64 noundef 1, i64 noundef 10485760, ptr noundef %41)
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ByteArray_st, ptr %43, i32 0, i32 1
  store i64 %42, ptr %44, align 8, !tbaa !13
  br label %45

45:                                               ; preds = %38
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = call i32 @hash_update(ptr noundef %46, ptr noundef %47)
  store i32 %48, ptr %5, align 4, !tbaa !11
  %49 = load i32, ptr %5, align 4, !tbaa !11
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %53

51:                                               ; preds = %45
  %52 = load i32, ptr %5, align 4, !tbaa !11
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 194, i32 noundef %52)
  br label %83

53:                                               ; preds = %45
  br label %54

54:                                               ; preds = %53
  br label %55

55:                                               ; preds = %54
  br label %56

56:                                               ; preds = %55
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.ByteArray_st, ptr %57, i32 0, i32 1
  %59 = load i64, ptr %58, align 8, !tbaa !13
  %60 = icmp eq i64 %59, 10485760
  br i1 %60, label %38, label %61, !llvm.loop !16

61:                                               ; preds = %56
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = call i32 @ferror(ptr noundef %62) #5
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %70

65:                                               ; preds = %61
  br label %66

66:                                               ; preds = %65
  store i32 4129, ptr %5, align 4, !tbaa !11
  %67 = load i32, ptr %5, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 198, i32 noundef %67, ptr noundef null)
  br label %83

68:                                               ; No predecessors!
  br label %69

69:                                               ; preds = %68
  br label %70

70:                                               ; preds = %69, %61
  br label %71

71:                                               ; preds = %70
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %"class.UapkiNS::ContentHasher", ptr %9, i32 0, i32 7
  %74 = call noundef ptr @_ZN7UapkiNS7SmartBAadEv(ptr noundef nonnull align 8 dereferenceable(8) %73)
  %75 = call i32 @hash_final(ptr noundef %72, ptr noundef %74)
  store i32 %75, ptr %5, align 4, !tbaa !11
  %76 = load i32, ptr %5, align 4, !tbaa !11
  %77 = icmp ne i32 %76, 0
  br i1 %77, label %78, label %80

78:                                               ; preds = %71
  %79 = load i32, ptr %5, align 4, !tbaa !11
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 201, i32 noundef %79)
  br label %83

80:                                               ; preds = %71
  br label %81

81:                                               ; preds = %80
  br label %82

82:                                               ; preds = %81
  br label %83

83:                                               ; preds = %82, %78, %66, %51, %33, %25, %16
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = icmp ne ptr %84, null
  br i1 %85, label %86, label %89

86:                                               ; preds = %83
  %87 = load ptr, ptr %8, align 8, !tbaa !5
  %88 = call i32 @fclose(ptr noundef %87)
  br label %89

89:                                               ; preds = %86, %83
  %90 = load ptr, ptr %7, align 8, !tbaa !5
  call void @ba_free(ptr noundef %90)
  %91 = load ptr, ptr %6, align 8, !tbaa !5
  call void @hash_free(ptr noundef %91)
  %92 = load i32, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  ret i32 %92
}

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

declare void @ba_free(ptr noundef) #3

declare ptr @fopen_utf8(ptr noundef, i32 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare noundef ptr @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv(ptr noundef nonnull align 8 dereferenceable(32)) #1 align 2

declare void @stacktrace_create(ptr noundef, i64 noundef, i32 noundef, ptr noundef) #3

declare ptr @hash_alloc(i32 noundef) #3

declare void @stacktrace_add(ptr noundef, i64 noundef, i32 noundef) #3

declare ptr @ba_alloc_by_len(i64 noundef) #3

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

declare ptr @ba_get_buf(ptr noundef) #3

declare i32 @hash_update(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind
declare i32 @ferror(ptr noundef) #4

declare i32 @hash_final(ptr noundef, ptr noundef) #3

declare i32 @fclose(ptr noundef) #3

declare void @hash_free(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { mustprogress 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 = { 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 }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"_ZTS7HashAlg", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !15, i64 8}
!14 = !{!"_ZTS12ByteArray_st", !6, i64 0, !15, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7UapkiNS7SmartBAadEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7UapkiNS13ContentHasher10digestFileE7HashAlg(ptr noundef nonnull align 8 dereferenceable(80) %0, i32 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = tail call noundef ptr @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv(ptr noundef nonnull align 8 dereferenceable(32) %3) #5
  %5 = tail call ptr @fopen_utf8(ptr noundef %4, i32 noundef 0)
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %.thread44, label %6

.thread44:                                        ; preds = %2
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 185, i32 noundef 4128, ptr noundef null)
  br label %32

6:                                                ; preds = %2
  %7 = tail call ptr @hash_alloc(i32 noundef %1)
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %10

9:                                                ; preds = %6
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 188, i32 noundef 2)
  br label %.thread

10:                                               ; preds = %6
  %11 = tail call ptr @ba_alloc_by_len(i64 noundef 10485760)
  %12 = icmp eq ptr %11, null
  br i1 %12, label %14, label %.preheader

.preheader:                                       ; preds = %10
  %13 = getelementptr inbounds i8, ptr %11, i64 8
  br label %15

14:                                               ; preds = %10
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 190, i32 noundef 2)
  br label %.thread

15:                                               ; preds = %.preheader, %20
  %16 = tail call ptr @ba_get_buf(ptr noundef nonnull %11)
  %17 = tail call i64 @fread(ptr noundef %16, i64 noundef 1, i64 noundef 10485760, ptr noundef nonnull %5)
  store i64 %17, ptr %13, align 8, !tbaa !5
  %18 = tail call i32 @hash_update(ptr noundef nonnull %7, ptr noundef nonnull %11)
  %.not32 = icmp eq i32 %18, 0
  br i1 %.not32, label %20, label %19

19:                                               ; preds = %15
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 194, i32 noundef %18)
  br label %.thread

20:                                               ; preds = %15
  %21 = load i64, ptr %13, align 8, !tbaa !5
  %22 = icmp eq i64 %21, 10485760
  br i1 %22, label %15, label %23, !llvm.loop !11

23:                                               ; preds = %20
  %24 = tail call i32 @ferror(ptr noundef nonnull %5) #5
  %.not33 = icmp eq i32 %24, 0
  br i1 %.not33, label %26, label %25

25:                                               ; preds = %23
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 198, i32 noundef 4129, ptr noundef null)
  br label %.thread

26:                                               ; preds = %23
  %27 = getelementptr inbounds i8, ptr %0, i64 72
  %28 = tail call noundef ptr @_ZN7UapkiNS7SmartBAadEv(ptr noundef nonnull align 8 dereferenceable(8) %27)
  %29 = tail call i32 @hash_final(ptr noundef nonnull %7, ptr noundef %28)
  %.not34 = icmp eq i32 %29, 0
  br i1 %.not34, label %.thread, label %30

30:                                               ; preds = %26
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 201, i32 noundef %29)
  br label %.thread

.thread:                                          ; preds = %19, %25, %30, %26, %9, %14
  %.042 = phi ptr [ null, %14 ], [ null, %9 ], [ %11, %26 ], [ %11, %30 ], [ %11, %25 ], [ %11, %19 ]
  %.02438 = phi i32 [ 2, %14 ], [ 2, %9 ], [ 0, %26 ], [ %29, %30 ], [ 4129, %25 ], [ %18, %19 ]
  %31 = tail call i32 @fclose(ptr noundef nonnull %5)
  br label %32

32:                                               ; preds = %.thread44, %.thread
  %.043 = phi ptr [ %.042, %.thread ], [ null, %.thread44 ]
  %.02341 = phi ptr [ %7, %.thread ], [ null, %.thread44 ]
  %.02439 = phi i32 [ %.02438, %.thread ], [ 4128, %.thread44 ]
  tail call void @ba_free(ptr noundef %.043)
  tail call void @hash_free(ptr noundef %.02341)
  ret i32 %.02439
}

declare void @ba_free(ptr noundef) local_unnamed_addr #2

declare ptr @fopen_utf8(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare noundef ptr @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #0 align 2

declare void @stacktrace_create(ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

declare ptr @hash_alloc(i32 noundef) local_unnamed_addr #2

declare void @stacktrace_add(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #2

declare ptr @ba_alloc_by_len(i64 noundef) local_unnamed_addr #2

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

declare ptr @ba_get_buf(ptr noundef) local_unnamed_addr #2

declare i32 @hash_update(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind memory(read)
declare noundef i32 @ferror(ptr nocapture noundef) local_unnamed_addr #4

declare i32 @hash_final(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #3

declare void @hash_free(ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 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 = { 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 #4 = { nofree nounwind memory(read) "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 }

!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 8}
!6 = !{!"_ZTS12ByteArray_st", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: ConstraintEliminationPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass