; LLVM IR from JamisHoo/Cryptographic-Algorithms :: src/des_cbc.ll (Z7des_cbcPKvmS0_S0_Pv)

; 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.std::array" = type { [16 x %"struct.std::array.0"] }
%"struct.std::array.0" = type { [6 x i8] }

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z15generateSubkeysPKh(ptr dead_on_unwind noalias writable sret(%"struct.std::array") align 1, ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z17des_cbc_iterationPKhRKSt5arrayIS1_IhLm6EELm16EEPh(ptr noundef, ptr noundef nonnull align 1 dereferenceable(96), ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7des_cbcPKvmS0_S0_Pv(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca [8 x i8], align 1
  %15 = alloca %"struct.std::array", align 1
  %16 = alloca i64, align 8
  %17 = alloca i32, align 4
  %18 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %19, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %20, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %21, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %22 = getelementptr inbounds [8 x i8], ptr %14, i64 0, i64 0
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %22, ptr align 1 %23, i64 8, i1 false)
  call void @llvm.lifetime.start.p0(i64 96, ptr %15) #3
  %24 = load ptr, ptr %12, align 8, !tbaa !5
  call void @_Z15generateSubkeysPKh(ptr dead_on_unwind writable sret(%"struct.std::array") align 1 %15, ptr noundef %24)
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  store i64 0, ptr %16, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %65, %5
  %26 = load i64, ptr %16, align 8, !tbaa !9
  %27 = load i64, ptr %7, align 8, !tbaa !9
  %28 = udiv i64 %27, 8
  %29 = icmp ult i64 %26, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %25
  store i32 2, ptr %17, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  br label %68

31:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #3
  store i64 0, ptr %18, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %51, %31
  %33 = load i64, ptr %18, align 8, !tbaa !9
  %34 = icmp ult i64 %33, 8
  br i1 %34, label %36, label %35

35:                                               ; preds = %32
  store i32 5, ptr %17, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #3
  br label %54

36:                                               ; preds = %32
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = load i64, ptr %16, align 8, !tbaa !9
  %39 = mul i64 8, %38
  %40 = load i64, ptr %18, align 8, !tbaa !9
  %41 = add i64 %39, %40
  %42 = getelementptr inbounds i8, ptr %37, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !11
  %44 = zext i8 %43 to i32
  %45 = load i64, ptr %18, align 8, !tbaa !9
  %46 = getelementptr inbounds [8 x i8], ptr %14, i64 0, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = zext i8 %47 to i32
  %49 = xor i32 %48, %44
  %50 = trunc i32 %49 to i8
  store i8 %50, ptr %46, align 1, !tbaa !11
  br label %51

51:                                               ; preds = %36
  %52 = load i64, ptr %18, align 8, !tbaa !9
  %53 = add i64 %52, 1
  store i64 %53, ptr %18, align 8, !tbaa !9
  br label %32, !llvm.loop !12

54:                                               ; preds = %35
  %55 = getelementptr inbounds [8 x i8], ptr %14, i64 0, i64 0
  %56 = load ptr, ptr %13, align 8, !tbaa !5
  %57 = load i64, ptr %16, align 8, !tbaa !9
  %58 = mul i64 8, %57
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  call void @_Z17des_cbc_iterationPKhRKSt5arrayIS1_IhLm6EELm16EEPh(ptr noundef %55, ptr noundef nonnull align 1 dereferenceable(96) %15, ptr noundef %59)
  %60 = getelementptr inbounds [8 x i8], ptr %14, i64 0, i64 0
  %61 = load ptr, ptr %13, align 8, !tbaa !5
  %62 = load i64, ptr %16, align 8, !tbaa !9
  %63 = mul i64 8, %62
  %64 = getelementptr inbounds i8, ptr %61, i64 %63
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %60, ptr align 1 %64, i64 8, i1 false)
  br label %65

65:                                               ; preds = %54
  %66 = load i64, ptr %16, align 8, !tbaa !9
  %67 = add i64 %66, 1
  store i64 %67, ptr %16, align 8, !tbaa !9
  br label %25, !llvm.loop !14

68:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 96, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  ret void
}

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !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"

%"struct.std::array" = type { [16 x %"struct.std::array.0"] }
%"struct.std::array.0" = type { [6 x i8] }

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z15generateSubkeysPKh(ptr dead_on_unwind noalias writable sret(%"struct.std::array") align 1, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z17des_cbc_iterationPKhRKSt5arrayIS1_IhLm6EELm16EEPh(ptr noundef, ptr noundef nonnull align 1 dereferenceable(96), ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7des_cbcPKvmS0_S0_Pv(ptr nocapture noundef readonly %0, i64 noundef %1, ptr noundef %2, ptr nocapture noundef readonly %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = alloca [8 x i8], align 8
  %7 = alloca %"struct.std::array", align 1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #2
  %8 = load i64, ptr %3, align 1
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %7) #2
  call void @_Z15generateSubkeysPKh(ptr dead_on_unwind nonnull writable sret(%"struct.std::array") align 1 %7, ptr noundef %2)
  %9 = lshr i64 %1, 3
  %.not = icmp ult i64 %1, 8
  br i1 %.not, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %5, %.preheader
  %.in.in = phi i64 [ %31, %.preheader ], [ %8, %5 ]
  %.01719 = phi i64 [ %32, %.preheader ], [ 0, %5 ]
  %.in25 = lshr i64 %.in.in, 8
  %.in24 = lshr i64 %.in.in, 16
  %.in23 = lshr i64 %.in.in, 24
  %.in22 = lshr i64 %.in.in, 32
  %.in21 = lshr i64 %.in.in, 40
  %.in20 = lshr i64 %.in.in, 48
  %.in = lshr i64 %.in.in, 56
  %10 = shl nuw i64 %.01719, 3
  %11 = getelementptr i8, ptr %0, i64 %10
  %12 = insertelement <2 x i64> poison, i64 %.in.in, i64 0
  %13 = insertelement <2 x i64> %12, i64 %.in25, i64 1
  %14 = trunc <2 x i64> %13 to <2 x i8>
  %15 = shufflevector <2 x i8> %14, <2 x i8> poison, <8 x i32> <i32 0, i32 1, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
  %16 = trunc i64 %.in24 to i8
  %17 = insertelement <8 x i8> %15, i8 %16, i64 2
  %18 = trunc i64 %.in23 to i8
  %19 = insertelement <8 x i8> %17, i8 %18, i64 3
  %20 = trunc i64 %.in22 to i8
  %21 = insertelement <8 x i8> %19, i8 %20, i64 4
  %22 = trunc i64 %.in21 to i8
  %23 = insertelement <8 x i8> %21, i8 %22, i64 5
  %24 = trunc i64 %.in20 to i8
  %25 = insertelement <8 x i8> %23, i8 %24, i64 6
  %26 = trunc nuw i64 %.in to i8
  %27 = insertelement <8 x i8> %25, i8 %26, i64 7
  %28 = load <8 x i8>, ptr %11, align 1, !tbaa !5
  %29 = xor <8 x i8> %28, %27
  store <8 x i8> %29, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %4, i64 %10
  call void @_Z17des_cbc_iterationPKhRKSt5arrayIS1_IhLm6EELm16EEPh(ptr noundef nonnull %6, ptr noundef nonnull align 1 dereferenceable(96) %7, ptr noundef %30)
  %31 = load i64, ptr %30, align 1
  store i64 %31, ptr %6, align 8
  %32 = add nuw nsw i64 %.01719, 1
  %exitcond.not = icmp eq i64 %32, %9
  br i1 %exitcond.not, label %._crit_edge, label %.preheader, !llvm.loop !8

._crit_edge:                                      ; preds = %.preheader, %5
  call void @llvm.lifetime.end.p0(i64 96, ptr nonnull %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #2
  ret void
}

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass