; LLVM IR from machinefi/psa-crypto-esp32 :: psa_layer (psa_its_nvs)

; 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.psa_its_file_header_t = type { [8 x i8], [4 x i8], [4 x i8] }
%struct.psa_storage_info_t = type { i32, i32 }
%struct._nvs_drv = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@its_nvs = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 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: nounwind uwtable
declare hidden void @psa_its_fill_filename(i64 noundef, ptr noundef) #1

; 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
define dso_local i32 @psa_its_get(i64 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %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
  %13 = alloca i32, align 4
  %14 = alloca %struct.psa_its_file_header_t, align 1
  %15 = alloca i64, align 8
  %16 = alloca %struct.psa_storage_info_t, align 4
  %17 = alloca [13 x i8], align 1
  %18 = alloca i32, align 4
  store i64 %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i32 %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !11
  store ptr %4, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %14) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  store i64 16, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #2
  call void @llvm.lifetime.start.p0(i64 13, ptr %17) #2
  %19 = load i64, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds [13 x i8], ptr %17, i64 0, i64 0
  call void @psa_its_fill_filename(i64 noundef %19, ptr noundef %20)
  %21 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %22 = getelementptr inbounds %struct._nvs_drv, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = getelementptr inbounds [13 x i8], ptr %17, i64 0, i64 0
  %25 = call i32 %23(ptr noundef %24, i32 noundef 1, ptr noundef %13)
  %26 = load i32, ptr %13, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %5
  store i32 -140, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %134

29:                                               ; preds = %5
  %30 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %31 = getelementptr inbounds %struct._nvs_drv, ptr %30, i32 0, i32 3
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = load i32, ptr %13, align 4, !tbaa !9
  %34 = call i32 %32(i32 noundef %33, ptr noundef @.str, ptr noundef %14, ptr noundef %15)
  store i32 %34, ptr %12, align 4, !tbaa !9
  %35 = load i64, ptr %15, align 8, !tbaa !5
  %36 = icmp ne i64 %35, 16
  br i1 %36, label %37, label %38

37:                                               ; preds = %29
  store i32 -152, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %134

38:                                               ; preds = %29
  %39 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 1
  %40 = getelementptr inbounds [4 x i8], ptr %39, i64 0, i64 0
  %41 = load i8, ptr %40, align 1, !tbaa !16
  %42 = zext i8 %41 to i32
  %43 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 1
  %44 = getelementptr inbounds [4 x i8], ptr %43, i64 0, i64 1
  %45 = load i8, ptr %44, align 1, !tbaa !16
  %46 = zext i8 %45 to i32
  %47 = shl i32 %46, 8
  %48 = or i32 %42, %47
  %49 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 1
  %50 = getelementptr inbounds [4 x i8], ptr %49, i64 0, i64 2
  %51 = load i8, ptr %50, align 1, !tbaa !16
  %52 = zext i8 %51 to i32
  %53 = shl i32 %52, 16
  %54 = or i32 %48, %53
  %55 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 1
  %56 = getelementptr inbounds [4 x i8], ptr %55, i64 0, i64 3
  %57 = load i8, ptr %56, align 1, !tbaa !16
  %58 = zext i8 %57 to i32
  %59 = shl i32 %58, 24
  %60 = or i32 %54, %59
  %61 = getelementptr inbounds %struct.psa_storage_info_t, ptr %16, i32 0, i32 0
  store i32 %60, ptr %61, align 4, !tbaa !17
  %62 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 2
  %63 = getelementptr inbounds [4 x i8], ptr %62, i64 0, i64 0
  %64 = load i8, ptr %63, align 1, !tbaa !16
  %65 = zext i8 %64 to i32
  %66 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 2
  %67 = getelementptr inbounds [4 x i8], ptr %66, i64 0, i64 1
  %68 = load i8, ptr %67, align 1, !tbaa !16
  %69 = zext i8 %68 to i32
  %70 = shl i32 %69, 8
  %71 = or i32 %65, %70
  %72 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 2
  %73 = getelementptr inbounds [4 x i8], ptr %72, i64 0, i64 2
  %74 = load i8, ptr %73, align 1, !tbaa !16
  %75 = zext i8 %74 to i32
  %76 = shl i32 %75, 16
  %77 = or i32 %71, %76
  %78 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %14, i32 0, i32 2
  %79 = getelementptr inbounds [4 x i8], ptr %78, i64 0, i64 3
  %80 = load i8, ptr %79, align 1, !tbaa !16
  %81 = zext i8 %80 to i32
  %82 = shl i32 %81, 24
  %83 = or i32 %77, %82
  %84 = getelementptr inbounds %struct.psa_storage_info_t, ptr %16, i32 0, i32 1
  store i32 %83, ptr %84, align 4, !tbaa !19
  %85 = load i32, ptr %12, align 4, !tbaa !9
  %86 = icmp ne i32 %85, 0
  br i1 %86, label %87, label %88

87:                                               ; preds = %38
  br label %124

88:                                               ; preds = %38
  store i32 -135, ptr %12, align 4, !tbaa !9
  %89 = load i32, ptr %8, align 4, !tbaa !9
  %90 = load i32, ptr %9, align 4, !tbaa !9
  %91 = add i32 %89, %90
  %92 = load i32, ptr %8, align 4, !tbaa !9
  %93 = icmp ult i32 %91, %92
  br i1 %93, label %94, label %95

94:                                               ; preds = %88
  br label %124

95:                                               ; preds = %88
  %96 = load i32, ptr %8, align 4, !tbaa !9
  %97 = load i32, ptr %9, align 4, !tbaa !9
  %98 = add i32 %96, %97
  %99 = getelementptr inbounds %struct.psa_storage_info_t, ptr %16, i32 0, i32 0
  %100 = load i32, ptr %99, align 4, !tbaa !17
  %101 = icmp ugt i32 %98, %100
  br i1 %101, label %102, label %103

102:                                              ; preds = %95
  br label %124

103:                                              ; preds = %95
  store i32 -146, ptr %12, align 4, !tbaa !9
  %104 = load i32, ptr %9, align 4, !tbaa !9
  %105 = zext i32 %104 to i64
  store i64 %105, ptr %15, align 8, !tbaa !5
  %106 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %107 = getelementptr inbounds %struct._nvs_drv, ptr %106, i32 0, i32 3
  %108 = load ptr, ptr %107, align 8, !tbaa !15
  %109 = load i32, ptr %13, align 4, !tbaa !9
  %110 = load ptr, ptr %10, align 8, !tbaa !11
  %111 = call i32 %108(i32 noundef %109, ptr noundef @.str.2, ptr noundef %110, ptr noundef %15)
  store i32 %111, ptr %12, align 4, !tbaa !9
  %112 = load i64, ptr %15, align 8, !tbaa !5
  %113 = load i32, ptr %9, align 4, !tbaa !9
  %114 = zext i32 %113 to i64
  %115 = icmp ne i64 %112, %114
  br i1 %115, label %116, label %117

116:                                              ; preds = %103
  br label %124

117:                                              ; preds = %103
  store i32 0, ptr %12, align 4, !tbaa !9
  %118 = load ptr, ptr %11, align 8, !tbaa !11
  %119 = icmp ne ptr %118, null
  br i1 %119, label %120, label %123

120:                                              ; preds = %117
  %121 = load i64, ptr %15, align 8, !tbaa !5
  %122 = load ptr, ptr %11, align 8, !tbaa !11
  store i64 %121, ptr %122, align 8, !tbaa !5
  br label %123

123:                                              ; preds = %120, %117
  br label %124

124:                                              ; preds = %123, %116, %102, %94, %87
  %125 = load i32, ptr %13, align 4, !tbaa !9
  %126 = icmp ne i32 %125, 0
  br i1 %126, label %127, label %132

127:                                              ; preds = %124
  %128 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %129 = getelementptr inbounds %struct._nvs_drv, ptr %128, i32 0, i32 1
  %130 = load ptr, ptr %129, align 8, !tbaa !20
  %131 = load i32, ptr %13, align 4, !tbaa !9
  call void %130(i32 noundef %131)
  br label %132

