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

; 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 dso_local i32 @mbedtls_gcm_crypt_and_tag(ptr noundef, i32 noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_gcm_auth_decrypt(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7, ptr noundef %8, ptr 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 i64, align 8
  %20 = alloca ptr, align 8
  %21 = alloca ptr, align 8
  %22 = alloca i32, align 4
  %23 = alloca [16 x i8], align 16
  %24 = alloca i64, align 8
  %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 i64 %7, ptr %19, align 8, !tbaa !9
  store ptr %8, ptr %20, align 8, !tbaa !5
  store ptr %9, ptr %21, align 8, !tbaa !5
  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 8, 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 %20, align 8, !tbaa !5
  %34 = load ptr, ptr %21, align 8, !tbaa !5
  %35 = load i64, ptr %19, align 8, !tbaa !9
  %36 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  %37 = call i32 @mbedtls_gcm_crypt_and_tag(ptr noundef %27, i32 noundef 0, i64 noundef %28, ptr noundef %29, i64 noundef %30, ptr noundef %31, i64 noundef %32, ptr noundef %33, ptr noundef %34, i64 noundef %35, ptr 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 %69

41:                                               ; preds = %10
  store i32 0, ptr %25, align 4, !tbaa !11
  store i64 0, ptr %24, align 8, !tbaa !9
  br label %42

42:                                               ; preds = %59, %41
  %43 = load i64, ptr %24, align 8, !tbaa !9
  %44 = load i64, ptr %19, align 8, !tbaa !9
  %45 = icmp ult i64 %43, %44
  br i1 %45, label %46, label %62

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

59:                                               ; preds = %46
  %60 = load i64, ptr %24, align 8, !tbaa !9
  %61 = add i64 %60, 1
  store i64 %61, ptr %24, align 8, !tbaa !9
  br label %42, !llvm.loop !14

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

65:                                               ; preds = %62
  %66 = load ptr, ptr %21, align 8, !tbaa !5
  %67 = load i64, ptr %13, align 8, !tbaa !9
  call void @mbedtls_zeroize(ptr noundef %66, i64 noundef %67)
  store i32 -18, ptr %11, align 4
  store i32 1, ptr %26, align 4
  br label %69

68:                                               ; preds = %62
  store i32 0, ptr %11, align 4
  store i32 1, ptr %26, align 4
  br label %69

69:                                               ; preds = %68, %65, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %25) #2
  call void @llvm.lifetime.end.p0(i64 8, 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
  %70 = load i32, ptr %11, align 4
  ret i32 %70
}

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

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 dso_local i32 @mbedtls_gcm_crypt_and_tag(ptr noundef, i32 noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_gcm_auth_decrypt(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr nocapture noundef readonly %6, i64 noundef %7, ptr noundef %8, ptr 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 @mbedtls_gcm_crypt_and_tag(ptr noundef %0, i32 noundef 0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %8, ptr noundef %9, i64 noundef %7, ptr noundef nonnull %11)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %.preheader, label %._crit_edge.thread

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

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

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %7, -8
  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 ], [ %21, %vector.body ]
  %vec.phi28 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %22, %vector.body ]
  %13 = getelementptr inbounds i8, ptr %6, i64 %index
  %14 = getelementptr inbounds i8, ptr %13, i64 4
  %wide.load = load <4 x i8>, ptr %13, align 1, !tbaa !5
  %wide.load29 = load <4 x i8>, ptr %14, align 1, !tbaa !5
  %15 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 %index
  %16 = getelementptr inbounds i8, ptr %15, i64 4
  %wide.load30 = load <4 x i8>, ptr %15, align 8, !tbaa !5
  %wide.load31 = load <4 x i8>, ptr %16, align 4, !tbaa !5
  %17 = xor <4 x i8> %wide.load30, %wide.load
  %18 = xor <4 x i8> %wide.load31, %wide.load29
  %19 = zext <4 x i8> %17 to <4 x i32>
  %20 = zext <4 x i8> %18 to <4 x i32>
  %21 = or <4 x i32> %vec.phi, %19
  %22 = or <4 x i32> %vec.phi28, %20
  %index.next = add nuw i64 %index, 8
  %23 = icmp eq i64 %index.next, %n.vec
  br i1 %23, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = or <4 x i32> %22, %21
  %24 = call i32 @llvm.vector.reduce.or.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %7
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader32

.lr.ph.preheader32:                               ; preds = %middle.block, %.lr.ph.preheader
  %.025.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %24, %middle.block ]
  %.02024.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader32, %.lr.ph
  %.025 = phi i32 [ %31, %.lr.ph ], [ %.025.ph, %.lr.ph.preheader32 ]
  %.02024 = phi i64 [ %32, %.lr.ph ], [ %.02024.ph, %.lr.ph.preheader32 ]
  %25 = getelementptr inbounds i8, ptr %6, i64 %.02024
  %26 = load i8, ptr %25, align 1, !tbaa !5
  %27 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 %.02024
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %29 = xor i8 %28, %26
  %30 = zext i8 %29 to i32
  %31 = or i32 %.025, %30
  %32 = add nuw i64 %.02024, 1
  %exitcond.not = icmp eq i64 %32, %7
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %middle.block
  %.lcssa = phi i32 [ %24, %middle.block ], [ %31, %.lr.ph ]
  %33 = icmp eq i32 %.lcssa, 0
  br i1 %33, label %._crit_edge.thread, label %34

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

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

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

; 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, !11, !10}


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