; LLVM IR from aws/s2n-tls :: stuffer (s2n_stuffer_hex)

; 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_result = type { i32 }
%struct.s2n_blob = type { ptr, i32, i32, i8 }
%struct.s2n_stuffer = type { %struct.s2n_blob, i32, i32, i32, i8 }

@value_to_hex = external hidden constant [16 x i8], align 16

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @s2n_result_is_ok(i32) #0

declare i32 @s2n_stuffer_validate(ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #2

declare i32 @s2n_blob_validate(ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_stuffer_write_hex(ptr noundef %0, ptr noundef %1) #4 {
  %3 = alloca %struct.s2n_result, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.s2n_result, align 4
  %7 = alloca %struct.s2n_result, align 4
  %8 = alloca %struct.s2n_result, align 4
  %9 = alloca %struct.s2n_result, align 4
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = call i32 @s2n_stuffer_validate(ptr noundef %17)
  %19 = getelementptr inbounds %struct.s2n_result, ptr %7, i32 0, i32 0
  store i32 %18, ptr %19, align 4
  %20 = getelementptr inbounds %struct.s2n_result, ptr %7, i32 0, i32 0
  %21 = load i32, ptr %20, align 4
  %22 = call zeroext i1 @s2n_result_is_ok(i32 %21)
  %23 = xor i1 %22, true
  %24 = xor i1 %23, true
  %25 = zext i1 %24 to i32
  %26 = sext i32 %25 to i64
  %27 = call i64 @llvm.expect.i64(i64 %26, i64 1)
  %28 = icmp ne i64 %27, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %16
  %30 = getelementptr inbounds %struct.s2n_result, ptr %6, i32 0, i32 0
  store i32 0, ptr %30, align 4, !tbaa !9
  br label %33

31:                                               ; preds = %16
  %32 = getelementptr inbounds %struct.s2n_result, ptr %6, i32 0, i32 0
  store i32 -1, ptr %32, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %31, %29
  %34 = getelementptr inbounds %struct.s2n_result, ptr %6, i32 0, i32 0
  %35 = load i32, ptr %34, align 4
  %36 = call zeroext i1 @s2n_result_is_ok(i32 %35)
  br i1 %36, label %39, label %37

37:                                               ; preds = %33
  %38 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  store i32 -1, ptr %38, align 4, !tbaa !9
  br label %146

39:                                               ; preds = %33
  br label %40

40:                                               ; preds = %39
  br label %41

41:                                               ; preds = %40
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = call i32 @s2n_blob_validate(ptr noundef %42)
  %44 = getelementptr inbounds %struct.s2n_result, ptr %9, i32 0, i32 0
  store i32 %43, ptr %44, align 4
  %45 = getelementptr inbounds %struct.s2n_result, ptr %9, i32 0, i32 0
  %46 = load i32, ptr %45, align 4
  %47 = call zeroext i1 @s2n_result_is_ok(i32 %46)
  %48 = xor i1 %47, true
  %49 = xor i1 %48, true
  %50 = zext i1 %49 to i32
  %51 = sext i32 %50 to i64
  %52 = call i64 @llvm.expect.i64(i64 %51, i64 1)
  %53 = icmp ne i64 %52, 0
  br i1 %53, label %54, label %56

54:                                               ; preds = %41
  %55 = getelementptr inbounds %struct.s2n_result, ptr %8, i32 0, i32 0
  store i32 0, ptr %55, align 4, !tbaa !9
  br label %58

56:                                               ; preds = %41
  %57 = getelementptr inbounds %struct.s2n_result, ptr %8, i32 0, i32 0
  store i32 -1, ptr %57, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %56, %54
  %59 = getelementptr inbounds %struct.s2n_result, ptr %8, i32 0, i32 0
  %60 = load i32, ptr %59, align 4
  %61 = call zeroext i1 @s2n_result_is_ok(i32 %60)
  br i1 %61, label %64, label %62

62:                                               ; preds = %58
  %63 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  store i32 -1, ptr %63, align 4, !tbaa !9
  br label %146

64:                                               ; preds = %58
  br label %65

65:                                               ; preds = %64
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.s2n_blob, ptr %66, i32 0, i32 1
  %68 = load i32, ptr %67, align 8, !tbaa !12
  %69 = zext i32 %68 to i64
  store i64 %69, ptr %10, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %70 = load i64, ptr %10, align 8, !tbaa !14
  %71 = mul i64 %70, 2
  store i64 %71, ptr %11, align 8, !tbaa !14
  br label %72

72:                                               ; preds = %65
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = load i64, ptr %11, align 8, !tbaa !14
  %75 = trunc i64 %74 to i32
  %76 = call i32 @s2n_stuffer_reserve_space(ptr noundef %73, i32 noundef %75)
  %77 = icmp sgt i32 %76, -1
  br i1 %77, label %80, label %78

78:                                               ; preds = %72
  %79 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  store i32 -1, ptr %79, align 4, !tbaa !9
  store i32 1, ptr %12, align 4
  br label %145

80:                                               ; preds = %72
  br label %81

81:                                               ; preds = %80
  br label %82

82:                                               ; preds = %81
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.s2n_stuffer, ptr %83, i32 0, i32 0
  %85 = getelementptr inbounds %struct.s2n_blob, ptr %84, i32 0, i32 0
  %86 = load ptr, ptr %85, align 8, !tbaa !16
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.s2n_stuffer, ptr %87, i32 0, i32 2
  %89 = load i32, ptr %88, align 4, !tbaa !18
  %90 = zext i32 %89 to i64
  %91 = getelementptr inbounds i8, ptr %86, i64 %90
  store ptr %91, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %92 = load ptr, ptr %5, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.s2n_blob, ptr %92, i32 0, i32 0
  %94 = load ptr, ptr %93, align 8, !tbaa !19
  store ptr %94, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  store i64 0, ptr %15, align 8, !tbaa !14
  br label %95

95:                                               ; preds = %128, %82
  %96 = load i64, ptr %15, align 8, !tbaa !14
  %97 = load i64, ptr %10, align 8, !tbaa !14
  %98 = icmp ult i64 %96, %97
  br i1 %98, label %100, label %99

99:                                               ; preds = %95
  store i32 8, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  br label %131

100:                                              ; preds = %95
  %101 = load ptr, ptr %14, align 8, !tbaa !5
  %102 = load i64, ptr %15, align 8, !tbaa !14
  %103 = getelementptr inbounds i8, ptr %101, i64 %102
  %104 = load i8, ptr %103, align 1, !tbaa !20
  %105 = zext i8 %104 to i32
  %106 = ashr i32 %105, 4
  %107 = sext i32 %106 to i64
  %108 = getelementptr inbounds [16 x i8], ptr @value_to_hex, i64 0, i64 %107
  %109 = load i8, ptr %108, align 1, !tbaa !20
  %110 = load ptr, ptr %13, align 8, !tbaa !5
  %111 = load i64, ptr %15, align 8, !tbaa !14
  %112 = mul i64 %111, 2
  %113 = getelementptr inbounds i8, ptr %110, i64 %112
  store i8 %109, ptr %113, align 1, !tbaa !20
  %114 = load ptr, ptr %14, align 8, !tbaa !5
  %115 = load i64, ptr %15, align 8, !tbaa !14
  %116 = getelementptr inbounds i8, ptr %114, i64 %115
  %117 = load i8, ptr %116, align 1, !tbaa !20
  %118 = zext i8 %117 to i32
  %119 = and i32 %118, 15
  %120 = sext i32 %119 to i64
  %121 = getelementptr inbounds [16 x i8], ptr @value_to_hex, i64 0, i64 %120
  %122 = load i8, ptr %121, align 1, !tbaa !20
  %123 = load ptr, ptr %13, align 8, !tbaa !5
  %124 = load i64, ptr %15, align 8, !tbaa !14
  %125 = mul i64 %124, 2
  %126 = add i64 %125, 1
  %127 = getelementptr inbounds i8, ptr %123, i64 %126
  store i8 %122, ptr %127, align 1, !tbaa !20
  br label %128

128:                                              ; preds = %100
  %129 = load i64, ptr %15, align 8, !tbaa !14
  %130 = add i64 %129, 1
  store i64 %130, ptr %15, align 8, !tbaa !14
  br label %95, !llvm.loop !21

131:                                              ; preds = %99
  br label %132

132:                                              ; preds = %131
  %133 = load ptr, ptr %4, align 8, !tbaa !5
  %134 = load i64, ptr %11, align 8, !tbaa !14
  %135 = trunc i64 %134 to i32
  %136 = call i32 @s2n_stuffer_skip_write(ptr noundef %133, i32 noundef %135)
  %137 = icmp sgt i32 %136, -1
  br i1 %137, label %140, label %138

138:                                              ; preds = %132
  %139 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  store i32 -1, ptr %139, align 4, !tbaa !9
  store i32 1, ptr %12, align 4
  br label %144

140:                                              ; preds = %132
  br label %141

141:                                              ; preds = %140
  br label %142

142:                                              ; preds = %141
  %143 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  store i32 0, ptr %143, align 4, !tbaa !9
  store i32 1, ptr %12, align 4
  br label %144

144:                                              ; preds = %142, %138
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  br label %145

145:                                              ; preds = %144, %78
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %146

146:                                              ; preds = %145, %62, %37
  %147 = getelementptr inbounds %struct.s2n_result, ptr %3, i32 0, i32 0
  %148 = load i32, ptr %147, align 4
  ret i32 %148
}

declare i32 @s2n_stuffer_reserve_space(ptr noundef, i32 noundef) #1

declare i32 @s2n_stuffer_skip_write(ptr noundef, i32 noundef) #1

attributes #0 = { inlinehint 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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

!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, !11, i64 0}
!10 = !{!"", !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !11, i64 8}
!13 = !{!"s2n_blob", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !6, i64 0}
!17 = !{!"s2n_stuffer", !13, i64 0, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 36, !11, i64 36}
!18 = !{!17, !11, i64 28}
!19 = !{!13, !6, i64 0}
!20 = !{!7, !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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"

@value_to_hex = external hidden local_unnamed_addr constant [16 x i8], align 16

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @s2n_result_is_ok(i32) local_unnamed_addr #0

declare i32 @s2n_stuffer_validate(ptr noundef) local_unnamed_addr #1

declare i32 @s2n_blob_validate(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @s2n_stuffer_write_hex(ptr noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = tail call i32 @s2n_stuffer_validate(ptr noundef %0) #3
  %4 = tail call zeroext i1 @s2n_result_is_ok(i32 %3)
  %not. = xor i1 %4, true
  %. = sext i1 %not. to i32
  %5 = tail call zeroext i1 @s2n_result_is_ok(i32 %.)
  br i1 %5, label %6, label %41

6:                                                ; preds = %2
  %7 = tail call i32 @s2n_blob_validate(ptr noundef %1) #3
  %8 = tail call zeroext i1 @s2n_result_is_ok(i32 %7)
  %not.27 = xor i1 %8, true
  %.25 = sext i1 %not.27 to i32
  %9 = tail call zeroext i1 @s2n_result_is_ok(i32 %.25)
  br i1 %9, label %10, label %41

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %1, i64 8
  %12 = load i32, ptr %11, align 8, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = shl i32 %12, 1
  %15 = tail call i32 @s2n_stuffer_reserve_space(ptr noundef %0, i32 noundef %14) #3
  %16 = icmp sgt i32 %15, -1
  br i1 %16, label %17, label %41

17:                                               ; preds = %10
  %18 = load ptr, ptr %0, align 8, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %0, i64 28
  %20 = load i32, ptr %19, align 4, !tbaa !13
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %18, i64 %21
  %23 = load ptr, ptr %1, align 8, !tbaa !14
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %17
  %24 = tail call i32 @s2n_stuffer_skip_write(ptr noundef nonnull %0, i32 noundef %14) #3
  %.lobit = ashr i32 %24, 31
  br label %41

.lr.ph:                                           ; preds = %17, %.lr.ph
  %.029 = phi i64 [ %40, %.lr.ph ], [ 0, %17 ]
  %25 = getelementptr inbounds i8, ptr %23, i64 %.029
  %26 = load i8, ptr %25, align 1, !tbaa !15
  %27 = lshr i8 %26, 4
  %28 = zext nneg i8 %27 to i64
  %29 = getelementptr inbounds [16 x i8], ptr @value_to_hex, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !15
  %31 = shl nuw nsw i64 %.029, 1
  %32 = getelementptr inbounds i8, ptr %22, i64 %31
  store i8 %30, ptr %32, align 1, !tbaa !15
  %33 = load i8, ptr %25, align 1, !tbaa !15
  %34 = and i8 %33, 15
  %35 = zext nneg i8 %34 to i64
  %36 = getelementptr inbounds [16 x i8], ptr @value_to_hex, i64 0, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !15
  %38 = or disjoint i64 %31, 1
  %39 = getelementptr inbounds i8, ptr %22, i64 %38
  store i8 %37, ptr %39, align 1, !tbaa !15
  %40 = add nuw nsw i64 %.029, 1
  %exitcond.not = icmp eq i64 %40, %13
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

41:                                               ; preds = %._crit_edge, %10, %6, %2
  %.sroa.024.2 = phi i32 [ -1, %2 ], [ -1, %6 ], [ %.lobit, %._crit_edge ], [ -1, %10 ]
  ret i32 %.sroa.024.2
}

declare i32 @s2n_stuffer_reserve_space(ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @s2n_stuffer_skip_write(ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { inlinehint 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 = { "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 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 #3 = { 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, !10, i64 8}
!6 = !{!"s2n_blob", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"s2n_stuffer", !6, i64 0, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 36, !10, i64 36}
!13 = !{!12, !10, i64 28}
!14 = !{!6, !7, i64 0}
!15 = !{!8, !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,SROAPass,SimplifyCFGPass,TailCallElimPass