132:                                              ; preds = %127, %124
  %133 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %133, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %134

134:                                              ; preds = %132, %37, %28
  call void @llvm.lifetime.end.p0(i64 13, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  %135 = load i32, ptr %6, align 4
  ret i32 %135
}

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 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"_nvs_drv", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48}
!15 = !{!14, !12, i64 24}
!16 = !{!7, !7, i64 0}
!17 = !{!18, !10, i64 0}
!18 = !{!"psa_storage_info_t", !10, i64 0, !10, i64 4}
!19 = !{!18, !10, i64 4}
!20 = !{!14, !12, i64 8}


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

%struct.psa_its_file_header_t = type { [8 x i8], [4 x i8], [4 x i8] }

@its_nvs = external dso_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare hidden void @psa_its_fill_filename(i64 noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_its_get(i64 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef writeonly %4) local_unnamed_addr #1 {
  %6 = alloca i32, align 4
  %7 = alloca %struct.psa_its_file_header_t, align 1
  %8 = alloca i64, align 8
  %9 = alloca [13 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #2
  store i64 16, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 13, ptr nonnull %9) #2
  call void @psa_its_fill_filename(i64 noundef %0, ptr noundef nonnull %9)
  %10 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = call i32 %11(ptr noundef nonnull %9, i32 noundef 1, ptr noundef nonnull %6) #2
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %44, label %15

15:                                               ; preds = %5
  %16 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %16, i64 24
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  %19 = call i32 %18(i32 noundef %13, ptr noundef nonnull @.str, ptr noundef nonnull %7, ptr noundef nonnull %8) #2
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %.not = icmp eq i64 %20, 16
  br i1 %.not, label %21, label %44

21:                                               ; preds = %15
  %.not20 = icmp eq i32 %19, 0
  br i1 %.not20, label %22, label %38

22:                                               ; preds = %21
  %23 = getelementptr inbounds i8, ptr %7, i64 8
  %24 = load i32, ptr %23, align 1
  %25 = add i32 %2, %1
  %26 = icmp ult i32 %25, %1
  %27 = icmp ugt i32 %25, %24
  %or.cond = select i1 %26, i1 true, i1 %27
  br i1 %or.cond, label %38, label %28

28:                                               ; preds = %22
  %29 = zext i32 %2 to i64
  store i64 %29, ptr %8, align 8, !tbaa !9
  %30 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %31 = getelementptr inbounds i8, ptr %30, i64 24
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = call i32 %32(i32 noundef %33, ptr noundef nonnull @.str.2, ptr noundef %3, ptr noundef nonnull %8) #2
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %.not21 = icmp eq i64 %35, %29
  br i1 %.not21, label %36, label %38

36:                                               ; preds = %28
  %.not22 = icmp eq ptr %4, null
  br i1 %.not22, label %38, label %37

37:                                               ; preds = %36
  store i64 %29, ptr %4, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %36, %37, %28, %22, %21
  %.013 = phi i32 [ %19, %21 ], [ -135, %22 ], [ %34, %28 ], [ 0, %37 ], [ 0, %36 ]
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %.not23 = icmp eq i32 %39, 0
  br i1 %.not23, label %44, label %40

40:                                               ; preds = %38
  %41 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %42 = getelementptr inbounds i8, ptr %41, i64 8
  %43 = load ptr, ptr %42, align 8, !tbaa !16
  call void %43(i32 noundef %39) #2
  br label %44

44:                                               ; preds = %38, %40, %15, %5
  %.0 = phi i32 [ -140, %5 ], [ -152, %15 ], [ %.013, %40 ], [ %.013, %38 ]
  call void @llvm.lifetime.end.p0(i64 13, ptr nonnull %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #2
  ret i32 %.0
}

attributes #0 = { mustprogress 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 = { 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 = !{!"int", !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 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"_nvs_drv", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48}
!15 = !{!14, !12, i64 24}
!16 = !{!14, !12, i64 8}


; Effective O3 passes: AggressiveInstCombinePass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass