; LLVM IR from specinfo-ua/UAPKI :: library/cm-pkcs12/src/storage (pkcs12-utils)

; 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.SafeContents = type { %struct.anon, %struct.asn_struct_ctx_s }
%struct.anon = type { ptr, i32, i32, ptr }
%struct.asn_struct_ctx_s = type { i16, i16, i32, ptr, i64 }

@.str = external hidden unnamed_addr constant [33 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

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

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

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

declare void @asn_free(ptr noundef, ptr noundef) #1

declare i32 @asn_encode_ba(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @pkcs12_write_safecontents(ptr noundef %0, i64 noundef %1, ptr noundef %2) #3 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  br label %111

17:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store ptr null, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store ptr null, ptr %10, align 8, !tbaa !5
  br label %18

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

21:                                               ; preds = %18
  store i32 2, ptr %8, align 4, !tbaa !11
  %22 = load i32, ptr %8, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 216, i32 noundef %22, ptr noundef null)
  br label %104

23:                                               ; preds = %18
  br label %24

24:                                               ; preds = %23
  br label %25

25:                                               ; preds = %24
  br label %26

26:                                               ; preds = %25
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %31, label %29

29:                                               ; preds = %26
  store i32 2, ptr %8, align 4, !tbaa !11
  %30 = load i32, ptr %8, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 217, i32 noundef %30, ptr noundef null)
  br label %104

31:                                               ; preds = %26
  br label %32

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %51, %33
  %35 = load i64, ptr %11, align 8, !tbaa !9
  %36 = load i64, ptr %6, align 8, !tbaa !9
  %37 = icmp ult i64 %35, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  store i32 7, ptr %12, align 4
  br label %54

39:                                               ; preds = %34
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load i64, ptr %11, align 8, !tbaa !9
  %43 = getelementptr inbounds ptr, ptr %41, i64 %42
  %44 = load ptr, ptr %43, align 8, !tbaa !5
  %45 = icmp ne ptr %44, null
  br i1 %45, label %48, label %46

46:                                               ; preds = %40
  store i32 2, ptr %8, align 4, !tbaa !11
  %47 = load i32, ptr %8, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 219, i32 noundef %47, ptr noundef null)
  store i32 4, ptr %12, align 4
  br label %54

48:                                               ; preds = %40
  br label %49

49:                                               ; preds = %48
  br label %50

50:                                               ; preds = %49
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %11, align 8, !tbaa !9
  %53 = add i64 %52, 1
  store i64 %53, ptr %11, align 8, !tbaa !9
  br label %34, !llvm.loop !13

54:                                               ; preds = %46, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  %55 = load i32, ptr %12, align 4
  switch i32 %55, label %110 [
    i32 7, label %56
    i32 4, label %104
  ]

56:                                               ; preds = %54
  %57 = call noalias ptr @calloc(i64 noundef 1, i64 noundef 48) #5
  store ptr %57, ptr %10, align 8, !tbaa !5
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = icmp eq ptr %58, null
  br i1 %59, label %60, label %62

60:                                               ; preds = %56
  store i32 1, ptr %8, align 4, !tbaa !11
  %61 = load i32, ptr %8, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 222, i32 noundef %61, ptr noundef null)
  br label %104

62:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %86, %62
  %64 = load i64, ptr %13, align 8, !tbaa !9
  %65 = load i64, ptr %6, align 8, !tbaa !9
  %66 = icmp ult i64 %64, %65
  br i1 %66, label %68, label %67

67:                                               ; preds = %63
  store i32 12, ptr %12, align 4
  br label %89

68:                                               ; preds = %63
  br label %69

69:                                               ; preds = %68
  %70 = call ptr @get_SafeBag_desc()
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = load i64, ptr %13, align 8, !tbaa !9
  %73 = getelementptr inbounds ptr, ptr %71, i64 %72
  %74 = load ptr, ptr %73, align 8, !tbaa !5
  %75 = call ptr @asn_decode_ba_with_alloc(ptr noundef %70, ptr noundef %74)
  store ptr %75, ptr %9, align 8, !tbaa !5
  %76 = icmp eq ptr null, %75
  br i1 %76, label %77, label %79

77:                                               ; preds = %69
  store i32 2, ptr %8, align 4, !tbaa !11
  %78 = load i32, ptr %8, align 4, !tbaa !11
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 224, i32 noundef %78)
  store i32 4, ptr %12, align 4
  br label %89

79:                                               ; preds = %69
  br label %80

80:                                               ; preds = %79
  br label %81

81:                                               ; preds = %80
  %82 = load ptr, ptr %10, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.SafeContents, ptr %82, i32 0, i32 0
  %84 = load ptr, ptr %9, align 8, !tbaa !5
  %85 = call i32 @asn_set_add(ptr noundef %83, ptr noundef %84)
  store ptr null, ptr %9, align 8, !tbaa !5
  br label %86

86:                                               ; preds = %81
  %87 = load i64, ptr %13, align 8, !tbaa !9
  %88 = add i64 %87, 1
  store i64 %88, ptr %13, align 8, !tbaa !9
  br label %63, !llvm.loop !15

89:                                               ; preds = %77, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  %90 = load i32, ptr %12, align 4
  switch i32 %90, label %110 [
    i32 12, label %91
    i32 4, label %104
  ]

91:                                               ; preds = %89
  br label %92

92:                                               ; preds = %91
  %93 = call ptr @get_SafeContents_desc()
  %94 = load ptr, ptr %10, align 8, !tbaa !5
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  %96 = call i32 @asn_encode_ba(ptr noundef %93, ptr noundef %94, ptr noundef %95)
  store i32 %96, ptr %8, align 4, !tbaa !11
  %97 = load i32, ptr %8, align 4, !tbaa !11
  %98 = icmp ne i32 %97, 0
  br i1 %98, label %99, label %101

99:                                               ; preds = %92
  %100 = load i32, ptr %8, align 4, !tbaa !11
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 229, i32 noundef %100)
  br label %104

101:                                              ; preds = %92
  br label %102

102:                                              ; preds = %101
  br label %103

103:                                              ; preds = %102
  br label %104

104:                                              ; preds = %103, %99, %89, %60, %54, %29, %21
  %105 = call ptr @get_SafeBag_desc()
  %106 = load ptr, ptr %9, align 8, !tbaa !5
  call void @asn_free(ptr noundef %105, ptr noundef %106)
  %107 = call ptr @get_SafeContents_desc()
  %108 = load ptr, ptr %10, align 8, !tbaa !5
  call void @asn_free(ptr noundef %107, ptr noundef %108)
  %109 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %109, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %110

110:                                              ; preds = %104, %89, %54
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %111

111:                                              ; preds = %110, %16
  %112 = load i32, ptr %4, align 4
  ret i32 %112
}

declare ptr @asn_decode_ba_with_alloc(ptr noundef, ptr noundef) #1

declare ptr @get_SafeBag_desc() #1

declare i32 @asn_set_add(ptr noundef, ptr noundef) #1

declare ptr @get_SafeContents_desc() #1

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 allocsize(0,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 #3 = { 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 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }

!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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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 [33 x i8], align 1

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

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

declare void @asn_free(ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @asn_encode_ba(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @pkcs12_write_safecontents(ptr noundef readonly %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #2 {
  %4 = icmp eq i64 %1, 0
  br i1 %4, label %34, label %5

5:                                                ; preds = %3
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %6, label %7

6:                                                ; preds = %5
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 216, i32 noundef 2, ptr noundef null) #3
  br label %31

7:                                                ; preds = %5
  %.not49 = icmp eq ptr %2, null
  br i1 %.not49, label %8, label %.preheader61

8:                                                ; preds = %7
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 217, i32 noundef 2, ptr noundef null) #3
  br label %31

9:                                                ; preds = %.preheader61
  %10 = add nuw i64 %.03162, 1
  %exitcond.not = icmp eq i64 %10, %1
  br i1 %exitcond.not, label %14, label %.preheader61, !llvm.loop !5

.preheader61:                                     ; preds = %7, %9
  %.03162 = phi i64 [ %10, %9 ], [ 0, %7 ]
  %11 = getelementptr inbounds ptr, ptr %0, i64 %.03162
  %12 = load ptr, ptr %11, align 8, !tbaa !7
  %.not50 = icmp eq ptr %12, null
  br i1 %.not50, label %13, label %9

13:                                               ; preds = %.preheader61
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 219, i32 noundef 2, ptr noundef null) #3
  br label %31

14:                                               ; preds = %9
  %15 = tail call noalias dereferenceable_or_null(48) ptr @calloc(i64 noundef 1, i64 noundef 48) #4
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %.preheader

17:                                               ; preds = %14
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 222, i32 noundef 1, ptr noundef null) #3
  br label %31

.preheader:                                       ; preds = %14, %23
  %.063 = phi i64 [ %25, %23 ], [ 0, %14 ]
  %18 = tail call ptr @get_SafeBag_desc() #3
  %19 = getelementptr inbounds ptr, ptr %0, i64 %.063
  %20 = load ptr, ptr %19, align 8, !tbaa !7
  %21 = tail call ptr @asn_decode_ba_with_alloc(ptr noundef %18, ptr noundef %20) #3
  %22 = icmp eq ptr %21, null
  br i1 %22, label %26, label %23

23:                                               ; preds = %.preheader
  %24 = tail call i32 @asn_set_add(ptr noundef nonnull %15, ptr noundef nonnull %21) #3
  %25 = add nuw i64 %.063, 1
  %exitcond64.not = icmp eq i64 %25, %1
  br i1 %exitcond64.not, label %27, label %.preheader, !llvm.loop !11

26:                                               ; preds = %.preheader
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 224, i32 noundef 2) #3
  br label %31

27:                                               ; preds = %23
  %28 = tail call ptr @get_SafeContents_desc() #3
  %29 = tail call i32 @asn_encode_ba(ptr noundef %28, ptr noundef nonnull %15, ptr noundef nonnull %2) #3
  %.not51 = icmp eq i32 %29, 0
  br i1 %.not51, label %31, label %30

30:                                               ; preds = %27
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 229, i32 noundef %29) #3
  br label %31

31:                                               ; preds = %26, %13, %27, %30, %17, %8, %6
  %.237 = phi i32 [ 2, %13 ], [ 1, %17 ], [ 2, %26 ], [ %29, %30 ], [ 0, %27 ], [ 2, %8 ], [ 2, %6 ]
  %.032 = phi ptr [ null, %13 ], [ null, %17 ], [ %15, %26 ], [ %15, %30 ], [ %15, %27 ], [ null, %8 ], [ null, %6 ]
  %32 = tail call ptr @get_SafeBag_desc() #3
  tail call void @asn_free(ptr noundef %32, ptr noundef null) #3
  %33 = tail call ptr @get_SafeContents_desc() #3
  tail call void @asn_free(ptr noundef %33, ptr noundef %.032) #3
  br label %34

34:                                               ; preds = %31, %3
  %.139 = phi i32 [ 0, %3 ], [ %.237, %31 ]
  ret i32 %.139
}

declare ptr @asn_decode_ba_with_alloc(ptr noundef, ptr noundef) local_unnamed_addr #0

declare ptr @get_SafeBag_desc() local_unnamed_addr #0

declare i32 @asn_set_add(ptr noundef, ptr noundef) local_unnamed_addr #0

declare ptr @get_SafeContents_desc() local_unnamed_addr #0

attributes #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 nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 }
attributes #4 = { nounwind allocsize(0,1) }

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


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