; 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: nounwind uwtable
define dso_local i32 @tc_cbc_mode_encrypt(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5) #0 {
  %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 i32, align 4
  %16 = alloca i32, align 4
  %17 = 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 4, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %36, label %20

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

23:                                               ; preds = %20
  %24 = load ptr, ptr %13, align 8, !tbaa !5
  %25 = icmp eq ptr %24, null
  br i1 %25, label %36, label %26

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

29:                                               ; preds = %26
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %36, label %32

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

36:                                               ; preds = %32, %29, %26, %23, %20, %6
  store i32 0, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %91

37:                                               ; preds = %32
  %38 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %39 = load ptr, ptr %12, align 8, !tbaa !5
  %40 = call i32 @_copy(ptr noundef %38, i32 noundef 16, ptr noundef %39, i32 noundef 16)
  store i32 0, ptr %16, align 4, !tbaa !9
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %41

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

45:                                               ; preds = %41
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %46, i32 1
  store ptr %47, ptr %10, align 8, !tbaa !5
  %48 = load i8, ptr %46, align 1, !tbaa !11
  %49 = zext i8 %48 to i32
  %50 = load i32, ptr %16, align 4, !tbaa !9
  %51 = add i32 %50, 1
  store i32 %51, ptr %16, align 4, !tbaa !9
  %52 = zext i32 %50 to i64
  %53 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !11
  %55 = zext i8 %54 to i32
  %56 = xor i32 %55, %49
  %57 = trunc i32 %56 to i8
  store i8 %57, ptr %53, align 1, !tbaa !11
  %58 = load i32, ptr %16, align 4, !tbaa !9
  %59 = icmp eq i32 %58, 16
  br i1 %59, label %60, label %70

60:                                               ; preds = %45
  %61 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %62 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %63 = load ptr, ptr %13, align 8, !tbaa !5
  %64 = call i32 @tc_aes_encrypt(ptr noundef %61, ptr noundef %62, ptr noundef %63)
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %67 = call i32 @_copy(ptr noundef %65, i32 noundef 16, ptr noundef %66, i32 noundef 16)
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = getelementptr inbounds i8, ptr %68, i64 16
  store ptr %69, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %86

70:                                               ; preds = %45
  %71 = load i32, ptr %15, align 4, !tbaa !9
  %72 = load i32, ptr %11, align 4, !tbaa !9
  %73 = sub i32 %72, 1
  %74 = icmp eq i32 %71, %73
  br i1 %74, label %75, label %85

75:                                               ; preds = %70
  %76 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %77 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  %79 = call i32 @tc_aes_encrypt(ptr noundef %76, ptr noundef %77, ptr noundef %78)
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  %81 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %82 = call i32 @_copy(ptr noundef %80, i32 noundef 16, ptr noundef %81, i32 noundef 16)
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  %84 = getelementptr inbounds i8, ptr %83, i64 16
  store ptr %84, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %85

85:                                               ; preds = %75, %70
  br label %86

86:                                               ; preds = %85, %60
  br label %87

87:                                               ; preds = %86
  %88 = load i32, ptr %15, align 4, !tbaa !9
  %89 = add i32 %88, 1
  store i32 %89, ptr %15, align 4, !tbaa !9
  br label %41, !llvm.loop !12

90:                                               ; preds = %41
  store i32 1, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %91

91:                                               ; preds = %90, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #3
  %92 = load i32, ptr %7, align 4
  ret i32 %92
}

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

declare i32 @_copy(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #2

declare i32 @tc_aes_encrypt(ptr noundef, ptr noundef, ptr noundef) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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: nounwind uwtable
define dso_local range(i32 0, 2) i32 @tc_cbc_mode_encrypt(ptr noundef %0, i32 noundef %1, ptr noundef readonly %2, i32 noundef %3, ptr noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %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.not47 = and i1 %8, %9
  %10 = icmp ne ptr %5, null
  %or.cond3.not44 = and i1 %or.cond.not47, %10
  %11 = icmp ne i32 %3, 0
  %or.cond5.not42 = and i1 %11, %or.cond3.not44
  %12 = icmp ne i32 %1, 0
  %or.cond7.not40 = and i1 %12, %or.cond5.not42
  %13 = and i32 %1, 15
  %.not = icmp eq i32 %13, 0
  %or.cond38 = and i1 %.not, %or.cond7.not40
  br i1 %or.cond38, label %14, label %.loopexit

14:                                               ; preds = %6
  %15 = call i32 @_copy(ptr noundef nonnull %7, i32 noundef 16, ptr noundef %4, i32 noundef 16) #3
  %16 = add i32 %3, -1
  br label %17

17:                                               ; preds = %14, %30
  %.051 = phi i32 [ 0, %14 ], [ %.1, %30 ]
  %.02850 = phi i32 [ 0, %14 ], [ %31, %30 ]
  %.03049 = phi ptr [ %0, %14 ], [ %.131, %30 ]
  %.03248 = phi ptr [ %2, %14 ], [ %18, %30 ]
  %18 = getelementptr inbounds i8, ptr %.03248, i64 1
  %19 = load i8, ptr %.03248, align 1, !tbaa !5
  %20 = add i32 %.051, 1
  %21 = zext i32 %.051 to i64
  %22 = getelementptr inbounds [16 x i8], ptr %7, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !5
  %24 = xor i8 %23, %19
  store i8 %24, ptr %22, align 1, !tbaa !5
  %25 = icmp eq i32 %20, 16
  %26 = icmp eq i32 %.02850, %16
  %or.cond = or i1 %25, %26
  br i1 %or.cond, label %.sink.split, label %30

.sink.split:                                      ; preds = %17
  %27 = call i32 @tc_aes_encrypt(ptr noundef nonnull %7, ptr noundef nonnull %7, ptr noundef nonnull %5) #3
  %28 = call i32 @_copy(ptr noundef %.03049, i32 noundef 16, ptr noundef nonnull %7, i32 noundef 16) #3
  %29 = getelementptr inbounds i8, ptr %.03049, i64 16
  br label %30

30:                                               ; preds = %17, %.sink.split
  %.131 = phi ptr [ %29, %.sink.split ], [ %.03049, %17 ]
  %.1 = phi i32 [ 0, %.sink.split ], [ %20, %17 ]
  %31 = add nuw i32 %.02850, 1
  %exitcond.not = icmp eq i32 %31, %3
  br i1 %exitcond.not, label %.loopexit, label %17, !llvm.loop !8

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

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

declare i32 @_copy(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @tc_aes_encrypt(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass