; LLVM IR from aws/s2n-tls :: crypto (s2n_sequence)

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

%struct.s2n_debug_info = type { ptr, ptr }
%struct.s2n_blob = type { ptr, i32, i32, i8 }

@_s2n_debug_info = external thread_local global %struct.s2n_debug_info, align 8
@s2n_errno = external thread_local global i32, align 4
@.str.1 = external hidden unnamed_addr constant [91 x i8], align 1

; 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 speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

; Function Attrs: nounwind willreturn memory(read)
declare ptr @strrchr(ptr noundef, i32 noundef) #2

declare i32 @s2n_calculate_stacktrace() #3

; 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_sequence_number_to_uint64(ptr noundef %0, ptr noundef %1) #4 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %42, label %11

11:                                               ; preds = %8
  br label %12

12:                                               ; preds = %11
  br label %13

13:                                               ; preds = %12
  %14 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %15 = getelementptr inbounds %struct.s2n_debug_info, ptr %14, i32 0, i32 0
  store ptr @.str.1, ptr %15, align 8, !tbaa !9
  %16 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %17 = getelementptr inbounds %struct.s2n_debug_info, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %18, i64 21
  %20 = call ptr @strrchr(ptr noundef %19, i32 noundef 47) #5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %29

22:                                               ; preds = %13
  %23 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %24 = getelementptr inbounds %struct.s2n_debug_info, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %25, i64 21
  %27 = call ptr @strrchr(ptr noundef %26, i32 noundef 47) #5
  %28 = getelementptr inbounds i8, ptr %27, i64 1
  br label %34

29:                                               ; preds = %13
  %30 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %31 = getelementptr inbounds %struct.s2n_debug_info, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %32, i64 21
  br label %34

34:                                               ; preds = %29, %22
  %35 = phi ptr [ %28, %22 ], [ %33, %29 ]
  %36 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  %37 = getelementptr inbounds %struct.s2n_debug_info, ptr %36, i32 0, i32 1
  store ptr %35, ptr %37, align 8, !tbaa !11
  %38 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  store i32 402653193, ptr %38, align 4, !tbaa !12
  %39 = call i32 @s2n_calculate_stacktrace()
  br label %40

40:                                               ; preds = %34
  store i32 -1, ptr %3, align 4
  br label %77

41:                                               ; No predecessors!
  br label %42

42:                                               ; preds = %41, %8
  br label %43

43:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #6
  store i8 0, ptr %6, align 1, !tbaa !14
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 0, ptr %44, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.s2n_blob, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 8, !tbaa !17
  store i32 %47, ptr %7, align 4, !tbaa !12
  br label %48

48:                                               ; preds = %73, %43
  %49 = load i32, ptr %7, align 4, !tbaa !12
  %50 = icmp ugt i32 %49, 0
  br i1 %50, label %52, label %51

51:                                               ; preds = %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %76

52:                                               ; preds = %48
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.s2n_blob, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !19
  %56 = load i32, ptr %7, align 4, !tbaa !12
  %57 = sub i32 %56, 1
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %55, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !14
  %61 = zext i8 %60 to i64
  %62 = load i8, ptr %6, align 1, !tbaa !14
  %63 = zext i8 %62 to i32
  %64 = zext i32 %63 to i64
  %65 = shl i64 %61, %64
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = load i64, ptr %66, align 8, !tbaa !15
  %68 = add i64 %67, %65
  store i64 %68, ptr %66, align 8, !tbaa !15
  %69 = load i8, ptr %6, align 1, !tbaa !14
  %70 = zext i8 %69 to i32
  %71 = add nsw i32 %70, 8
  %72 = trunc i32 %71 to i8
  store i8 %72, ptr %6, align 1, !tbaa !14
  br label %73

73:                                               ; preds = %52
  %74 = load i32, ptr %7, align 4, !tbaa !12
  %75 = add i32 %74, -1
  store i32 %75, ptr %7, align 4, !tbaa !12
  br label %48, !llvm.loop !20

76:                                               ; preds = %51
  store i32 0, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #6
  br label %77

77:                                               ; preds = %76, %40
  %78 = load i32, ptr %3, align 4
  ret i32 %78
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nounwind willreturn memory(read) "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 #3 = { "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 #4 = { 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 #5 = { nounwind willreturn memory(read) }
attributes #6 = { 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, !6, i64 0}
!10 = !{!"s2n_debug_info", !6, i64 0, !6, i64 8}
!11 = !{!10, !6, i64 8}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!18, !13, i64 8}
!18 = !{!"s2n_blob", !6, i64 0, !13, i64 8, !13, i64 12, !13, i64 16}
!19 = !{!18, !6, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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"

%struct.s2n_debug_info = type { ptr, ptr }

@_s2n_debug_info = external thread_local local_unnamed_addr global %struct.s2n_debug_info, align 8
@s2n_errno = external thread_local local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [91 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare ptr @strrchr(ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @s2n_calculate_stacktrace() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @s2n_sequence_number_to_uint64(ptr noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #3 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %3, label %11

3:                                                ; preds = %2
  %4 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @_s2n_debug_info)
  store ptr @.str.1, ptr %4, align 8, !tbaa !5
  %5 = tail call ptr @strrchr(ptr noundef nonnull dereferenceable(1) getelementptr inbounds (i8, ptr @.str.1, i64 21), i32 noundef 47) #4
  %.not14 = icmp eq ptr %5, null
  %6 = getelementptr inbounds i8, ptr %5, i64 1
  %7 = select i1 %.not14, ptr getelementptr inbounds (i8, ptr @.str.1, i64 21), ptr %6
  %8 = getelementptr inbounds i8, ptr %4, i64 8
  store ptr %7, ptr %8, align 8, !tbaa !10
  %9 = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  store i32 402653193, ptr %9, align 4, !tbaa !11
  %10 = tail call i32 @s2n_calculate_stacktrace() #5
  br label %.loopexit

11:                                               ; preds = %2
  store i64 0, ptr %1, align 8, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load i32, ptr %12, align 8, !tbaa !15
  %.not1516 = icmp eq i32 %13, 0
  br i1 %.not1516, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %14 = load ptr, ptr %0, align 8, !tbaa !17
  %15 = zext i32 %13 to i64
  %xtraiter = and i64 %15, 1
  %16 = icmp eq i32 %13, 1
  br i1 %16, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %15, 4294967294
  %invariant.gep = getelementptr i8, ptr %14, i64 -1
  br label %17

17:                                               ; preds = %17, %.lr.ph.new
  %indvars.iv = phi i64 [ %15, %.lr.ph.new ], [ %26, %17 ]
  %18 = phi i64 [ 0, %.lr.ph.new ], [ %33, %17 ]
  %.01017 = phi i32 [ 0, %.lr.ph.new ], [ %34, %17 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %17 ]
  %gep = getelementptr i8, ptr %invariant.gep, i64 %indvars.iv
  %19 = load i8, ptr %gep, align 1, !tbaa !18
  %20 = zext i8 %19 to i64
  %21 = and i32 %.01017, 255
  %22 = zext nneg i32 %21 to i64
  %23 = shl i64 %20, %22
  %24 = add i64 %23, %18
  store i64 %24, ptr %1, align 8, !tbaa !13
  %25 = add nsw i32 %.01017, 8
  %26 = add nsw i64 %indvars.iv, -2
  %27 = getelementptr inbounds i8, ptr %14, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !18
  %29 = zext i8 %28 to i64
  %30 = and i32 %25, 255
  %31 = zext nneg i32 %30 to i64
  %32 = shl i64 %29, %31
  %33 = add i64 %32, %24
  store i64 %33, ptr %1, align 8, !tbaa !13
  %34 = add nuw nsw i32 %30, 8
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa.loopexit, label %17, !llvm.loop !19

.loopexit.loopexit.unr-lcssa.loopexit:            ; preds = %17
  %35 = and i32 %34, 255
  %36 = zext nneg i32 %35 to i64
  br label %.loopexit.loopexit.unr-lcssa

.loopexit.loopexit.unr-lcssa:                     ; preds = %.loopexit.loopexit.unr-lcssa.loopexit, %.lr.ph
  %indvars.iv.unr = phi i64 [ %15, %.lr.ph ], [ %26, %.loopexit.loopexit.unr-lcssa.loopexit ]
  %.unr = phi i64 [ 0, %.lr.ph ], [ %33, %.loopexit.loopexit.unr-lcssa.loopexit ]
  %.01017.unr = phi i64 [ 0, %.lr.ph ], [ %36, %.loopexit.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.loopexit.loopexit.epilog-lcssa

.loopexit.loopexit.epilog-lcssa:                  ; preds = %.loopexit.loopexit.unr-lcssa
  %37 = getelementptr i8, ptr %14, i64 %indvars.iv.unr
  %38 = getelementptr i8, ptr %37, i64 -1
  %39 = load i8, ptr %38, align 1, !tbaa !18
  %40 = zext i8 %39 to i64
  %41 = shl i64 %40, %.01017.unr
  %42 = add i64 %41, %.unr
  store i64 %42, ptr %1, align 8, !tbaa !13
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit.epilog-lcssa, %.loopexit.loopexit.unr-lcssa, %11, %3
  %.011 = phi i32 [ -1, %3 ], [ 0, %11 ], [ 0, %.loopexit.loopexit.unr-lcssa ], [ 0, %.loopexit.loopexit.epilog-lcssa ]
  ret i32 %.011
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { "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 #3 = { 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 #4 = { nounwind willreturn memory(read) }
attributes #5 = { 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, !7, i64 0}
!6 = !{!"s2n_debug_info", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !8, i64 0}
!15 = !{!16, !12, i64 8}
!16 = !{!"s2n_blob", !7, i64 0, !12, i64 8, !12, i64 12, !12, i64 16}
!17 = !{!16, !7, i64 0}
!18 = !{!8, !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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