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

; 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.mbedtls_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr }
%struct.mbedtls_cipher_info_t = type { i32, i32, i32, ptr, i32, i32, i32, ptr }

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

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

15:                                               ; preds = %3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = icmp eq ptr null, %18
  br i1 %19, label %25, label %20

20:                                               ; preds = %15
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %21, i32 0, i32 2
  %23 = load i32, ptr %22, align 4, !tbaa !14
  %24 = icmp ne i32 0, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %20, %15, %3
  store i32 -24832, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %75

26:                                               ; preds = %20
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = getelementptr inbounds %struct.mbedtls_cipher_info_t, ptr %29, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !15
  %32 = icmp eq i32 6, %31
  br i1 %32, label %33, label %74

33:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = icmp ugt i64 %34, 16
  br i1 %35, label %36, label %37

36:                                               ; preds = %33
  store i32 -24832, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %73

37:                                               ; preds = %33
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %38, i32 0, i32 9
  %40 = load ptr, ptr %39, align 8, !tbaa !17
  %41 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 0
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = call i32 @mbedtls_gcm_finish(ptr noundef %40, ptr noundef %41, i64 noundef %42)
  store i32 %43, ptr %8, align 4, !tbaa !18
  %44 = icmp ne i32 0, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %37
  %46 = load i32, ptr %8, align 4, !tbaa !18
  store i32 %46, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %73

47:                                               ; preds = %37
  store i32 0, ptr %12, align 4, !tbaa !18
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %48

48:                                               ; preds = %65, %47
  %49 = load i64, ptr %11, align 8, !tbaa !9
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = icmp ult i64 %49, %50
  br i1 %51, label %52, label %68

52:                                               ; preds = %48
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i64, ptr %11, align 8, !tbaa !9
  %55 = getelementptr inbounds i8, ptr %53, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !19
  %57 = zext i8 %56 to i32
  %58 = load i64, ptr %11, align 8, !tbaa !9
  %59 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !19
  %61 = zext i8 %60 to i32
  %62 = xor i32 %57, %61
  %63 = load i32, ptr %12, align 4, !tbaa !18
  %64 = or i32 %63, %62
  store i32 %64, ptr %12, align 4, !tbaa !18
  br label %65

65:                                               ; preds = %52
  %66 = load i64, ptr %11, align 8, !tbaa !9
  %67 = add i64 %66, 1
  store i64 %67, ptr %11, align 8, !tbaa !9
  br label %48, !llvm.loop !20

68:                                               ; preds = %48
  %69 = load i32, ptr %12, align 4, !tbaa !18
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %71, label %72

71:                                               ; preds = %68
  store i32 -25344, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %73

72:                                               ; preds = %68
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %73

73:                                               ; preds = %72, %71, %45, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #3
  br label %75

74:                                               ; preds = %26
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %75

75:                                               ; preds = %74, %73, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %76 = load i32, ptr %4, align 4
  ret i32 %76
}

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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8, !13, i64 12, !6, i64 16, !6, i64 24, !7, i64 32, !10, i64 48, !7, i64 56, !10, i64 72, !6, i64 80}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 12}
!15 = !{!16, !13, i64 4}
!16 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !6, i64 16, !13, i64 24, !13, i64 28, !13, i64 32, !6, i64 40}
!17 = !{!12, !6, i64 80}
!18 = !{!13, !13, i64 0}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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

declare i32 @mbedtls_gcm_finish(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_cipher_check_tag(ptr noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = alloca [16 x i8], align 16
  %5 = icmp eq ptr %0, null
  br i1 %5, label %46, label %6

6:                                                ; preds = %3
  %7 = load ptr, ptr %0, align 8, !tbaa !5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %46, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %0, i64 12
  %11 = load i32, ptr %10, align 4, !tbaa !12
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %12, label %46

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %7, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = icmp eq i32 %14, 6
  br i1 %15, label %16, label %46

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #4
  %17 = icmp ugt i64 %2, 16
  br i1 %17, label %._crit_edge, label %18

18:                                               ; preds = %16
  %19 = getelementptr inbounds i8, ptr %0, i64 80
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = call i32 @mbedtls_gcm_finish(ptr noundef %20, ptr noundef nonnull %4, i64 noundef %2) #4
  %.not22 = icmp eq i32 %21, 0
  br i1 %.not22, label %.preheader, label %._crit_edge

.preheader:                                       ; preds = %18
  %.not26 = icmp eq i64 %2, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph.preheader

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

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %2, 24
  %22 = getelementptr inbounds i8, ptr %1, i64 4
  %wide.load = load <4 x i8>, ptr %1, align 1, !tbaa !16
  %wide.load28 = load <4 x i8>, ptr %22, align 1, !tbaa !16
  %23 = getelementptr inbounds i8, ptr %4, i64 4
  %wide.load29 = load <4 x i8>, ptr %4, align 16, !tbaa !16
  %wide.load30 = load <4 x i8>, ptr %23, align 4, !tbaa !16
  %24 = xor <4 x i8> %wide.load29, %wide.load
  %25 = xor <4 x i8> %wide.load30, %wide.load28
  %26 = icmp eq i64 %n.vec, 8
  br i1 %26, label %middle.block, label %vector.body.1, !llvm.loop !17

vector.body.1:                                    ; preds = %vector.ph
  %27 = getelementptr inbounds i8, ptr %1, i64 8
  %28 = getelementptr inbounds i8, ptr %1, i64 12
  %wide.load.1 = load <4 x i8>, ptr %27, align 1, !tbaa !16
  %wide.load28.1 = load <4 x i8>, ptr %28, align 1, !tbaa !16
  %29 = getelementptr inbounds i8, ptr %4, i64 8
  %30 = getelementptr inbounds i8, ptr %4, i64 12
  %wide.load29.1 = load <4 x i8>, ptr %29, align 8, !tbaa !16
  %wide.load30.1 = load <4 x i8>, ptr %30, align 4, !tbaa !16
  %31 = xor <4 x i8> %wide.load29.1, %wide.load.1
  %32 = xor <4 x i8> %wide.load30.1, %wide.load28.1
  %33 = or <4 x i8> %24, %31
  %34 = or <4 x i8> %25, %32
  br label %middle.block

middle.block:                                     ; preds = %vector.body.1, %vector.ph
  %.lcssa34.in = phi <4 x i8> [ %24, %vector.ph ], [ %33, %vector.body.1 ]
  %.lcssa33.in = phi <4 x i8> [ %25, %vector.ph ], [ %34, %vector.body.1 ]
  %bin.rdx35 = or <4 x i8> %.lcssa33.in, %.lcssa34.in
  %bin.rdx = zext <4 x i8> %bin.rdx35 to <4 x i32>
  %35 = call i32 @llvm.vector.reduce.or.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %2
  br i1 %cmp.n, label %._crit_edge.loopexit, label %.lr.ph.preheader31

.lr.ph.preheader31:                               ; preds = %middle.block, %.lr.ph.preheader
  %.025.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %35, %middle.block ]
  %.01624.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader31, %.lr.ph
  %.025 = phi i32 [ %42, %.lr.ph ], [ %.025.ph, %.lr.ph.preheader31 ]
  %.01624 = phi i64 [ %43, %.lr.ph ], [ %.01624.ph, %.lr.ph.preheader31 ]
  %36 = getelementptr inbounds i8, ptr %1, i64 %.01624
  %37 = load i8, ptr %36, align 1, !tbaa !16
  %38 = getelementptr inbounds [16 x i8], ptr %4, i64 0, i64 %.01624
  %39 = load i8, ptr %38, align 1, !tbaa !16
  %40 = xor i8 %39, %37
  %41 = zext i8 %40 to i32
  %42 = or i32 %.025, %41
  %43 = add nuw nsw i64 %.01624, 1
  %exitcond.not = icmp eq i64 %43, %2
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !21

._crit_edge.loopexit:                             ; preds = %.lr.ph, %middle.block
  %.lcssa = phi i32 [ %35, %middle.block ], [ %42, %.lr.ph ]
  %44 = icmp eq i32 %.lcssa, 0
  %45 = select i1 %44, i32 0, i32 -25344
  br label %._crit_edge

._crit_edge:                                      ; preds = %.preheader, %._crit_edge.loopexit, %18, %16
  %.017 = phi i32 [ -24832, %16 ], [ %21, %18 ], [ 0, %.preheader ], [ %45, %._crit_edge.loopexit ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #4
  br label %46

46:                                               ; preds = %12, %3, %6, %9, %._crit_edge
  %.1 = phi i32 [ %.017, %._crit_edge ], [ -24832, %9 ], [ -24832, %6 ], [ -24832, %3 ], [ 0, %12 ]
  ret i32 %.1
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.or.v4i32(<4 x i32>) #3

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 24, !8, i64 32, !11, i64 48, !8, i64 56, !11, i64 72, !7, i64 80}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !10, i64 12}
!13 = !{!14, !10, i64 4}
!14 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !7, i64 40}
!15 = !{!6, !7, i64 80}
!16 = !{!8, !8, i64 0}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !20, !19}


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