; LLVM IR from number571/c-crypto-lib :: crypto/cipher (xor)

; 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: nounwind uwtable
define dso_local signext i8 @xor(ptr noundef %0, i16 noundef signext %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i16 %1, ptr %6, align 2, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !11
  br label %10

10:                                               ; preds = %27, %4
  %11 = load i64, ptr %9, align 8, !tbaa !11
  %12 = load i64, ptr %7, align 8, !tbaa !11
  %13 = icmp ult i64 %11, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %10
  %16 = load ptr, ptr %8, align 8, !tbaa !5
  %17 = load i64, ptr %9, align 8, !tbaa !11
  %18 = getelementptr inbounds i16, ptr %16, i64 %17
  %19 = load i16, ptr %18, align 2, !tbaa !9
  %20 = sext i16 %19 to i32
  %21 = load i16, ptr %6, align 2, !tbaa !9
  %22 = sext i16 %21 to i32
  %23 = xor i32 %20, %22
  %24 = trunc i32 %23 to i16
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds i16, ptr %25, i32 1
  store ptr %26, ptr %5, align 8, !tbaa !5
  store i16 %24, ptr %25, align 2, !tbaa !9
  br label %27

27:                                               ; preds = %15
  %28 = load i64, ptr %9, align 8, !tbaa !11
  %29 = add i64 %28, 1
  store i64 %29, ptr %9, align 8, !tbaa !11
  br label %10, !llvm.loop !13

30:                                               ; preds = %14
  ret i8 0
}

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

; 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 = { 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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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(write, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef signext i8 @xor(ptr nocapture noundef writeonly %0, i16 noundef signext %1, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %5 = ptrtoint ptr %0 to i64
  %6 = ptrtoint ptr %3 to i64
  %min.iters.check = icmp ult i64 %2, 16
  %7 = sub i64 %5, %6
  %diff.check = icmp ult i64 %7, 32
  %or.cond = or i1 %min.iters.check, %diff.check
  br i1 %or.cond, label %.lr.ph.preheader12, label %vector.ph

.lr.ph.preheader12:                               ; preds = %middle.block, %.lr.ph.preheader
  %.08.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.067.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %xtraiter = and i64 %2, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader12, %.lr.ph.prol
  %.08.prol = phi i64 [ %12, %.lr.ph.prol ], [ %.08.ph, %.lr.ph.preheader12 ]
  %.067.prol = phi ptr [ %11, %.lr.ph.prol ], [ %.067.ph, %.lr.ph.preheader12 ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader12 ]
  %8 = getelementptr inbounds i16, ptr %3, i64 %.08.prol
  %9 = load i16, ptr %8, align 2, !tbaa !5
  %10 = xor i16 %9, %1
  %11 = getelementptr inbounds i8, ptr %.067.prol, i64 2
  store i16 %10, ptr %.067.prol, align 2, !tbaa !5
  %12 = add nuw i64 %.08.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !9

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader12
  %.08.unr = phi i64 [ %.08.ph, %.lr.ph.preheader12 ], [ %12, %.lr.ph.prol ]
  %.067.unr = phi ptr [ %.067.ph, %.lr.ph.preheader12 ], [ %11, %.lr.ph.prol ]
  %13 = sub i64 %.08.ph, %2
  %14 = icmp ugt i64 %13, -4
  br i1 %14, label %._crit_edge, label %.lr.ph.preheader12.new

.lr.ph.preheader12.new:                           ; preds = %.lr.ph.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %3, i64 2
  %invariant.gep14 = getelementptr i8, ptr %3, i64 4
  %invariant.gep16 = getelementptr i8, ptr %3, i64 6
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %2, -16
  %15 = shl i64 %n.vec, 1
  %ind.end = getelementptr i8, ptr %0, i64 %15
  %broadcast.splatinsert = insertelement <8 x i16> poison, i16 %1, i64 0
  %broadcast.splat = shufflevector <8 x i16> %broadcast.splatinsert, <8 x i16> poison, <8 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %16 = getelementptr inbounds i16, ptr %3, i64 %index
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %wide.load = load <8 x i16>, ptr %16, align 2, !tbaa !5
  %wide.load11 = load <8 x i16>, ptr %17, align 2, !tbaa !5
  %18 = xor <8 x i16> %wide.load, %broadcast.splat
  %19 = xor <8 x i16> %wide.load11, %broadcast.splat
  %20 = getelementptr i8, ptr %next.gep, i64 16
  store <8 x i16> %18, ptr %next.gep, align 2, !tbaa !5
  store <8 x i16> %19, ptr %20, align 2, !tbaa !5
  %index.next = add nuw i64 %index, 16
  %21 = icmp eq i64 %index.next, %n.vec
  br i1 %21, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %2
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader12

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %4
  ret i8 0

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader12.new
  %.08 = phi i64 [ %.08.unr, %.lr.ph.preheader12.new ], [ %35, %.lr.ph ]
  %.067 = phi ptr [ %.067.unr, %.lr.ph.preheader12.new ], [ %34, %.lr.ph ]
  %22 = getelementptr inbounds i16, ptr %3, i64 %.08
  %23 = load i16, ptr %22, align 2, !tbaa !5
  %24 = xor i16 %23, %1
  %25 = getelementptr inbounds i8, ptr %.067, i64 2
  store i16 %24, ptr %.067, align 2, !tbaa !5
  %gep = getelementptr i16, ptr %invariant.gep, i64 %.08
  %26 = load i16, ptr %gep, align 2, !tbaa !5
  %27 = xor i16 %26, %1
  %28 = getelementptr inbounds i8, ptr %.067, i64 4
  store i16 %27, ptr %25, align 2, !tbaa !5
  %gep15 = getelementptr i16, ptr %invariant.gep14, i64 %.08
  %29 = load i16, ptr %gep15, align 2, !tbaa !5
  %30 = xor i16 %29, %1
  %31 = getelementptr inbounds i8, ptr %.067, i64 6
  store i16 %30, ptr %28, align 2, !tbaa !5
  %gep17 = getelementptr i16, ptr %invariant.gep16, i64 %.08
  %32 = load i16, ptr %gep17, align 2, !tbaa !5
  %33 = xor i16 %32, %1
  %34 = getelementptr inbounds i8, ptr %.067, i64 8
  store i16 %33, ptr %31, align 2, !tbaa !5
  %35 = add nuw i64 %.08, 4
  %exitcond.not.3 = icmp eq i64 %35, %2
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !15
}

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


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass