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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

15:                                               ; preds = %50, %6
  %16 = load i64, ptr %9, align 8, !tbaa !11
  %17 = add i64 %16, -1
  store i64 %17, ptr %9, align 8, !tbaa !11
  %18 = icmp ne i64 %16, 0
  br i1 %18, label %19, label %54

19:                                               ; preds = %15
  %20 = getelementptr inbounds [17 x i8], ptr %14, i64 0, i64 0
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %20, ptr align 1 %21, i64 16, i1 false)
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = load ptr, ptr %10, align 8, !tbaa !5
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = call i32 @mbedtls_aes_crypt_ecb(ptr noundef %22, i32 noundef 1, ptr noundef %23, ptr noundef %24)
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %32

28:                                               ; preds = %19
  %29 = load ptr, ptr %11, align 8, !tbaa !5
  %30 = load i8, ptr %29, align 1, !tbaa !13
  %31 = getelementptr inbounds [17 x i8], ptr %14, i64 0, i64 16
  store i8 %30, ptr %31, align 16, !tbaa !13
  br label %32

32:                                               ; preds = %28, %19
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i64 0
  %35 = load i8, ptr %34, align 1, !tbaa !13
  %36 = zext i8 %35 to i32
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i32 1
  store ptr %38, ptr %11, align 8, !tbaa !5
  %39 = load i8, ptr %37, align 1, !tbaa !13
  %40 = zext i8 %39 to i32
  %41 = xor i32 %36, %40
  %42 = trunc i32 %41 to i8
  %43 = load ptr, ptr %12, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i32 1
  store ptr %44, ptr %12, align 8, !tbaa !5
  store i8 %42, ptr %43, align 1, !tbaa !13
  store i8 %42, ptr %13, align 1, !tbaa !13
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = icmp eq i32 %45, 1
  br i1 %46, label %47, label %50

47:                                               ; preds = %32
  %48 = load i8, ptr %13, align 1, !tbaa !13
  %49 = getelementptr inbounds [17 x i8], ptr %14, i64 0, i64 16
  store i8 %48, ptr %49, align 16, !tbaa !13
  br label %50

50:                                               ; preds = %47, %32
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds [17 x i8], ptr %14, i64 0, i64 0
  %53 = getelementptr inbounds i8, ptr %52, i64 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %51, ptr align 1 %53, i64 16, i1 false)
  br label %15, !llvm.loop !14

54:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 17, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #3
  ret i32 0
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!12, !12, i64 0}
!12 = !{!"long", !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_aes_crypt_ecb(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @mbedtls_aes_crypt_cfb8(ptr noundef %0, i32 noundef %1, i64 noundef %2, ptr noundef %3, ptr nocapture noundef readonly %4, ptr nocapture noundef writeonly %5) local_unnamed_addr #1 {
  %.sroa.0 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %.sroa.0)
  %.not14 = icmp eq i64 %2, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %7 = add i64 %2, -1
  %.sroa.0.1.sroa_idx = getelementptr inbounds i8, ptr %.sroa.0, i64 1
  %.sroa.4.1..sroa_idx = getelementptr inbounds i8, ptr %3, i64 15
  switch i32 %1, label %.lr.ph.split.split [
    i32 0, label %.lr.ph.split.us.split
    i32 1, label %.lr.ph.split.split.us
  ]

.lr.ph.split.us.split:                            ; preds = %.lr.ph, %.lr.ph.split.us.split
  %8 = phi i64 [ %15, %.lr.ph.split.us.split ], [ %7, %.lr.ph ]
  %.01216.us = phi ptr [ %14, %.lr.ph.split.us.split ], [ %5, %.lr.ph ]
  %.01315.us = phi ptr [ %12, %.lr.ph.split.us.split ], [ %4, %.lr.ph ]
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %.sroa.0, ptr noundef nonnull align 1 dereferenceable(16) %3, i64 16, i1 false)
  %9 = tail call i32 @mbedtls_aes_crypt_ecb(ptr noundef %0, i32 noundef 1, ptr noundef %3, ptr noundef %3)
  %10 = load i8, ptr %.01315.us, align 1, !tbaa !5
  %11 = load i8, ptr %3, align 1, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %.01315.us, i64 1
  %13 = xor i8 %10, %11
  %14 = getelementptr inbounds i8, ptr %.01216.us, i64 1
  store i8 %13, ptr %.01216.us, align 1, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(15) %3, ptr noundef nonnull align 1 dereferenceable(15) %.sroa.0.1.sroa_idx, i64 15, i1 false)
  store i8 %10, ptr %.sroa.4.1..sroa_idx, align 1
  %15 = add i64 %8, -1
  %.not.us = icmp eq i64 %8, 0
  br i1 %.not.us, label %._crit_edge, label %.lr.ph.split.us.split, !llvm.loop !8

.lr.ph.split.split.us:                            ; preds = %.lr.ph, %.lr.ph.split.split.us
  %16 = phi i64 [ %23, %.lr.ph.split.split.us ], [ %7, %.lr.ph ]
  %.01216.us19 = phi ptr [ %22, %.lr.ph.split.split.us ], [ %5, %.lr.ph ]
  %.01315.us20 = phi ptr [ %19, %.lr.ph.split.split.us ], [ %4, %.lr.ph ]
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %.sroa.0, ptr noundef nonnull align 1 dereferenceable(16) %3, i64 16, i1 false)
  %17 = tail call i32 @mbedtls_aes_crypt_ecb(ptr noundef %0, i32 noundef 1, ptr noundef %3, ptr noundef %3)
  %18 = load i8, ptr %3, align 1, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.01315.us20, i64 1
  %20 = load i8, ptr %.01315.us20, align 1, !tbaa !5
  %21 = xor i8 %20, %18
  %22 = getelementptr inbounds i8, ptr %.01216.us19, i64 1
  store i8 %21, ptr %.01216.us19, align 1, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(15) %3, ptr noundef nonnull align 1 dereferenceable(15) %.sroa.0.1.sroa_idx, i64 15, i1 false)
  store i8 %21, ptr %.sroa.4.1..sroa_idx, align 1
  %23 = add i64 %16, -1
  %.not.us21 = icmp eq i64 %16, 0
  br i1 %.not.us21, label %._crit_edge, label %.lr.ph.split.split.us, !llvm.loop !8

.lr.ph.split.split:                               ; preds = %.lr.ph, %.lr.ph.split.split
  %24 = phi i64 [ %31, %.lr.ph.split.split ], [ %7, %.lr.ph ]
  %.01216 = phi ptr [ %30, %.lr.ph.split.split ], [ %5, %.lr.ph ]
  %.01315 = phi ptr [ %27, %.lr.ph.split.split ], [ %4, %.lr.ph ]
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %.sroa.0, ptr noundef nonnull align 1 dereferenceable(16) %3, i64 16, i1 false)
  %25 = tail call i32 @mbedtls_aes_crypt_ecb(ptr noundef %0, i32 noundef 1, ptr noundef %3, ptr noundef %3)
  %26 = load i8, ptr %3, align 1, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %.01315, i64 1
  %28 = load i8, ptr %.01315, align 1, !tbaa !5
  %29 = xor i8 %28, %26
  %30 = getelementptr inbounds i8, ptr %.01216, i64 1
  store i8 %29, ptr %.01216, align 1, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(15) %3, ptr noundef nonnull align 1 dereferenceable(15) %.sroa.0.1.sroa_idx, i64 15, i1 false)
  %31 = add i64 %24, -1
  %.not = icmp eq i64 %24, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.split.split, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph.split.split.us, %.lr.ph.split.us.split, %.lr.ph.split.split, %6
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %.sroa.0)
  ret i32 0
}

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass