; 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: nounwind uwtable
define dso_local i32 @mbedtls_aes_crypt_ctr(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6) #1 {
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i64, align 8
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i64 %1, ptr %9, align 8, !tbaa !9
  store ptr %2, ptr %10, align 8, !tbaa !5
  store ptr %3, ptr %11, align 8, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !5
  store ptr %5, ptr %13, align 8, !tbaa !5
  store ptr %6, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #2
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  %19 = load i64, ptr %18, align 8, !tbaa !9
  store i64 %19, ptr %17, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %51, %7
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = add i64 %21, -1
  store i64 %22, ptr %9, align 8, !tbaa !9
  %23 = icmp ne i64 %21, 0
  br i1 %23, label %24, label %69

24:                                               ; preds = %20
  %25 = load i64, ptr %17, align 8, !tbaa !9
  %26 = icmp eq i64 %25, 0
  br i1 %26, label %27, label %51

27:                                               ; preds = %24
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load ptr, ptr %11, align 8, !tbaa !5
  %30 = load ptr, ptr %12, align 8, !tbaa !5
  %31 = call i32 @mbedtls_aes_crypt_ecb(ptr noundef %28, i32 noundef 1, ptr noundef %29, ptr noundef %30)
  store i32 16, ptr %16, align 4, !tbaa !11
  br label %32

32:                                               ; preds = %47, %27
  %33 = load i32, ptr %16, align 4, !tbaa !11
  %34 = icmp sgt i32 %33, 0
  br i1 %34, label %35, label %50

35:                                               ; preds = %32
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = load i32, ptr %16, align 4, !tbaa !11
  %38 = sub nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i8, ptr %36, i64 %39
  %41 = load i8, ptr %40, align 1, !tbaa !13
  %42 = add i8 %41, 1
  store i8 %42, ptr %40, align 1, !tbaa !13
  %43 = zext i8 %42 to i32
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %35
  br label %50

46:                                               ; preds = %35
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %16, align 4, !tbaa !11
  %49 = add nsw i32 %48, -1
  store i32 %49, ptr %16, align 4, !tbaa !11
  br label %32, !llvm.loop !14

50:                                               ; preds = %45, %32
  br label %51

51:                                               ; preds = %50, %24
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %52, i32 1
  store ptr %53, ptr %13, align 8, !tbaa !5
  %54 = load i8, ptr %52, align 1, !tbaa !13
  %55 = zext i8 %54 to i32
  store i32 %55, ptr %15, align 4, !tbaa !11
  %56 = load i32, ptr %15, align 4, !tbaa !11
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  %58 = load i64, ptr %17, align 8, !tbaa !9
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !13
  %61 = zext i8 %60 to i32
  %62 = xor i32 %56, %61
  %63 = trunc i32 %62 to i8
  %64 = load ptr, ptr %14, align 8, !tbaa !5
  %65 = getelementptr inbounds i8, ptr %64, i32 1
  store ptr %65, ptr %14, align 8, !tbaa !5
  store i8 %63, ptr %64, align 1, !tbaa !13
  %66 = load i64, ptr %17, align 8, !tbaa !9
  %67 = add i64 %66, 1
  %68 = and i64 %67, 15
  store i64 %68, ptr %17, align 8, !tbaa !9
  br label %20, !llvm.loop !16

69:                                               ; preds = %20
  %70 = load i64, ptr %17, align 8, !tbaa !9
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  store i64 %70, ptr %71, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  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 = { 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"}
!16 = distinct !{!16, !15}


; 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: nounwind uwtable
declare dso_local i32 @mbedtls_aes_crypt_ecb(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @mbedtls_aes_crypt_ctr(ptr noundef %0, i64 noundef %1, ptr nocapture noundef %2, ptr noundef %3, ptr noundef %4, ptr nocapture noundef readonly %5, ptr nocapture noundef writeonly %6) local_unnamed_addr #0 {
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %.not23 = icmp eq i64 %1, 0
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7
  %invariant.gep = getelementptr i8, ptr %3, i64 -1
  br label %9

9:                                                ; preds = %.lr.ph, %.loopexit
  %.in = phi i64 [ %1, %.lr.ph ], [ %10, %.loopexit ]
  %.026 = phi i64 [ %8, %.lr.ph ], [ %27, %.loopexit ]
  %.01925 = phi ptr [ %6, %.lr.ph ], [ %25, %.loopexit ]
  %.02024 = phi ptr [ %5, %.lr.ph ], [ %20, %.loopexit ]
  %10 = add i64 %.in, -1
  %11 = icmp eq i64 %.026, 0
  br i1 %11, label %12, label %.loopexit

12:                                               ; preds = %9
  %13 = tail call i32 @mbedtls_aes_crypt_ecb(ptr noundef %0, i32 noundef 1, ptr noundef %3, ptr noundef %4)
  br label %14

14:                                               ; preds = %14, %12
  %.01722 = phi i32 [ 16, %12 ], [ %18, %14 ]
  %15 = zext nneg i32 %.01722 to i64
  %gep = getelementptr i8, ptr %invariant.gep, i64 %15
  %16 = load i8, ptr %gep, align 1, !tbaa !9
  %17 = add i8 %16, 1
  store i8 %17, ptr %gep, align 1, !tbaa !9
  %.not21 = icmp eq i8 %17, 0
  %18 = add nsw i32 %.01722, -1
  %19 = icmp ugt i32 %.01722, 1
  %or.cond = and i1 %.not21, %19
  br i1 %or.cond, label %14, label %.loopexit, !llvm.loop !10

.loopexit:                                        ; preds = %14, %9
  %20 = getelementptr inbounds i8, ptr %.02024, i64 1
  %21 = load i8, ptr %.02024, align 1, !tbaa !9
  %22 = getelementptr inbounds i8, ptr %4, i64 %.026
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %24 = xor i8 %23, %21
  %25 = getelementptr inbounds i8, ptr %.01925, i64 1
  store i8 %24, ptr %.01925, align 1, !tbaa !9
  %26 = add i64 %.026, 1
  %27 = and i64 %26, 15
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %._crit_edge, label %9, !llvm.loop !12

._crit_edge:                                      ; preds = %.loopexit, %7
  %.0.lcssa = phi i64 [ %8, %7 ], [ %27, %.loopexit ]
  store i64 %.0.lcssa, ptr %2, align 8, !tbaa !5
  ret i32 0
}

attributes #0 = { 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" }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass