; LLVM IR from aws/s2n-tls :: utils (s2n_safety)

; 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
define dso_local i32 @s2n_constant_time_copy_or_dont(ptr noundef %0, ptr noundef %1, i32 noundef %2, i8 noundef zeroext %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i64, align 8
  %11 = alloca i8, align 1
  %12 = alloca i8, align 1
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i8 %3, ptr %8, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  %13 = load i8, ptr %8, align 1, !tbaa !11
  %14 = zext i8 %13 to i32
  %15 = and i32 65535, %14
  %16 = sub nsw i32 %15, 1
  %17 = ashr i32 %16, 8
  %18 = and i32 %17, 255
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %9, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !12
  br label %20

20:                                               ; preds = %52, %4
  %21 = load i64, ptr %10, align 8, !tbaa !12
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = zext i32 %22 to i64
  %24 = icmp ult i64 %21, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %55

26:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #2
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr %10, align 8, !tbaa !12
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !11
  store i8 %30, ptr %11, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #2
  %31 = load i8, ptr %11, align 1, !tbaa !11
  %32 = zext i8 %31 to i32
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i64, ptr %10, align 8, !tbaa !12
  %35 = getelementptr inbounds i8, ptr %33, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = zext i8 %36 to i32
  %38 = xor i32 %32, %37
  %39 = load i8, ptr %9, align 1, !tbaa !11
  %40 = zext i8 %39 to i32
  %41 = and i32 %38, %40
  %42 = trunc i32 %41 to i8
  store i8 %42, ptr %12, align 1, !tbaa !11
  %43 = load i8, ptr %11, align 1, !tbaa !11
  %44 = zext i8 %43 to i32
  %45 = load i8, ptr %12, align 1, !tbaa !11
  %46 = zext i8 %45 to i32
  %47 = xor i32 %44, %46
  %48 = trunc i32 %47 to i8
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = load i64, ptr %10, align 8, !tbaa !12
  %51 = getelementptr inbounds i8, ptr %49, i64 %50
  store i8 %48, ptr %51, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #2
  br label %52

52:                                               ; preds = %26
  %53 = load i64, ptr %10, align 8, !tbaa !12
  %54 = add i64 %53, 1
  store i64 %54, ptr %10, align 8, !tbaa !12
  br label %20, !llvm.loop !14

55:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #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 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef i32 @s2n_constant_time_copy_or_dont(ptr nocapture noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, i8 noundef zeroext %3) local_unnamed_addr #0 {
  %5 = zext i32 %2 to i64
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %._crit_edge, label %iter.check

iter.check:                                       ; preds = %4
  %6 = zext i8 %3 to i32
  %7 = add nuw nsw i32 %6, 65535
  %8 = lshr i32 %7, 8
  %9 = trunc i32 %8 to i8
  %min.iters.check = icmp ult i32 %2, 8
  br i1 %min.iters.check, label %vec.epilog.scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %iter.check
  %scevgep = getelementptr i8, ptr %0, i64 %5
  %scevgep14 = getelementptr i8, ptr %1, i64 %5
  %bound0 = icmp ugt ptr %scevgep14, %0
  %bound1 = icmp ugt ptr %scevgep, %1
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %vec.epilog.scalar.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %vector.memcheck
  %min.iters.check15 = icmp ult i32 %2, 32
  br i1 %min.iters.check15, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %5, 4294967264
  %broadcast.splatinsert = insertelement <16 x i8> poison, i8 %9, i64 0
  %broadcast.splat = shufflevector <16 x i8> %broadcast.splatinsert, <16 x i8> poison, <16 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %10 = getelementptr inbounds i8, ptr %0, i64 %index
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %wide.load = load <16 x i8>, ptr %10, align 1, !tbaa !5, !alias.scope !8, !noalias !11
  %wide.load16 = load <16 x i8>, ptr %11, align 1, !tbaa !5, !alias.scope !8, !noalias !11
  %12 = getelementptr inbounds i8, ptr %1, i64 %index
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  %wide.load17 = load <16 x i8>, ptr %12, align 1, !tbaa !5, !alias.scope !11
  %wide.load18 = load <16 x i8>, ptr %13, align 1, !tbaa !5, !alias.scope !11
  %14 = xor <16 x i8> %wide.load17, %wide.load
  %15 = xor <16 x i8> %wide.load18, %wide.load16
  %16 = and <16 x i8> %14, %broadcast.splat
  %17 = and <16 x i8> %15, %broadcast.splat
  %18 = xor <16 x i8> %16, %wide.load
  %19 = xor <16 x i8> %17, %wide.load16
  store <16 x i8> %18, ptr %10, align 1, !tbaa !5, !alias.scope !8, !noalias !11
  store <16 x i8> %19, ptr %11, align 1, !tbaa !5, !alias.scope !8, !noalias !11
  %index.next = add nuw i64 %index, 32
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %5
  br i1 %cmp.n, label %._crit_edge, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %n.vec.remaining = and i64 %5, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %vec.epilog.scalar.ph.preheader, label %vec.epilog.ph

vec.epilog.scalar.ph.preheader:                   ; preds = %vec.epilog.middle.block, %vector.memcheck, %iter.check, %vec.epilog.iter.check
  %.013.ph = phi i64 [ 0, %iter.check ], [ 0, %vector.memcheck ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec20, %vec.epilog.middle.block ]
  %xtraiter = and i64 %5, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %vec.epilog.scalar.ph.prol.loopexit, label %vec.epilog.scalar.ph.prol

vec.epilog.scalar.ph.prol:                        ; preds = %vec.epilog.scalar.ph.preheader
  %21 = getelementptr inbounds i8, ptr %0, i64 %.013.ph
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %1, i64 %.013.ph
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %25 = xor i8 %24, %22
  %26 = and i8 %25, %9
  %27 = xor i8 %26, %22
  store i8 %27, ptr %21, align 1, !tbaa !5
  %28 = or disjoint i64 %.013.ph, 1
  br label %vec.epilog.scalar.ph.prol.loopexit

vec.epilog.scalar.ph.prol.loopexit:               ; preds = %vec.epilog.scalar.ph.prol, %vec.epilog.scalar.ph.preheader
  %.013.unr = phi i64 [ %.013.ph, %vec.epilog.scalar.ph.preheader ], [ %28, %vec.epilog.scalar.ph.prol ]
  %29 = add nsw i64 %5, -1
  %30 = icmp eq i64 %.013.ph, %29
  br i1 %30, label %._crit_edge, label %vec.epilog.scalar.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec20 = and i64 %5, 4294967288
  %broadcast.splatinsert24 = insertelement <8 x i8> poison, i8 %9, i64 0
  %broadcast.splat25 = shufflevector <8 x i8> %broadcast.splatinsert24, <8 x i8> poison, <8 x i32> zeroinitializer
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index21 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next26, %vec.epilog.vector.body ]
  %31 = getelementptr inbounds i8, ptr %0, i64 %index21
  %wide.load22 = load <8 x i8>, ptr %31, align 1, !tbaa !5, !alias.scope !17, !noalias !20
  %32 = getelementptr inbounds i8, ptr %1, i64 %index21
  %wide.load23 = load <8 x i8>, ptr %32, align 1, !tbaa !5, !alias.scope !20
  %33 = xor <8 x i8> %wide.load23, %wide.load22
  %34 = and <8 x i8> %33, %broadcast.splat25
  %35 = xor <8 x i8> %34, %wide.load22
  store <8 x i8> %35, ptr %31, align 1, !tbaa !5, !alias.scope !17, !noalias !20
  %index.next26 = add nuw i64 %index21, 8
  %36 = icmp eq i64 %index.next26, %n.vec20
  br i1 %36, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !22

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n27 = icmp eq i64 %n.vec20, %5
  br i1 %cmp.n27, label %._crit_edge, label %vec.epilog.scalar.ph.preheader

._crit_edge:                                      ; preds = %vec.epilog.scalar.ph.prol.loopexit, %vec.epilog.scalar.ph, %middle.block, %vec.epilog.middle.block, %4
  ret i32 0

vec.epilog.scalar.ph:                             ; preds = %vec.epilog.scalar.ph.prol.loopexit, %vec.epilog.scalar.ph
  %.013 = phi i64 [ %52, %vec.epilog.scalar.ph ], [ %.013.unr, %vec.epilog.scalar.ph.prol.loopexit ]
  %37 = getelementptr inbounds i8, ptr %0, i64 %.013
  %38 = load i8, ptr %37, align 1, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %1, i64 %.013
  %40 = load i8, ptr %39, align 1, !tbaa !5
  %41 = xor i8 %40, %38
  %42 = and i8 %41, %9
  %43 = xor i8 %42, %38
  store i8 %43, ptr %37, align 1, !tbaa !5
  %44 = add nuw nsw i64 %.013, 1
  %45 = getelementptr inbounds i8, ptr %0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %1, i64 %44
  %48 = load i8, ptr %47, align 1, !tbaa !5
  %49 = xor i8 %48, %46
  %50 = and i8 %49, %9
  %51 = xor i8 %50, %46
  store i8 %51, ptr %45, align 1, !tbaa !5
  %52 = add nuw nsw i64 %.013, 2
  %exitcond.not.1 = icmp eq i64 %52, %5
  br i1 %exitcond.not.1, label %._crit_edge, label %vec.epilog.scalar.ph, !llvm.loop !23
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9}
!9 = distinct !{!9, !10}
!10 = distinct !{!10, !"LVerDomain"}
!11 = !{!12}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = !{!18}
!18 = distinct !{!18, !19}
!19 = distinct !{!19, !"LVerDomain"}
!20 = !{!21}
!21 = distinct !{!21, !19}
!22 = distinct !{!22, !14, !15, !16}
!23 = distinct !{!23, !14, !15}


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