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

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

; 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
define dso_local i32 @tc_cbc_mode_decrypt(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca [16 x i8], align 16
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !9
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i32 %3, ptr %11, align 4, !tbaa !9
  store ptr %4, ptr %12, align 8, !tbaa !5
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %45, label %21

21:                                               ; preds = %6
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  %23 = icmp eq ptr %22, null
  br i1 %23, label %45, label %24

24:                                               ; preds = %21
  %25 = load ptr, ptr %13, align 8, !tbaa !5
  %26 = icmp eq ptr %25, null
  br i1 %26, label %45, label %27

27:                                               ; preds = %24
  %28 = load i32, ptr %11, align 4, !tbaa !9
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %45, label %30

30:                                               ; preds = %27
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %45, label %33

33:                                               ; preds = %30
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = urem i32 %34, 16
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %45, label %37

37:                                               ; preds = %33
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = urem i32 %38, 16
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %45, label %41

41:                                               ; preds = %37
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = load i32, ptr %11, align 4, !tbaa !9
  %44 = icmp ne i32 %42, %43
  br i1 %44, label %45, label %46

45:                                               ; preds = %41, %37, %33, %30, %27, %24, %21, %6
  store i32 0, ptr %7, align 4
  store i32 1, ptr %18, align 4
  br label %82

46:                                               ; preds = %41
  %47 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %47, ptr %15, align 8, !tbaa !5
  store i32 0, ptr %17, align 4, !tbaa !9
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %78, %46
  %49 = load i32, ptr %16, align 4, !tbaa !9
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = icmp ult i32 %49, %50
  br i1 %51, label %52, label %81

52:                                               ; preds = %48
  %53 = load i32, ptr %16, align 4, !tbaa !9
  %54 = urem i32 %53, 16
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %63

56:                                               ; preds = %52
  %57 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = load ptr, ptr %13, align 8, !tbaa !5
  %60 = call i32 @tc_aes_decrypt(ptr noundef %57, ptr noundef %58, ptr noundef %59)
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %61, i64 16
  store ptr %62, ptr %10, align 8, !tbaa !5
  store i32 0, ptr %17, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %56, %52
  %64 = load i32, ptr %17, align 4, !tbaa !9
  %65 = add i32 %64, 1
  store i32 %65, ptr %17, align 4, !tbaa !9
  %66 = zext i32 %64 to i64
  %67 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 %66
  %68 = load i8, ptr %67, align 1, !tbaa !11
  %69 = zext i8 %68 to i32
  %70 = load ptr, ptr %15, align 8, !tbaa !5
  %71 = getelementptr inbounds i8, ptr %70, i32 1
  store ptr %71, ptr %15, align 8, !tbaa !5
  %72 = load i8, ptr %70, align 1, !tbaa !11
  %73 = zext i8 %72 to i32
  %74 = xor i32 %69, %73
  %75 = trunc i32 %74 to i8
  %76 = load ptr, ptr %8, align 8, !tbaa !5
  %77 = getelementptr inbounds i8, ptr %76, i32 1
  store ptr %77, ptr %8, align 8, !tbaa !5
  store i8 %75, ptr %76, align 1, !tbaa !11
  br label %78

78:                                               ; preds = %63
  %79 = load i32, ptr %16, align 4, !tbaa !9
  %80 = add i32 %79, 1
  store i32 %80, ptr %16, align 4, !tbaa !9
  br label %48, !llvm.loop !12

81:                                               ; preds = %48
  store i32 1, ptr %7, align 4
  store i32 1, ptr %18, align 4
  br label %82

82:                                               ; preds = %81, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #3
  %83 = load i32, ptr %7, align 4
  ret i32 %83
}

declare i32 @tc_aes_decrypt(ptr noundef, ptr noundef, ptr noundef) #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 = { "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/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"}


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

; 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
define dso_local range(i32 0, 2) i32 @tc_cbc_mode_decrypt(ptr noundef writeonly %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr nocapture noundef readonly %4, ptr noundef %5) local_unnamed_addr #1 {
  %7 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #3
  %8 = icmp ne ptr %0, null
  %9 = icmp ne ptr %2, null
  %or.cond.not51 = and i1 %8, %9
  %10 = icmp ne ptr %5, null
  %or.cond3.not48 = and i1 %or.cond.not51, %10
  %11 = icmp ne i32 %3, 0
  %or.cond5.not46 = and i1 %11, %or.cond3.not48
  %12 = icmp ne i32 %1, 0
  %or.cond7.not44 = and i1 %12, %or.cond5.not46
  %13 = or i32 %3, %1
  %14 = and i32 %13, 15
  %15 = icmp eq i32 %14, 0
  %or.cond41 = and i1 %15, %or.cond7.not44
  %.not39 = icmp eq i32 %1, %3
  %or.cond42 = and i1 %.not39, %or.cond41
  br i1 %or.cond42, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %6, %21
  %.056 = phi i32 [ %22, %21 ], [ 0, %6 ]
  %.02755 = phi i32 [ %30, %21 ], [ 0, %6 ]
  %.02854 = phi ptr [ %26, %21 ], [ %4, %6 ]
  %.03053 = phi ptr [ %29, %21 ], [ %0, %6 ]
  %.03152 = phi ptr [ %.132, %21 ], [ %2, %6 ]
  %16 = and i32 %.02755, 15
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %.preheader
  %19 = call i32 @tc_aes_decrypt(ptr noundef nonnull %7, ptr noundef %.03152, ptr noundef nonnull %5) #3
  %20 = getelementptr inbounds i8, ptr %.03152, i64 16
  br label %21

21:                                               ; preds = %18, %.preheader
  %.132 = phi ptr [ %20, %18 ], [ %.03152, %.preheader ]
  %.1 = phi i32 [ 0, %18 ], [ %.056, %.preheader ]
  %22 = add i32 %.1, 1
  %23 = zext i32 %.1 to i64
  %24 = getelementptr inbounds [16 x i8], ptr %7, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %.02854, i64 1
  %27 = load i8, ptr %.02854, align 1, !tbaa !5
  %28 = xor i8 %27, %25
  %29 = getelementptr inbounds i8, ptr %.03053, i64 1
  store i8 %28, ptr %.03053, align 1, !tbaa !5
  %30 = add nuw i32 %.02755, 1
  %exitcond.not = icmp eq i32 %30, %3
  br i1 %exitcond.not, label %.loopexit, label %.preheader, !llvm.loop !8

.loopexit:                                        ; preds = %21, %6
  %.029 = phi i32 [ 0, %6 ], [ 1, %21 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #3
  ret i32 %.029
}

declare i32 @tc_aes_decrypt(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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 = { "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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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