; LLVM IR from specinfo-ua/UAPKI :: library/common/pkix (extension-helper-json)

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

@.str = external hidden unnamed_addr constant [38 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [13 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 ptr @asn_decode_ba_with_alloc(ptr noundef, ptr noundef) #1

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

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

declare ptr @json_value_init_array() #1

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

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

declare ptr @json_value_init_object() #1

declare ptr @json_array_get_object(ptr noundef, i64 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7UapkiNS15ExtensionHelper18DecodeToJsonObject15alternativeNameEPK12ByteArray_stP13json_object_t(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr null, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr null, ptr %8, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %2
  %12 = call ptr @get_GeneralNames_desc()
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = call ptr @asn_decode_ba_with_alloc(ptr noundef %12, ptr noundef %13)
  store ptr %14, ptr %7, align 8, !tbaa !5
  %15 = icmp eq ptr null, %14
  br i1 %15, label %16, label %18

16:                                               ; preds = %11
  store i32 2, ptr %6, align 4, !tbaa !9
  %17 = load i32, ptr %6, align 4, !tbaa !9
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 228, i32 noundef %17)
  br label %71

18:                                               ; preds = %11
  br label %19

19:                                               ; preds = %18
  br label %20

20:                                               ; preds = %19
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = call ptr @json_value_init_array()
  %23 = call i32 @json_object_set_value(ptr noundef %21, ptr noundef @.str.7, ptr noundef %22)
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %20
  store i32 4113, ptr %6, align 4, !tbaa !9
  br label %71

26:                                               ; preds = %20
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call ptr @json_object_get_array(ptr noundef %27, ptr noundef @.str.7)
  store ptr %28, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %65, %26
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.GeneralNames, ptr %31, i32 0, i32 0
  %33 = getelementptr inbounds %struct.anon.1, ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 8, !tbaa !11
  %35 = icmp slt i32 %30, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %29
  store i32 5, ptr %10, align 4
  br label %68

37:                                               ; preds = %29
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = call ptr @json_value_init_object()
  %40 = call i32 @json_array_append_value(ptr noundef %38, ptr noundef %39)
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %43

42:                                               ; preds = %37
  store i32 4113, ptr %6, align 4, !tbaa !9
  store i32 4, ptr %10, align 4
  br label %68

43:                                               ; preds = %37
  br label %44

44:                                               ; preds = %43
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.GeneralNames, ptr %45, i32 0, i32 0
  %47 = getelementptr inbounds %struct.anon.1, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !17
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds ptr, ptr %48, i64 %50
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = load i32, ptr %9, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = call ptr @json_array_get_object(ptr noundef %53, i64 noundef %55)
  %57 = call noundef i32 @_ZN7UapkiNSL27decode_general_name_to_jsonEPK11GeneralNameP13json_object_t(ptr noundef %52, ptr noundef %56)
  store i32 %57, ptr %6, align 4, !tbaa !9
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %62

60:                                               ; preds = %44
  %61 = load i32, ptr %6, align 4, !tbaa !9
  call void @stacktrace_add(ptr noundef @.str, i64 noundef 235, i32 noundef %61)
  store i32 4, ptr %10, align 4
  br label %68

62:                                               ; preds = %44
  br label %63

63:                                               ; preds = %62
  br label %64

64:                                               ; preds = %63
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %9, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %9, align 4, !tbaa !9
  br label %29, !llvm.loop !18

68:                                               ; preds = %60, %42, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %69 = load i32, ptr %10, align 4
  switch i32 %69, label %75 [
    i32 5, label %70
    i32 4, label %71
  ]

70:                                               ; preds = %68
  br label %71

71:                                               ; preds = %70, %68, %25, %16
  %72 = call ptr @get_GeneralNames_desc()
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  call void @asn_free(ptr noundef %72, ptr noundef %73)
  %74 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %74, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %75

75:                                               ; preds = %71, %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %76 = load i32, ptr %3, align 4
  ret i32 %76
}

declare ptr @get_GeneralNames_desc() #1

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

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 = { 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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTS12GeneralNames", !13, i64 0, !14, i64 24}
!13 = !{!"_ZTSN12GeneralNamesUt_E", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16}
!14 = !{!"_ZTS16asn_struct_ctx_s", !15, i64 0, !15, i64 2, !10, i64 4, !6, i64 8, !16, i64 16}
!15 = !{!"short", !7, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!12, !6, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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 [38 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [13 x i8], align 1

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

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

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

declare ptr @json_value_init_array() local_unnamed_addr #0

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

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

declare ptr @json_value_init_object() local_unnamed_addr #0

declare ptr @json_array_get_object(ptr noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7UapkiNS15ExtensionHelper18DecodeToJsonObject15alternativeNameEPK12ByteArray_stP13json_object_t(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = tail call ptr @get_GeneralNames_desc()
  %4 = tail call ptr @asn_decode_ba_with_alloc(ptr noundef %3, ptr noundef %0)
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %7

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

7:                                                ; preds = %2
  %8 = tail call ptr @json_value_init_array()
  %9 = tail call i32 @json_object_set_value(ptr noundef %1, ptr noundef nonnull @.str.7, ptr noundef %8)
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %10, label %.loopexit

10:                                               ; preds = %7
  %11 = tail call ptr @json_object_get_array(ptr noundef %1, ptr noundef nonnull @.str.7)
  %12 = getelementptr inbounds i8, ptr %4, i64 8
  %13 = load i32, ptr %12, align 8, !tbaa !5
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph, label %.loopexit

15:                                               ; preds = %21
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = load i32, ptr %12, align 8, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = icmp slt i64 %indvars.iv.next, %17
  br i1 %18, label %.lr.ph, label %.loopexit, !llvm.loop !15

.lr.ph:                                           ; preds = %10, %15
  %indvars.iv = phi i64 [ %indvars.iv.next, %15 ], [ 0, %10 ]
  %19 = tail call ptr @json_value_init_object()
  %20 = tail call i32 @json_array_append_value(ptr noundef %11, ptr noundef %19)
  %.not23 = icmp eq i32 %20, 0
  br i1 %.not23, label %21, label %.loopexit

21:                                               ; preds = %.lr.ph
  %22 = load ptr, ptr %4, align 8, !tbaa !17
  %23 = getelementptr inbounds ptr, ptr %22, i64 %indvars.iv
  %24 = load ptr, ptr %23, align 8, !tbaa !18
  %25 = tail call ptr @json_array_get_object(ptr noundef %11, i64 noundef %indvars.iv)
  %26 = tail call noundef i32 @_ZN7UapkiNSL27decode_general_name_to_jsonEPK11GeneralNameP13json_object_t(ptr noundef %24, ptr noundef %25)
  %.not24 = icmp eq i32 %26, 0
  br i1 %.not24, label %15, label %27

27:                                               ; preds = %21
  tail call void @stacktrace_add(ptr noundef nonnull @.str, i64 noundef 235, i32 noundef %26)
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %15, %10, %27, %7, %6
  %.2 = phi i32 [ 2, %6 ], [ 4113, %7 ], [ %26, %27 ], [ 0, %10 ], [ 4113, %.lr.ph ], [ 0, %15 ]
  %28 = tail call ptr @get_GeneralNames_desc()
  tail call void @asn_free(ptr noundef %28, ptr noundef %4)
  ret i32 %.2
}

declare ptr @get_GeneralNames_desc() local_unnamed_addr #0

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

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

!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, !11, i64 8}
!6 = !{!"_ZTS12GeneralNames", !7, i64 0, !12, i64 24}
!7 = !{!"_ZTSN12GeneralNamesUt_E", !8, i64 0, !11, i64 8, !11, i64 12, !8, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"_ZTS16asn_struct_ctx_s", !13, i64 0, !13, i64 2, !11, i64 4, !8, i64 8, !14, i64 16}
!13 = !{!"short", !9, i64 0}
!14 = !{!"long", !9, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!6, !8, i64 0}
!18 = !{!8, !8, i64 0}


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