; LLVM IR from microsoft/vOW4SIKE_on_HW :: SIKE_vOW_hw-sw/ref_c/random (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"

@state = external hidden global ptr, align 8
@Key = external hidden global ptr, align 8
@Iv = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
declare hidden void @BlockCopy(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @KeyExpansion() #0

; Function Attrs: nounwind uwtable
declare hidden void @Cipher() #0

; Function Attrs: nounwind uwtable
define dso_local void @AES128_CBC_encrypt_buffer(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i8, align 1
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #3
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = urem i32 %13, 16
  %15 = trunc i32 %14 to i8
  store i8 %15, ptr %12, align 1, !tbaa !11
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  call void @BlockCopy(ptr noundef %16, ptr noundef %17)
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %18, ptr @state, align 8, !tbaa !5
  %19 = load ptr, ptr %9, align 8, !tbaa !5
  %20 = icmp ne ptr null, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %5
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %22, ptr @Key, align 8, !tbaa !5
  call void @KeyExpansion()
  br label %23

23:                                               ; preds = %21, %5
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %28

26:                                               ; preds = %23
  %27 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %27, ptr @Iv, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %26, %23
  store i64 0, ptr %11, align 8, !tbaa !12
  br label %29

29:                                               ; preds = %44, %28
  %30 = load i64, ptr %11, align 8, !tbaa !12
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = zext i32 %31 to i64
  %33 = icmp ult i64 %30, %32
  br i1 %33, label %34, label %47

34:                                               ; preds = %29
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  call void @XorWithIv(ptr noundef %35)
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  call void @BlockCopy(ptr noundef %36, ptr noundef %37)
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %38, ptr @state, align 8, !tbaa !5
  call void @Cipher()
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %39, ptr @Iv, align 8, !tbaa !5
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i64 16
  store ptr %41, ptr %7, align 8, !tbaa !5
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i64 16
  store ptr %43, ptr %6, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %34
  %45 = load i64, ptr %11, align 8, !tbaa !12
  %46 = add i64 %45, 16
  store i64 %46, ptr %11, align 8, !tbaa !12
  br label %29, !llvm.loop !14

47:                                               ; preds = %29
  %48 = load i8, ptr %12, align 1, !tbaa !11
  %49 = icmp ne i8 %48, 0
  br i1 %49, label %50, label %63

50:                                               ; preds = %47
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  call void @BlockCopy(ptr noundef %51, ptr noundef %52)
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i8, ptr %12, align 1, !tbaa !11
  %55 = zext i8 %54 to i32
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %53, i64 %56
  %58 = load i8, ptr %12, align 1, !tbaa !11
  %59 = zext i8 %58 to i32
  %60 = sub nsw i32 16, %59
  %61 = sext i32 %60 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %57, i8 0, i64 %61, i1 false)
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %62, ptr @state, align 8, !tbaa !5
  call void @Cipher()
  br label %63

63:                                               ; preds = %50, %47
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden void @XorWithIv(ptr noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !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"

@state = external hidden local_unnamed_addr global ptr, align 8
@Key = external hidden local_unnamed_addr global ptr, align 8
@Iv = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare hidden void @BlockCopy(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @KeyExpansion() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @Cipher() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @AES128_CBC_encrypt_buffer(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  tail call void @BlockCopy(ptr noundef %0, ptr noundef %1)
  store ptr %0, ptr @state, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %7, label %6

6:                                                ; preds = %5
  store ptr %3, ptr @Key, align 8, !tbaa !5
  tail call void @KeyExpansion()
  br label %7

7:                                                ; preds = %6, %5
  %.not29 = icmp eq ptr %4, null
  br i1 %.not29, label %9, label %8

8:                                                ; preds = %7
  store ptr %4, ptr @Iv, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %8, %7
  %10 = zext i32 %2 to i64
  %.not35 = icmp eq i32 %2, 0
  br i1 %.not35, label %._crit_edge.thread, label %.lr.ph

.lr.ph:                                           ; preds = %9, %.lr.ph
  %.033 = phi ptr [ %12, %.lr.ph ], [ %0, %9 ]
  %.02432 = phi i64 [ %13, %.lr.ph ], [ 0, %9 ]
  %.02531 = phi ptr [ %11, %.lr.ph ], [ %1, %9 ]
  tail call void @XorWithIv(ptr noundef %.02531)
  tail call void @BlockCopy(ptr noundef %.033, ptr noundef %.02531)
  store ptr %.033, ptr @state, align 8, !tbaa !5
  tail call void @Cipher()
  store ptr %.033, ptr @Iv, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %.02531, i64 16
  %12 = getelementptr inbounds i8, ptr %.033, i64 16
  %13 = add nuw nsw i64 %.02432, 16
  %14 = icmp ult i64 %13, %10
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph
  %15 = and i32 %2, 15
  %.not30 = icmp eq i32 %15, 0
  br i1 %.not30, label %._crit_edge.thread, label %16

16:                                               ; preds = %._crit_edge
  tail call void @BlockCopy(ptr noundef nonnull %12, ptr noundef nonnull %11)
  %17 = zext nneg i32 %15 to i64
  %18 = getelementptr inbounds i8, ptr %12, i64 %17
  %19 = sub nuw nsw i32 16, %15
  %20 = zext nneg i32 %19 to i64
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %18, i8 0, i64 %20, i1 false)
  store ptr %12, ptr @state, align 8, !tbaa !5
  tail call void @Cipher()
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %9, %16, %._crit_edge
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @XorWithIv(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass