; 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._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.1 = external hidden unnamed_addr constant [9 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_set(i64 noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca [13 x i8], align 1
  %12 = alloca i32, align 4
  %13 = alloca %struct.psa_its_file_header_t, align 1
  %14 = alloca i64, align 8
  store i64 %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !11
  store i32 %3, ptr %9, align 4, !tbaa !9
  %15 = load i64, ptr %6, align 8, !tbaa !5
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %4
  store i32 -136, ptr %5, align 4
  br label %127

18:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 -146, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 13, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %19 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 0
  %20 = getelementptr inbounds [8 x i8], ptr %19, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %20, ptr align 1 @.str.1, i64 8, i1 false)
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = and i32 %21, 255
  %23 = trunc i32 %22 to i8
  %24 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 1
  %25 = getelementptr inbounds [4 x i8], ptr %24, i64 0, i64 0
  store i8 %23, ptr %25, align 1, !tbaa !13
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = lshr i32 %26, 8
  %28 = and i32 %27, 255
  %29 = trunc i32 %28 to i8
  %30 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 1
  %31 = getelementptr inbounds [4 x i8], ptr %30, i64 0, i64 1
  store i8 %29, ptr %31, align 1, !tbaa !13
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = lshr i32 %32, 16
  %34 = and i32 %33, 255
  %35 = trunc i32 %34 to i8
  %36 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 1
  %37 = getelementptr inbounds [4 x i8], ptr %36, i64 0, i64 2
  store i8 %35, ptr %37, align 1, !tbaa !13
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = lshr i32 %38, 24
  %40 = and i32 %39, 255
  %41 = trunc i32 %40 to i8
  %42 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 1
  %43 = getelementptr inbounds [4 x i8], ptr %42, i64 0, i64 3
  store i8 %41, ptr %43, align 1, !tbaa !13
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = and i32 %44, 255
  %46 = trunc i32 %45 to i8
  %47 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 2
  %48 = getelementptr inbounds [4 x i8], ptr %47, i64 0, i64 0
  store i8 %46, ptr %48, align 1, !tbaa !13
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = lshr i32 %49, 8
  %51 = and i32 %50, 255
  %52 = trunc i32 %51 to i8
  %53 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 2
  %54 = getelementptr inbounds [4 x i8], ptr %53, i64 0, i64 1
  store i8 %52, ptr %54, align 1, !tbaa !13
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = lshr i32 %55, 16
  %57 = and i32 %56, 255
  %58 = trunc i32 %57 to i8
  %59 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 2
  %60 = getelementptr inbounds [4 x i8], ptr %59, i64 0, i64 2
  store i8 %58, ptr %60, align 1, !tbaa !13
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = lshr i32 %61, 24
  %63 = and i32 %62, 255
  %64 = trunc i32 %63 to i8
  %65 = getelementptr inbounds %struct.psa_its_file_header_t, ptr %13, i32 0, i32 2
  %66 = getelementptr inbounds [4 x i8], ptr %65, i64 0, i64 3
  store i8 %64, ptr %66, align 1, !tbaa !13
  %67 = load i64, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds [13 x i8], ptr %11, i64 0, i64 0
  call void @psa_its_fill_filename(i64 noundef %67, ptr noundef %68)
  %69 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %70 = getelementptr inbounds %struct._nvs_drv, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !14
  %72 = getelementptr inbounds [13 x i8], ptr %11, i64 0, i64 0
  %73 = call i32 %71(ptr noundef %72, i32 noundef 1, ptr noundef %12)
  %74 = load i32, ptr %12, align 4, !tbaa !9
  %75 = icmp eq i32 %74, 0
  br i1 %75, label %76, label %77

76:                                               ; preds = %18
  br label %117

77:                                               ; preds = %18
  store i32 -142, ptr %10, align 4, !tbaa !9
  %78 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %79 = getelementptr inbounds %struct._nvs_drv, ptr %78, i32 0, i32 2
  %80 = load ptr, ptr %79, align 8, !tbaa !16
  %81 = load i32, ptr %12, align 4, !tbaa !9
  %82 = call i32 %80(i32 noundef %81, ptr noundef @.str, ptr noundef %13, i64 noundef 16)
  %83 = sext i32 %82 to i64
  store i64 %83, ptr %14, align 8, !tbaa !5
  %84 = load i64, ptr %14, align 8, !tbaa !5
  %85 = icmp ne i64 %84, 16
  br i1 %85, label %86, label %87

86:                                               ; preds = %77
  br label %117

87:                                               ; preds = %77
  %88 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %89 = getelementptr inbounds %struct._nvs_drv, ptr %88, i32 0, i32 6
  %90 = load ptr, ptr %89, align 8, !tbaa !17
  %91 = load i32, ptr %12, align 4, !tbaa !9
  %92 = call i32 %90(i32 noundef %91)
  %93 = load i32, ptr %7, align 4, !tbaa !9
  %94 = icmp ne i32 %93, 0
  br i1 %94, label %95, label %111

95:                                               ; preds = %87
  %96 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %97 = getelementptr inbounds %struct._nvs_drv, ptr %96, i32 0, i32 2
  %98 = load ptr, ptr %97, align 8, !tbaa !16
  %99 = load i32, ptr %12, align 4, !tbaa !9
  %100 = load ptr, ptr %8, align 8, !tbaa !11
  %101 = load i32, ptr %7, align 4, !tbaa !9
  %102 = zext i32 %101 to i64
  %103 = call i32 %98(i32 noundef %99, ptr noundef @.str.2, ptr noundef %100, i64 noundef %102)
  %104 = sext i32 %103 to i64
  store i64 %104, ptr %14, align 8, !tbaa !5
  %105 = load i64, ptr %14, align 8, !tbaa !5
  %106 = load i32, ptr %7, align 4, !tbaa !9
  %107 = zext i32 %106 to i64
  %108 = icmp ne i64 %105, %107
  br i1 %108, label %109, label %110

109:                                              ; preds = %95
  br label %117

110:                                              ; preds = %95
  br label %111

111:                                              ; preds = %110, %87
  %112 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %113 = getelementptr inbounds %struct._nvs_drv, ptr %112, i32 0, i32 6
  %114 = load ptr, ptr %113, align 8, !tbaa !17
  %115 = load i32, ptr %12, align 4, !tbaa !9
  %116 = call i32 %114(i32 noundef %115)
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %117

117:                                              ; preds = %111, %109, %86, %76
  %118 = load i32, ptr %12, align 4, !tbaa !9
  %119 = icmp ne i32 %118, 0
  br i1 %119, label %120, label %125

120:                                              ; preds = %117
  %121 = load ptr, ptr @its_nvs, align 8, !tbaa !11
  %122 = getelementptr inbounds %struct._nvs_drv, ptr %121, i32 0, i32 1
  %123 = load ptr, ptr %122, align 8, !tbaa !18
  %124 = load i32, ptr %12, align 4, !tbaa !9
  call void %123(i32 noundef %124)
  br label %125

125:                                              ; preds = %120, %117
  %126 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %126, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 13, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %127

127:                                              ; preds = %125, %17
  %128 = load i32, ptr %5, align 4
  ret i32 %128
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 = !{!7, !7, i64 0}
!14 = !{!15, !12, i64 0}
!15 = !{!"_nvs_drv", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48}
!16 = !{!15, !12, i64 16}
!17 = !{!15, !12, i64 48}
!18 = !{!15, !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.1 = external hidden unnamed_addr constant [9 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 range(i32 -146, 1) i32 @psa_its_set(i64 noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = alloca [13 x i8], align 1
  %6 = alloca i32, align 4
  %7 = alloca %struct.psa_its_file_header_t, align 8
  %8 = icmp eq i64 %0, 0
  br i1 %8, label %68, label %9

9:                                                ; preds = %4
  call void @llvm.lifetime.start.p0(i64 13, ptr nonnull %5) #2
  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
  %10 = load i64, ptr @.str.1, align 1
  store i64 %10, ptr %7, align 8
  %11 = trunc i32 %1 to i8
  %12 = getelementptr inbounds i8, ptr %7, i64 8
  store i8 %11, ptr %12, align 8, !tbaa !9
  %13 = lshr i32 %1, 8
  %14 = trunc i32 %13 to i8
  %15 = getelementptr inbounds i8, ptr %7, i64 9
  store i8 %14, ptr %15, align 1, !tbaa !9
  %16 = lshr i32 %1, 16
  %17 = trunc i32 %16 to i8
  %18 = getelementptr inbounds i8, ptr %7, i64 10
  store i8 %17, ptr %18, align 2, !tbaa !9
  %19 = lshr i32 %1, 24
  %20 = trunc nuw i32 %19 to i8
  %21 = getelementptr inbounds i8, ptr %7, i64 11
  store i8 %20, ptr %21, align 1, !tbaa !9
  %22 = trunc i32 %3 to i8
  %23 = getelementptr inbounds i8, ptr %7, i64 12
  store i8 %22, ptr %23, align 4, !tbaa !9
  %24 = lshr i32 %3, 8
  %25 = trunc i32 %24 to i8
  %26 = getelementptr inbounds i8, ptr %7, i64 13
  store i8 %25, ptr %26, align 1, !tbaa !9
  %27 = lshr i32 %3, 16
  %28 = trunc i32 %27 to i8
  %29 = getelementptr inbounds i8, ptr %7, i64 14
  store i8 %28, ptr %29, align 2, !tbaa !9
  %30 = lshr i32 %3, 24
  %31 = trunc nuw i32 %30 to i8
  %32 = getelementptr inbounds i8, ptr %7, i64 15
  store i8 %31, ptr %32, align 1, !tbaa !9
  call void @psa_its_fill_filename(i64 noundef %0, ptr noundef nonnull %5)
  %33 = load ptr, ptr @its_nvs, align 8, !tbaa !10
  %34 = load ptr, ptr %33, align 8, !tbaa !12
  %35 = call i32 %34(ptr noundef nonnull %5, i32 noundef 1, ptr noundef nonnull %6) #2
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %.thread, label %38

38:                                               ; preds = %9
  %39 = load ptr, ptr @its_nvs, align 8, !tbaa !10
  %40 = getelementptr inbounds i8, ptr %39, i64 16
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = call i32 %41(i32 noundef %36, ptr noundef nonnull @.str, ptr noundef nonnull %7, i64 noundef 16) #2
  %.not = icmp eq i32 %42, 16
  br i1 %.not, label %43, label %63

43:                                               ; preds = %38
  %44 = load ptr, ptr @its_nvs, align 8, !tbaa !10
  %45 = getelementptr inbounds i8, ptr %44, i64 48
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = call i32 %46(i32 noundef %47) #2
  %.not22 = icmp eq i32 %1, 0
  br i1 %.not22, label %57, label %49

49:                                               ; preds = %43
  %50 = load ptr, ptr @its_nvs, align 8, !tbaa !10
  %51 = getelementptr inbounds i8, ptr %50, i64 16
  %52 = load ptr, ptr %51, align 8, !tbaa !14
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = zext i32 %1 to i64
  %55 = call i32 %52(i32 noundef %53, ptr noundef nonnull @.str.2, ptr noundef %2, i64 noundef %54) #2
  %56 = sext i32 %55 to i64
  %.not23 = icmp eq i64 %56, %54
  br i1 %.not23, label %57, label %63

57:                                               ; preds = %49, %43
  %58 = load ptr, ptr @its_nvs, align 8, !tbaa !10
  %59 = getelementptr inbounds i8, ptr %58, i64 48
  %60 = load ptr, ptr %59, align 8, !tbaa !15
  %61 = load i32, ptr %6, align 4, !tbaa !5
  %62 = call i32 %60(i32 noundef %61) #2
  br label %63

63:                                               ; preds = %49, %38, %57
  %.0.ph = phi i32 [ 0, %57 ], [ -142, %49 ], [ -142, %38 ]
  %.pr = load i32, ptr %6, align 4, !tbaa !5
  %.not24 = icmp eq i32 %.pr, 0
  br i1 %.not24, label %.thread, label %64

64:                                               ; preds = %63
  %65 = load ptr, ptr @its_nvs, align 8, !tbaa !10
  %66 = getelementptr inbounds i8, ptr %65, i64 8
  %67 = load ptr, ptr %66, align 8, !tbaa !16
  call void %67(i32 noundef %.pr) #2
  br label %.thread

.thread:                                          ; preds = %9, %64, %63
  %.027 = phi i32 [ %.0.ph, %64 ], [ %.0.ph, %63 ], [ -146, %9 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #2
  call void @llvm.lifetime.end.p0(i64 13, ptr nonnull %5) #2
  br label %68

68:                                               ; preds = %4, %.thread
  %.017 = phi i32 [ %.027, %.thread ], [ -136, %4 ]
  ret i32 %.017
}

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass