; 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
declare dso_local i32 @s2n_constant_time_copy_or_dont(ptr noundef, ptr noundef, i32 noundef, i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_constant_time_pkcs1_unpad_or_dont(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = 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 i32 %3, ptr %9, align 4, !tbaa !9
  %14 = load i32, ptr %8, align 4, !tbaa !9
  %15 = load i32, ptr %9, align 4, !tbaa !9
  %16 = add i32 %15, 3
  %17 = icmp ult i32 %14, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %4
  store i32 0, ptr %5, align 4
  br label %90

19:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  store i8 0, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = zext i32 %24 to i64
  %26 = sub i64 0, %25
  %27 = getelementptr inbounds i8, ptr %23, i64 %26
  store ptr %27, ptr %11, align 8, !tbaa !5
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i64 0
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = zext i8 %30 to i32
  %32 = xor i32 %31, 0
  %33 = load i8, ptr %10, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  %35 = or i32 %34, %32
  %36 = trunc i32 %35 to i8
  store i8 %36, ptr %10, align 1, !tbaa !11
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i64 1
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = zext i8 %39 to i32
  %41 = xor i32 %40, 2
  %42 = load i8, ptr %10, align 1, !tbaa !11
  %43 = zext i8 %42 to i32
  %44 = or i32 %43, %41
  %45 = trunc i32 %44 to i8
  store i8 %45, ptr %10, align 1, !tbaa !11
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %46, i64 -1
  %48 = load i8, ptr %47, align 1, !tbaa !11
  %49 = zext i8 %48 to i32
  %50 = xor i32 %49, 0
  %51 = load i8, ptr %10, align 1, !tbaa !11
  %52 = zext i8 %51 to i32
  %53 = or i32 %52, %50
  %54 = trunc i32 %53 to i8
  store i8 %54, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  store i64 2, ptr %12, align 8, !tbaa !12
  br label %55

55:                                               ; preds = %81, %19
  %56 = load i64, ptr %12, align 8, !tbaa !12
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = sub i32 %57, %58
  %60 = sub i32 %59, 1
  %61 = zext i32 %60 to i64
  %62 = icmp ult i64 %56, %61
  br i1 %62, label %64, label %63

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

64:                                               ; preds = %55
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #2
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = load i64, ptr %12, align 8, !tbaa !12
  %67 = getelementptr inbounds i8, ptr %65, i64 %66
  %68 = load i8, ptr %67, align 1, !tbaa !11
  %69 = zext i8 %68 to i32
  %70 = and i32 65535, %69
  %71 = sub nsw i32 %70, 1
  %72 = ashr i32 %71, 8
  %73 = and i32 %72, 255
  %74 = trunc i32 %73 to i8
  store i8 %74, ptr %13, align 1, !tbaa !11
  %75 = load i8, ptr %13, align 1, !tbaa !11
  %76 = zext i8 %75 to i32
  %77 = load i8, ptr %10, align 1, !tbaa !11
  %78 = zext i8 %77 to i32
  %79 = or i32 %78, %76
  %80 = trunc i32 %79 to i8
  store i8 %80, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #2
  br label %81

81:                                               ; preds = %64
  %82 = load i64, ptr %12, align 8, !tbaa !12
  %83 = add i64 %82, 1
  store i64 %83, ptr %12, align 8, !tbaa !12
  br label %55, !llvm.loop !14

84:                                               ; preds = %63
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = load ptr, ptr %11, align 8, !tbaa !5
  %87 = load i32, ptr %9, align 4, !tbaa !9
  %88 = load i8, ptr %10, align 1, !tbaa !11
  %89 = call i32 @s2n_constant_time_copy_or_dont(ptr noundef %85, ptr noundef %86, i32 noundef %87, i8 noundef zeroext %88)
  store i32 0, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  br label %90

90:                                               ; preds = %84, %18
  %91 = load i32, ptr %5, align 4
  ret i32 %91
}

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @s2n_constant_time_pkcs1_unpad_or_dont(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = add i32 %3, 3
  %6 = icmp ugt i32 %5, %2
  br i1 %6, label %59, label %7

7:                                                ; preds = %4
  %8 = zext i32 %2 to i64
  %9 = getelementptr inbounds i8, ptr %1, i64 %8
  %10 = zext i32 %3 to i64
  %11 = sub nsw i64 0, %10
  %12 = getelementptr inbounds i8, ptr %9, i64 %11
  %13 = load i8, ptr %1, align 1, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %1, i64 1
  %15 = load i8, ptr %14, align 1, !tbaa !5
  %16 = xor i8 %15, 2
  %17 = or i8 %16, %13
  %18 = getelementptr inbounds i8, ptr %12, i64 -1
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %20 = or i8 %17, %19
  %21 = xor i32 %3, -1
  %22 = add i32 %21, %2
  %23 = zext i32 %22 to i64
  %24 = icmp ugt i32 %22, 2
  br i1 %24, label %iter.check, label %._crit_edge

iter.check:                                       ; preds = %7
  %25 = add nsw i64 %23, -2
  %min.iters.check = icmp ult i64 %25, 8
  br i1 %min.iters.check, label %.lr.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %iter.check
  %min.iters.check27 = icmp ult i64 %25, 32
  br i1 %min.iters.check27, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %25, -32
  %26 = insertelement <16 x i8> <i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, i8 %20, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <16 x i8> [ %26, %vector.ph ], [ %37, %vector.body ]
  %vec.phi28 = phi <16 x i8> [ zeroinitializer, %vector.ph ], [ %38, %vector.body ]
  %offset.idx = or disjoint i64 %index, 2
  %27 = getelementptr inbounds i8, ptr %1, i64 %offset.idx
  %28 = getelementptr inbounds i8, ptr %27, i64 16
  %wide.load = load <16 x i8>, ptr %27, align 1, !tbaa !5
  %wide.load29 = load <16 x i8>, ptr %28, align 1, !tbaa !5
  %29 = zext <16 x i8> %wide.load to <16 x i32>
  %30 = zext <16 x i8> %wide.load29 to <16 x i32>
  %31 = add nuw nsw <16 x i32> %29, <i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535>
  %32 = add nuw nsw <16 x i32> %30, <i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535>
  %33 = lshr <16 x i32> %31, <i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8>
  %34 = lshr <16 x i32> %32, <i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8>
  %35 = trunc <16 x i32> %33 to <16 x i8>
  %36 = trunc <16 x i32> %34 to <16 x i8>
  %37 = or <16 x i8> %vec.phi, %35
  %38 = or <16 x i8> %vec.phi28, %36
  %index.next = add nuw i64 %index, 32
  %39 = icmp eq i64 %index.next, %n.vec
  br i1 %39, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = or <16 x i8> %38, %37
  %40 = tail call i8 @llvm.vector.reduce.or.v16i8(<16 x i8> %bin.rdx)
  %cmp.n = icmp eq i64 %25, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %ind.end32 = or disjoint i64 %n.vec, 2
  %n.vec.remaining = and i64 %25, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %.lr.ph.preheader, label %vec.epilog.ph

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %.02326.ph = phi i64 [ 2, %iter.check ], [ %ind.end32, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  %.02425.ph = phi i8 [ %20, %iter.check ], [ %40, %vec.epilog.iter.check ], [ %49, %vec.epilog.middle.block ]
  br label %.lr.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %bc.merge.rdx = phi i8 [ %40, %vec.epilog.iter.check ], [ %20, %vector.main.loop.iter.check ]
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec31 = and i64 %25, -8
  %ind.end = or disjoint i64 %n.vec31, 2
  %41 = insertelement <8 x i8> <i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, i8 %bc.merge.rdx, i64 0
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index33 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next37, %vec.epilog.vector.body ]
  %vec.phi34 = phi <8 x i8> [ %41, %vec.epilog.ph ], [ %47, %vec.epilog.vector.body ]
  %offset.idx35 = or disjoint i64 %index33, 2
  %42 = getelementptr inbounds i8, ptr %1, i64 %offset.idx35
  %wide.load36 = load <8 x i8>, ptr %42, align 1, !tbaa !5
  %43 = zext <8 x i8> %wide.load36 to <8 x i32>
  %44 = add nuw nsw <8 x i32> %43, <i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535, i32 65535>
  %45 = lshr <8 x i32> %44, <i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8>
  %46 = trunc <8 x i32> %45 to <8 x i8>
  %47 = or <8 x i8> %vec.phi34, %46
  %index.next37 = add nuw i64 %index33, 8
  %48 = icmp eq i64 %index.next37, %n.vec31
  br i1 %48, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !12

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %49 = tail call i8 @llvm.vector.reduce.or.v8i8(<8 x i8> %47)
  %cmp.n38 = icmp eq i64 %25, %n.vec31
  br i1 %cmp.n38, label %._crit_edge, label %.lr.ph.preheader

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %vec.epilog.middle.block, %7
  %.024.lcssa = phi i8 [ %20, %7 ], [ %40, %middle.block ], [ %49, %vec.epilog.middle.block ], [ %57, %.lr.ph ]
  %50 = tail call i32 @s2n_constant_time_copy_or_dont(ptr noundef %0, ptr noundef nonnull %12, i32 noundef %3, i8 noundef zeroext %.024.lcssa)
  br label %59

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.02326 = phi i64 [ %58, %.lr.ph ], [ %.02326.ph, %.lr.ph.preheader ]
  %.02425 = phi i8 [ %57, %.lr.ph ], [ %.02425.ph, %.lr.ph.preheader ]
  %51 = getelementptr inbounds i8, ptr %1, i64 %.02326
  %52 = load i8, ptr %51, align 1, !tbaa !5
  %53 = zext i8 %52 to i32
  %54 = add nuw nsw i32 %53, 65535
  %55 = lshr i32 %54, 8
  %56 = trunc i32 %55 to i8
  %57 = or i8 %.02425, %56
  %58 = add nuw nsw i64 %.02326, 1
  %exitcond.not = icmp eq i64 %58, %23
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

59:                                               ; preds = %4, %._crit_edge
  ret i32 0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.vector.reduce.or.v16i8(<16 x i8>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.vector.reduce.or.v8i8(<8 x i8>) #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 speculatable willreturn memory(none) }

!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, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9, !10, !11}
!13 = distinct !{!13, !9, !11, !10}


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