; LLVM IR from aws/s2n-tls :: utils (s2n_blob)

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @s2n_blob_validate(ptr noundef) #0

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

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

; 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_blob_char_to_lower(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %struct.s2n_result, align 4
  %5 = alloca %struct.s2n_result, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca %struct.s2n_result, align 4
  %10 = alloca %struct.s2n_result, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = call i32 @s2n_blob_validate(ptr noundef %12)
  %14 = getelementptr inbounds %struct.s2n_result, ptr %5, i32 0, i32 0
  store i32 %13, ptr %14, align 4
  %15 = getelementptr inbounds %struct.s2n_result, ptr %5, i32 0, i32 0
  %16 = load i32, ptr %15, align 4
  %17 = call zeroext i1 @s2n_result_is_ok(i32 %16)
  %18 = xor i1 %17, true
  %19 = xor i1 %18, true
  %20 = zext i1 %19 to i32
  %21 = sext i32 %20 to i64
  %22 = call i64 @llvm.expect.i64(i64 %21, i64 1)
  %23 = icmp ne i64 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %11
  %25 = getelementptr inbounds %struct.s2n_result, ptr %4, i32 0, i32 0
  store i32 0, ptr %25, align 4, !tbaa !9
  br label %28

26:                                               ; preds = %11
  %27 = getelementptr inbounds %struct.s2n_result, ptr %4, i32 0, i32 0
  store i32 -1, ptr %27, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %26, %24
  %29 = getelementptr inbounds %struct.s2n_result, ptr %4, i32 0, i32 0
  %30 = load i32, ptr %29, align 4
  %31 = call zeroext i1 @s2n_result_is_ok(i32 %30)
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  store i32 -1, ptr %2, align 4
  br label %92

33:                                               ; preds = %28
  br label %34

34:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 0, ptr %6, align 8, !tbaa !12
  br label %35

35:                                               ; preds = %64, %34
  %36 = load i64, ptr %6, align 8, !tbaa !12
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.s2n_blob, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 8, !tbaa !14
  %40 = zext i32 %39 to i64
  %41 = icmp ult i64 %36, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %67

43:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %44 = call ptr @__ctype_tolower_loc() #6
  %45 = load ptr, ptr %44, align 8, !tbaa !5
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.s2n_blob, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !16
  %49 = load i64, ptr %6, align 8, !tbaa !12
  %50 = getelementptr inbounds i8, ptr %48, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !17
  %52 = zext i8 %51 to i32
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i32, ptr %45, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !18
  store i32 %55, ptr %7, align 4, !tbaa !18
  %56 = load i32, ptr %7, align 4, !tbaa !18
  store i32 %56, ptr %8, align 4, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  %57 = load i32, ptr %8, align 4, !tbaa !18
  %58 = trunc i32 %57 to i8
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.s2n_blob, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !16
  %62 = load i64, ptr %6, align 8, !tbaa !12
  %63 = getelementptr inbounds i8, ptr %61, i64 %62
  store i8 %58, ptr %63, align 1, !tbaa !17
  br label %64

64:                                               ; preds = %43
  %65 = load i64, ptr %6, align 8, !tbaa !12
  %66 = add i64 %65, 1
  store i64 %66, ptr %6, align 8, !tbaa !12
  br label %35, !llvm.loop !19

67:                                               ; preds = %42
  br label %68

68:                                               ; preds = %67
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = call i32 @s2n_blob_validate(ptr noundef %69)
  %71 = getelementptr inbounds %struct.s2n_result, ptr %10, i32 0, i32 0
  store i32 %70, ptr %71, align 4
  %72 = getelementptr inbounds %struct.s2n_result, ptr %10, i32 0, i32 0
  %73 = load i32, ptr %72, align 4
  %74 = call zeroext i1 @s2n_result_is_ok(i32 %73)
  %75 = xor i1 %74, true
  %76 = xor i1 %75, true
  %77 = zext i1 %76 to i32
  %78 = sext i32 %77 to i64
  %79 = call i64 @llvm.expect.i64(i64 %78, i64 1)
  %80 = icmp ne i64 %79, 0
  br i1 %80, label %81, label %83

81:                                               ; preds = %68
  %82 = getelementptr inbounds %struct.s2n_result, ptr %9, i32 0, i32 0
  store i32 0, ptr %82, align 4, !tbaa !9
  br label %85

83:                                               ; preds = %68
  %84 = getelementptr inbounds %struct.s2n_result, ptr %9, i32 0, i32 0
  store i32 -1, ptr %84, align 4, !tbaa !9
  br label %85

85:                                               ; preds = %83, %81
  %86 = getelementptr inbounds %struct.s2n_result, ptr %9, i32 0, i32 0
  %87 = load i32, ptr %86, align 4
  %88 = call zeroext i1 @s2n_result_is_ok(i32 %87)
  br i1 %88, label %90, label %89

89:                                               ; preds = %85
  store i32 -1, ptr %2, align 4
  br label %92

90:                                               ; preds = %85
  br label %91

91:                                               ; preds = %90
  store i32 0, ptr %2, align 4
  br label %92

92:                                               ; preds = %91, %89, %32
  %93 = load i32, ptr %2, align 4
  ret i32 %93
}

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_tolower_loc() #4

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

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

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

.preheader:                                       ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %7 = tail call ptr @__ctype_tolower_loc() #3
  br label %11

._crit_edge:                                      ; preds = %11, %.preheader
  %8 = tail call i32 @s2n_blob_validate(ptr noundef nonnull %0)
  %9 = tail call zeroext i1 @s2n_result_is_ok(i32 %8)
  %not.16 = xor i1 %9, true
  %.15 = sext i1 %not.16 to i32
  %10 = tail call zeroext i1 @s2n_result_is_ok(i32 %.15)
  %not.17 = xor i1 %10, true
  %spec.select = sext i1 %not.17 to i32
  br label %24

11:                                               ; preds = %.lr.ph, %11
  %.01418 = phi i64 [ 0, %.lr.ph ], [ %20, %11 ]
  %12 = load ptr, ptr %7, align 8, !tbaa !11
  %13 = load ptr, ptr %0, align 8, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %13, i64 %.01418
  %15 = load i8, ptr %14, align 1, !tbaa !13
  %16 = zext i8 %15 to i64
  %17 = getelementptr inbounds i32, ptr %12, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !14
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %14, align 1, !tbaa !13
  %20 = add nuw nsw i64 %.01418, 1
  %21 = load i32, ptr %5, align 8, !tbaa !5
  %22 = zext i32 %21 to i64
  %23 = icmp ult i64 %20, %22
  br i1 %23, label %11, label %._crit_edge, !llvm.loop !15

24:                                               ; preds = %._crit_edge, %1
  %.0 = phi i32 [ -1, %1 ], [ %spec.select, %._crit_edge ]
  ret i32 %.0
}

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_tolower_loc() local_unnamed_addr #2

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 = { 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 #2 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 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, !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 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!8, !8, i64 0}
!14 = !{!10, !10, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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