; LLVM IR from angt/glorytun :: src (common)

; 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"

@gt_tohex.tbl = external hidden constant [17 x i8], align 16

; 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 @gt_tohex(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = icmp ne i64 %11, 0
  br i1 %12, label %14, label %13

13:                                               ; preds = %4
  store i32 -1, ptr %5, align 4
  br label %56

14:                                               ; preds = %4
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = sub i64 %15, 1
  %17 = udiv i64 %16, 2
  %18 = load i64, ptr %9, align 8, !tbaa !9
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %14
  store i32 -1, ptr %5, align 4
  br label %56

21:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %51, %21
  %23 = load i64, ptr %10, align 8, !tbaa !9
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = icmp ult i64 %23, %24
  br i1 %25, label %27, label %26

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

27:                                               ; preds = %22
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load i64, ptr %10, align 8, !tbaa !9
  %30 = getelementptr inbounds i8, ptr %28, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = zext i8 %31 to i32
  %33 = ashr i32 %32, 4
  %34 = and i32 15, %33
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i32 1
  store ptr %39, ptr %6, align 8, !tbaa !5
  store i8 %37, ptr %38, align 1, !tbaa !11
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = load i64, ptr %10, align 8, !tbaa !9
  %42 = getelementptr inbounds i8, ptr %40, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !11
  %44 = zext i8 %43 to i32
  %45 = and i32 15, %44
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !11
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds i8, ptr %49, i32 1
  store ptr %50, ptr %6, align 8, !tbaa !5
  store i8 %48, ptr %49, align 1, !tbaa !11
  br label %51

51:                                               ; preds = %27
  %52 = load i64, ptr %10, align 8, !tbaa !9
  %53 = add i64 %52, 1
  store i64 %53, ptr %10, align 8, !tbaa !9
  br label %22, !llvm.loop !12

54:                                               ; preds = %26
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %55, align 1, !tbaa !11
  store i32 0, ptr %5, align 4
  br label %56

56:                                               ; preds = %54, %20, %13
  %57 = load i32, ptr %5, align 4
  ret i32 %57
}

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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@gt_tohex.tbl = external hidden local_unnamed_addr constant [17 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(write, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 -1, 1) i32 @gt_tohex(ptr nocapture noundef writeonly %0, i64 noundef %1, ptr nocapture noundef readonly %2, i64 noundef %3) local_unnamed_addr #0 {
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %51, label %5

5:                                                ; preds = %4
  %6 = add i64 %1, -1
  %7 = lshr i64 %6, 1
  %8 = icmp ult i64 %7, %3
  br i1 %8, label %51, label %.preheader

.preheader:                                       ; preds = %5
  %.not19 = icmp eq i64 %3, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %xtraiter = and i64 %3, 1
  %9 = icmp eq i64 %3, 1
  br i1 %9, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %3, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi ptr [ poison, %.lr.ph.preheader ], [ %49, %.lr.ph ]
  %.018.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %50, %.lr.ph ]
  %.01417.unr = phi ptr [ %0, %.lr.ph.preheader ], [ %49, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %10 = getelementptr inbounds i8, ptr %2, i64 %.018.unr
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = lshr i8 %11, 4
  %13 = zext nneg i8 %12 to i64
  %14 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %.01417.unr, i64 1
  store i8 %15, ptr %.01417.unr, align 1, !tbaa !5
  %17 = load i8, ptr %10, align 1, !tbaa !5
  %18 = and i8 %17, 15
  %19 = zext nneg i8 %18 to i64
  %20 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %.01417.unr, i64 2
  store i8 %21, ptr %16, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %.preheader
  %.014.lcssa = phi ptr [ %0, %.preheader ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %22, %.lr.ph.epil ]
  store i8 0, ptr %.014.lcssa, align 1, !tbaa !5
  br label %51

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.018 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %50, %.lr.ph ]
  %.01417 = phi ptr [ %0, %.lr.ph.preheader.new ], [ %49, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %23 = getelementptr inbounds i8, ptr %2, i64 %.018
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %25 = lshr i8 %24, 4
  %26 = zext nneg i8 %25 to i64
  %27 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %.01417, i64 1
  store i8 %28, ptr %.01417, align 1, !tbaa !5
  %30 = load i8, ptr %23, align 1, !tbaa !5
  %31 = and i8 %30, 15
  %32 = zext nneg i8 %31 to i64
  %33 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %.01417, i64 2
  store i8 %34, ptr %29, align 1, !tbaa !5
  %36 = or disjoint i64 %.018, 1
  %37 = getelementptr inbounds i8, ptr %2, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !5
  %39 = lshr i8 %38, 4
  %40 = zext nneg i8 %39 to i64
  %41 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %.01417, i64 3
  store i8 %42, ptr %35, align 1, !tbaa !5
  %44 = load i8, ptr %37, align 1, !tbaa !5
  %45 = and i8 %44, 15
  %46 = zext nneg i8 %45 to i64
  %47 = getelementptr inbounds [17 x i8], ptr @gt_tohex.tbl, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %.01417, i64 4
  store i8 %48, ptr %43, align 1, !tbaa !5
  %50 = add nuw nsw i64 %.018, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !8

51:                                               ; preds = %5, %4, %._crit_edge
  %.013 = phi i32 [ 0, %._crit_edge ], [ -1, %4 ], [ -1, %5 ]
  ret i32 %.013
}

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


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