; LLVM IR from Northstrix/Encrypted_Data_Vault :: Raspberry_Pi_Pico_plus_ESP32/Firmware_for_Raspberry_Pi_Pico (serpent)

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

%union.anon = type { [8 x i32] }
%struct.serpent_key_t = type { [33 x [4 x i32]] }

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

; 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
declare dso_local i32 @serpent_gen_w(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @serpent_subbytes(ptr noundef, i32 noundef, i32 noundef) #1

; 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

; Function Attrs: nounwind uwtable
define dso_local void @serpent_setkey(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %union.anon, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %8 = getelementptr inbounds [32 x i8], ptr %5, i64 0, i64 0
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 1 %9, i64 32, i1 false)
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %54, %2
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = icmp ule i32 %11, 32
  br i1 %12, label %13, label %57

13:                                               ; preds = %10
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %43, %13
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp ult i32 %15, 4
  br i1 %16, label %17, label %46

17:                                               ; preds = %14
  %18 = getelementptr inbounds [8 x i32], ptr %5, i64 0, i64 0
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = mul i32 %19, 4
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = add i32 %20, %21
  %23 = call i32 @serpent_gen_w(ptr noundef %18, i32 noundef %22)
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.serpent_key_t, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds [33 x [4 x i32]], ptr %25, i64 0, i64 %27
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = zext i32 %29 to i64
  %31 = getelementptr inbounds [4 x i32], ptr %28, i64 0, i64 %30
  store i32 %23, ptr %31, align 4, !tbaa !9
  %32 = getelementptr inbounds [32 x i8], ptr %5, i64 0, i64 4
  call void @llvm.memmove.p0.p0.i64(ptr align 4 %5, ptr align 4 %32, i64 28, i1 false)
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.serpent_key_t, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds [33 x [4 x i32]], ptr %34, i64 0, i64 %36
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds [4 x i32], ptr %37, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = getelementptr inbounds [8 x i32], ptr %5, i64 0, i64 7
  store i32 %41, ptr %42, align 4, !tbaa !11
  br label %43

43:                                               ; preds = %17
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = add i32 %44, 1
  store i32 %45, ptr %7, align 4, !tbaa !9
  br label %14, !llvm.loop !12

46:                                               ; preds = %14
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.serpent_key_t, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = zext i32 %49 to i64
  %51 = getelementptr inbounds [33 x [4 x i32]], ptr %48, i64 0, i64 %50
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = sub i32 3, %52
  call void @serpent_subbytes(ptr noundef %51, i32 noundef %53, i32 noundef 0)
  br label %54

54:                                               ; preds = %46
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = add i32 %55, 1
  store i32 %56, ptr %6, align 4, !tbaa !9
  br label %10, !llvm.loop !14

57:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr %5) #3
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr 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 = !{!"any pointer", !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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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

%union.anon = type { [8 x i32] }

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

; 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
declare dso_local i32 @serpent_gen_w(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @serpent_subbytes(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress 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

; Function Attrs: nounwind uwtable
define dso_local void @serpent_setkey(ptr noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #1 {
  %3 = alloca %union.anon, align 4
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3) #3
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(32) %3, ptr noundef nonnull align 1 dereferenceable(32) %1, i64 32, i1 false)
  %4 = getelementptr inbounds i8, ptr %3, i64 4
  %5 = getelementptr inbounds i8, ptr %3, i64 28
  br label %.preheader

.preheader:                                       ; preds = %2, %.preheader
  %indvars.iv = phi i64 [ 0, %2 ], [ %indvars.iv.next, %.preheader ]
  %6 = shl nuw nsw i64 %indvars.iv, 2
  %7 = trunc nuw nsw i64 %6 to i32
  %8 = call i32 @serpent_gen_w(ptr noundef nonnull %3, i32 noundef %7)
  %9 = getelementptr inbounds [33 x [4 x i32]], ptr %0, i64 0, i64 %indvars.iv, i64 0
  store i32 %8, ptr %9, align 4, !tbaa !5
  call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(28) %3, ptr noundef nonnull align 4 dereferenceable(28) %4, i64 28, i1 false)
  store i32 %8, ptr %5, align 4, !tbaa !9
  %10 = trunc i64 %6 to i32
  %11 = or disjoint i32 %10, 1
  %12 = call i32 @serpent_gen_w(ptr noundef nonnull %3, i32 noundef %11)
  %13 = getelementptr inbounds [33 x [4 x i32]], ptr %0, i64 0, i64 %indvars.iv, i64 1
  store i32 %12, ptr %13, align 4, !tbaa !5
  call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(28) %3, ptr noundef nonnull align 4 dereferenceable(28) %4, i64 28, i1 false)
  store i32 %12, ptr %5, align 4, !tbaa !9
  %14 = trunc i64 %6 to i32
  %15 = or disjoint i32 %14, 2
  %16 = call i32 @serpent_gen_w(ptr noundef nonnull %3, i32 noundef %15)
  %17 = getelementptr inbounds [33 x [4 x i32]], ptr %0, i64 0, i64 %indvars.iv, i64 2
  store i32 %16, ptr %17, align 4, !tbaa !5
  call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(28) %3, ptr noundef nonnull align 4 dereferenceable(28) %4, i64 28, i1 false)
  store i32 %16, ptr %5, align 4, !tbaa !9
  %18 = trunc i64 %6 to i32
  %19 = or disjoint i32 %18, 3
  %20 = call i32 @serpent_gen_w(ptr noundef nonnull %3, i32 noundef %19)
  %21 = getelementptr inbounds [33 x [4 x i32]], ptr %0, i64 0, i64 %indvars.iv, i64 3
  store i32 %20, ptr %21, align 4, !tbaa !5
  call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(28) %3, ptr noundef nonnull align 4 dereferenceable(28) %4, i64 28, i1 false)
  store i32 %20, ptr %5, align 4, !tbaa !9
  %22 = trunc i64 %indvars.iv to i32
  %23 = sub i32 3, %22
  call void @serpent_subbytes(ptr noundef nonnull %9, i32 noundef %23, i32 noundef 0)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 33
  br i1 %exitcond.not, label %24, label %.preheader, !llvm.loop !10

24:                                               ; preds = %.preheader
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3) #3
  ret void
}

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

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass