; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (ccm)

; 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
declare hidden void @mbedtls_zeroize(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @ccm_auth_crypt(ptr noundef, i32 noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ccm_auth_decrypt(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, ptr noundef %7, ptr noundef %8, i64 noundef %9) #1 {
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i64, align 8
  %18 = alloca ptr, align 8
  %19 = alloca ptr, align 8
  %20 = alloca ptr, align 8
  %21 = alloca i64, align 8
  %22 = alloca i32, align 4
  %23 = alloca [16 x i8], align 16
  %24 = alloca i8, align 1
  %25 = alloca i32, align 4
  %26 = alloca i32, align 4
  store ptr %0, ptr %12, align 8, !tbaa !5
  store i64 %1, ptr %13, align 8, !tbaa !9
  store ptr %2, ptr %14, align 8, !tbaa !5
  store i64 %3, ptr %15, align 8, !tbaa !9
  store ptr %4, ptr %16, align 8, !tbaa !5
  store i64 %5, ptr %17, align 8, !tbaa !9
  store ptr %6, ptr %18, align 8, !tbaa !5
  store ptr %7, ptr %19, align 8, !tbaa !5
  store ptr %8, ptr %20, align 8, !tbaa !5
  store i64 %9, ptr %21, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %22) #2
  call void @llvm.lifetime.start.p0(i64 16, ptr %23) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %24) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %25) #2
  %27 = load ptr, ptr %12, align 8, !tbaa !5
  %28 = load i64, ptr %13, align 8, !tbaa !9
  %29 = load ptr, ptr %14, align 8, !tbaa !5
  %30 = load i64, ptr %15, align 8, !tbaa !9
  %31 = load ptr, ptr %16, align 8, !tbaa !5
  %32 = load i64, ptr %17, align 8, !tbaa !9
  %33 = load ptr, ptr %18, align 8, !tbaa !5
  %34 = load ptr, ptr %19, align 8, !tbaa !5
  %35 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  %36 = load i64, ptr %21, align 8, !tbaa !9
  %37 = call i32 @ccm_auth_crypt(ptr noundef %27, i32 noundef 1, i64 noundef %28, ptr noundef %29, i64 noundef %30, ptr noundef %31, i64 noundef %32, ptr noundef %33, ptr noundef %34, ptr noundef %35, i64 noundef %36)
  store i32 %37, ptr %22, align 4, !tbaa !11
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %41

39:                                               ; preds = %10
  %40 = load i32, ptr %22, align 4, !tbaa !11
  store i32 %40, ptr %11, align 4
  store i32 1, ptr %26, align 4
  br label %72

41:                                               ; preds = %10
  store i32 0, ptr %25, align 4, !tbaa !11
  store i8 0, ptr %24, align 1, !tbaa !13
  br label %42

42:                                               ; preds = %62, %41
  %43 = load i8, ptr %24, align 1, !tbaa !13
  %44 = zext i8 %43 to i64
  %45 = load i64, ptr %21, align 8, !tbaa !9
  %46 = icmp ult i64 %44, %45
  br i1 %46, label %47, label %65

47:                                               ; preds = %42
  %48 = load ptr, ptr %20, align 8, !tbaa !5
  %49 = load i8, ptr %24, align 1, !tbaa !13
  %50 = zext i8 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !13
  %53 = zext i8 %52 to i32
  %54 = load i8, ptr %24, align 1, !tbaa !13
  %55 = zext i8 %54 to i64
  %56 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !13
  %58 = zext i8 %57 to i32
  %59 = xor i32 %53, %58
  %60 = load i32, ptr %25, align 4, !tbaa !11
  %61 = or i32 %60, %59
  store i32 %61, ptr %25, align 4, !tbaa !11
  br label %62

62:                                               ; preds = %47
  %63 = load i8, ptr %24, align 1, !tbaa !13
  %64 = add i8 %63, 1
  store i8 %64, ptr %24, align 1, !tbaa !13
  br label %42, !llvm.loop !14

65:                                               ; preds = %42
  %66 = load i32, ptr %25, align 4, !tbaa !11
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %68, label %71

68:                                               ; preds = %65
  %69 = load ptr, ptr %19, align 8, !tbaa !5
  %70 = load i64, ptr %13, align 8, !tbaa !9
  call void @mbedtls_zeroize(ptr noundef %69, i64 noundef %70)
  store i32 -15, ptr %11, align 4
  store i32 1, ptr %26, align 4
  br label %72

71:                                               ; preds = %65
  store i32 0, ptr %11, align 4
  store i32 1, ptr %26, align 4
  br label %72

72:                                               ; preds = %71, %68, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %25) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %24) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %23) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %22) #2
  %73 = load i32, ptr %11, align 4
  ret i32 %73
}

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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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
declare hidden void @mbedtls_zeroize(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @ccm_auth_crypt(ptr noundef, i32 noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ccm_auth_decrypt(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, ptr noundef %7, ptr nocapture noundef readonly %8, i64 noundef %9) local_unnamed_addr #1 {
  %11 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %11) #3
  %12 = call i32 @ccm_auth_crypt(ptr noundef %0, i32 noundef 1, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, ptr noundef %7, ptr noundef nonnull %11, i64 noundef %9)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %.preheader, label %._crit_edge.thread

.preheader:                                       ; preds = %10
  %.not27 = icmp eq i64 %9, 0
  br i1 %.not27, label %._crit_edge.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %min.iters.check = icmp ult i64 %9, 8
  br i1 %min.iters.check, label %.lr.ph.preheader34, label %vector.scevcheck

vector.scevcheck:                                 ; preds = %.lr.ph.preheader
  %13 = add i64 %9, -1
  %14 = and i64 %13, 255
  %15 = icmp eq i64 %14, 255
  %16 = icmp ugt i64 %13, 255
  %17 = or i1 %15, %16
  br i1 %17, label %.lr.ph.preheader34, label %vector.ph

vector.ph:                                        ; preds = %vector.scevcheck
  %n.vec = and i64 %9, 504
  %ind.end = trunc i64 %n.vec to i8
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %26, %vector.body ]
  %vec.phi30 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %27, %vector.body ]
  %18 = getelementptr inbounds i8, ptr %8, i64 %index
  %19 = getelementptr inbounds i8, ptr %18, i64 4
  %wide.load = load <4 x i8>, ptr %18, align 1, !tbaa !5
  %wide.load31 = load <4 x i8>, ptr %19, align 1, !tbaa !5
  %20 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 4
  %wide.load32 = load <4 x i8>, ptr %20, align 8, !tbaa !5
  %wide.load33 = load <4 x i8>, ptr %21, align 4, !tbaa !5
  %22 = xor <4 x i8> %wide.load32, %wide.load
  %23 = xor <4 x i8> %wide.load33, %wide.load31
  %24 = zext <4 x i8> %22 to <4 x i32>
  %25 = zext <4 x i8> %23 to <4 x i32>
  %26 = or <4 x i32> %vec.phi, %24
  %27 = or <4 x i32> %vec.phi30, %25
  %index.next = add nuw i64 %index, 8
  %28 = icmp eq i64 %index.next, %n.vec
  br i1 %28, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = or <4 x i32> %27, %26
  %29 = call i32 @llvm.vector.reduce.or.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %9
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader34

.lr.ph.preheader34:                               ; preds = %middle.block, %vector.scevcheck, %.lr.ph.preheader
  %.ph = phi i64 [ 0, %vector.scevcheck ], [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.026.ph = phi i32 [ 0, %vector.scevcheck ], [ 0, %.lr.ph.preheader ], [ %29, %middle.block ]
  %.02025.ph = phi i8 [ 0, %vector.scevcheck ], [ 0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader34, %.lr.ph
  %30 = phi i64 [ %39, %.lr.ph ], [ %.ph, %.lr.ph.preheader34 ]
  %.026 = phi i32 [ %37, %.lr.ph ], [ %.026.ph, %.lr.ph.preheader34 ]
  %.02025 = phi i8 [ %38, %.lr.ph ], [ %.02025.ph, %.lr.ph.preheader34 ]
  %31 = getelementptr inbounds i8, ptr %8, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !5
  %33 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 %30
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %35 = xor i8 %34, %32
  %36 = zext i8 %35 to i32
  %37 = or i32 %.026, %36
  %38 = add i8 %.02025, 1
  %39 = zext i8 %38 to i64
  %40 = icmp ult i64 %39, %9
  br i1 %40, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %middle.block
  %.lcssa = phi i32 [ %29, %middle.block ], [ %37, %.lr.ph ]
  %41 = icmp eq i32 %.lcssa, 0
  br i1 %41, label %._crit_edge.thread, label %42

42:                                               ; preds = %._crit_edge
  call void @mbedtls_zeroize(ptr noundef %7, i64 noundef %1)
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %.preheader, %._crit_edge, %10, %42
  %.021 = phi i32 [ -15, %42 ], [ %12, %10 ], [ 0, %._crit_edge ], [ 0, %.preheader ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %11) #3
  ret i32 %.021
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.or.v4i32(<4 x i32>) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass