; LLVM IR from machinefi/psa-crypto-esp32 :: crypto (cmac_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"

%struct.tc_cmac_struct = type { [16 x i8], [16 x i8], [16 x i8], [16 x i8], i32, i32, ptr, i64 }

; 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

declare void @_set(ptr noundef, i8 noundef zeroext, i32 noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @tc_cmac_erase(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @tc_cmac_final(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %15, label %12

12:                                               ; preds = %2
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %12, %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %92

16:                                               ; preds = %12
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.tc_cmac_struct, ptr %17, i32 0, i32 5
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp eq i32 %19, 16
  br i1 %20, label %21, label %25

21:                                               ; preds = %16
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.tc_cmac_struct, ptr %22, i32 0, i32 1
  %24 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  store ptr %24, ptr %6, align 8, !tbaa !5
  br label %50

25:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.tc_cmac_struct, ptr %26, i32 0, i32 5
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = sub i32 16, %28
  %30 = zext i32 %29 to i64
  store i64 %30, ptr %9, align 8, !tbaa !13
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.tc_cmac_struct, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.tc_cmac_struct, ptr %33, i32 0, i32 5
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds [16 x i8], ptr %32, i64 0, i64 %36
  %38 = load i64, ptr %9, align 8, !tbaa !13
  %39 = trunc i64 %38 to i32
  call void @_set(ptr noundef %37, i8 noundef zeroext 0, i32 noundef %39)
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.tc_cmac_struct, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.tc_cmac_struct, ptr %42, i32 0, i32 5
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds [16 x i8], ptr %41, i64 0, i64 %45
  store i8 -128, ptr %46, align 1, !tbaa !14
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.tc_cmac_struct, ptr %47, i32 0, i32 2
  %49 = getelementptr inbounds [16 x i8], ptr %48, i64 0, i64 0
  store ptr %49, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %50

50:                                               ; preds = %25, %21
  store i32 0, ptr %7, align 4, !tbaa !15
  br label %51

51:                                               ; preds = %78, %50
  %52 = load i32, ptr %7, align 4, !tbaa !15
  %53 = icmp ult i32 %52, 16
  br i1 %53, label %54, label %81

54:                                               ; preds = %51
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.tc_cmac_struct, ptr %55, i32 0, i32 3
  %57 = load i32, ptr %7, align 4, !tbaa !15
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds [16 x i8], ptr %56, i64 0, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !14
  %61 = zext i8 %60 to i32
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = load i32, ptr %7, align 4, !tbaa !15
  %64 = zext i32 %63 to i64
  %65 = getelementptr inbounds i8, ptr %62, i64 %64
  %66 = load i8, ptr %65, align 1, !tbaa !14
  %67 = zext i8 %66 to i32
  %68 = xor i32 %61, %67
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.tc_cmac_struct, ptr %69, i32 0, i32 0
  %71 = load i32, ptr %7, align 4, !tbaa !15
  %72 = zext i32 %71 to i64
  %73 = getelementptr inbounds [16 x i8], ptr %70, i64 0, i64 %72
  %74 = load i8, ptr %73, align 1, !tbaa !14
  %75 = zext i8 %74 to i32
  %76 = xor i32 %75, %68
  %77 = trunc i32 %76 to i8
  store i8 %77, ptr %73, align 1, !tbaa !14
  br label %78

78:                                               ; preds = %54
  %79 = load i32, ptr %7, align 4, !tbaa !15
  %80 = add i32 %79, 1
  store i32 %80, ptr %7, align 4, !tbaa !15
  br label %51, !llvm.loop !16

81:                                               ; preds = %51
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.tc_cmac_struct, ptr %83, i32 0, i32 0
  %85 = getelementptr inbounds [16 x i8], ptr %84, i64 0, i64 0
  %86 = load ptr, ptr %5, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.tc_cmac_struct, ptr %86, i32 0, i32 6
  %88 = load ptr, ptr %87, align 8, !tbaa !18
  %89 = call i32 @tc_aes_encrypt(ptr noundef %82, ptr noundef %85, ptr noundef %88)
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = call i32 @tc_cmac_erase(ptr noundef %90)
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %92

92:                                               ; preds = %81, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %93 = load i32, ptr %3, align 4
  ret i32 %93
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #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, !11, i64 68}
!10 = !{!"tc_cmac_struct", !7, i64 0, !7, i64 16, !7, i64 32, !7, i64 48, !11, i64 64, !11, i64 68, !6, i64 72, !12, i64 80}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!11, !11, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !6, i64 72}


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

declare void @_set(ptr noundef, i8 noundef zeroext, i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @tc_cmac_final(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq ptr %1, null
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %30, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %1, i64 68
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 16
  br i1 %8, label %17, label %9

9:                                                ; preds = %5
  %10 = sub i32 16, %7
  %11 = getelementptr inbounds i8, ptr %1, i64 48
  %12 = zext i32 %7 to i64
  %13 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 %12
  tail call void @_set(ptr noundef nonnull %13, i8 noundef zeroext 0, i32 noundef %10) #2
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 %15
  store i8 -128, ptr %16, align 1, !tbaa !12
  br label %17

17:                                               ; preds = %5, %9
  %18 = phi i64 [ 32, %9 ], [ 16, %5 ]
  %19 = getelementptr inbounds i8, ptr %1, i64 %18
  %20 = getelementptr inbounds i8, ptr %1, i64 48
  %21 = load <16 x i8>, ptr %20, align 1, !tbaa !12
  %22 = load <16 x i8>, ptr %19, align 1, !tbaa !12
  %23 = xor <16 x i8> %22, %21
  %24 = load <16 x i8>, ptr %1, align 1, !tbaa !12
  %25 = xor <16 x i8> %23, %24
  store <16 x i8> %25, ptr %1, align 1, !tbaa !12
  %26 = getelementptr inbounds i8, ptr %1, i64 72
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = tail call i32 @tc_aes_encrypt(ptr noundef nonnull %0, ptr noundef nonnull %1, ptr noundef %27) #2
  %29 = tail call i32 @tc_cmac_erase(ptr noundef nonnull %1)
  br label %30

30:                                               ; preds = %2, %17
  %.0 = phi i32 [ 1, %17 ], [ 0, %2 ]
  ret i32 %.0
}

attributes #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 = { 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, !9, i64 68}
!6 = !{!"tc_cmac_struct", !7, i64 0, !7, i64 16, !7, i64 32, !7, i64 48, !9, i64 64, !9, i64 68, !10, i64 72, !11, i64 80}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!6, !10, i64 72}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass