; LLVM IR from LoupVaillant/Monocypher-Utils :: src (utils)

; 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: 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 @read_buffer(ptr noundef %0, i64 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = call i64 @string_length(ptr noundef %14)
  store i64 %15, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %16 = load i64, ptr %8, align 8, !tbaa !9
  %17 = udiv i64 %16, 2
  store i64 %17, ptr %9, align 8, !tbaa !9
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %76

21:                                               ; preds = %3
  %22 = load i64, ptr %8, align 8, !tbaa !9
  %23 = load i64, ptr %6, align 8, !tbaa !9
  %24 = icmp ugt i64 %22, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i32 -2, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %76

26:                                               ; preds = %21
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = urem i64 %27, 2
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  store i32 -3, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %76

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %68, %31
  %33 = load i64, ptr %11, align 8, !tbaa !9
  %34 = load i64, ptr %8, align 8, !tbaa !9
  %35 = icmp ult i64 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  store i32 2, ptr %10, align 4
  br label %71

37:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i64, ptr %11, align 8, !tbaa !9
  %40 = getelementptr inbounds i8, ptr %38, i64 %39
  %41 = load i8, ptr %40, align 1, !tbaa !11
  %42 = call i32 @int_of_hex(i8 noundef signext %41)
  store i32 %42, ptr %12, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = load i64, ptr %11, align 8, !tbaa !9
  %45 = add i64 %44, 1
  %46 = getelementptr inbounds i8, ptr %43, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = call i32 @int_of_hex(i8 noundef signext %47)
  store i32 %48, ptr %13, align 4, !tbaa !12
  %49 = load i32, ptr %12, align 4, !tbaa !12
  %50 = icmp eq i32 %49, -1
  br i1 %50, label %54, label %51

51:                                               ; preds = %37
  %52 = load i32, ptr %13, align 4, !tbaa !12
  %53 = icmp eq i32 %52, -1
  br i1 %53, label %54, label %55

54:                                               ; preds = %51, %37
  store i32 -4, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %65

55:                                               ; preds = %51
  %56 = load i32, ptr %13, align 4, !tbaa !12
  %57 = load i32, ptr %12, align 4, !tbaa !12
  %58 = shl i32 %57, 4
  %59 = add nsw i32 %56, %58
  %60 = trunc i32 %59 to i8
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = load i64, ptr %11, align 8, !tbaa !9
  %63 = udiv i64 %62, 2
  %64 = getelementptr inbounds i8, ptr %61, i64 %63
  store i8 %60, ptr %64, align 1, !tbaa !11
  store i32 0, ptr %10, align 4
  br label %65

65:                                               ; preds = %55, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  %66 = load i32, ptr %10, align 4
  switch i32 %66, label %71 [
    i32 0, label %67
  ]

67:                                               ; preds = %65
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %11, align 8, !tbaa !9
  %70 = add i64 %69, 2
  store i64 %70, ptr %11, align 8, !tbaa !9
  br label %32, !llvm.loop !14

71:                                               ; preds = %65, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  %72 = load i32, ptr %10, align 4
  switch i32 %72, label %76 [
    i32 2, label %73
  ]

73:                                               ; preds = %71
  %74 = load i64, ptr %9, align 8, !tbaa !9
  %75 = trunc i64 %74 to i32
  store i32 %75, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %76

76:                                               ; preds = %73, %71, %30, %25, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %77 = load i32, ptr %4, align 4
  ret i32 %77
}

; Function Attrs: nounwind uwtable
declare hidden i64 @string_length(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @int_of_hex(i8 noundef signext) #1

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 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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
define dso_local i32 @read_buffer(ptr nocapture noundef writeonly %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = tail call i64 @string_length(ptr noundef %2)
  %5 = lshr i64 %4, 1
  %6 = icmp eq ptr %2, null
  br i1 %6, label %.loopexit, label %7

7:                                                ; preds = %3
  %8 = icmp ugt i64 %4, %1
  br i1 %8, label %.loopexit, label %9

9:                                                ; preds = %7
  %10 = and i64 %4, 1
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %9
  %.not3334.not = icmp eq i64 %4, 0
  br i1 %.not3334.not, label %.critedge.thread, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %20
  %.02635 = phi i64 [ %26, %20 ], [ 0, %.preheader ]
  %11 = getelementptr inbounds i8, ptr %2, i64 %.02635
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %13 = tail call i32 @int_of_hex(i8 noundef signext %12)
  %14 = or disjoint i64 %.02635, 1
  %15 = getelementptr inbounds i8, ptr %2, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = tail call i32 @int_of_hex(i8 noundef signext %16)
  %18 = icmp eq i32 %13, -1
  %19 = icmp eq i32 %17, -1
  %or.cond.not.not = select i1 %18, i1 true, i1 %19
  br i1 %or.cond.not.not, label %.loopexit, label %20

20:                                               ; preds = %.lr.ph
  %21 = shl i32 %13, 4
  %22 = add nsw i32 %17, %21
  %23 = trunc i32 %22 to i8
  %24 = lshr exact i64 %.02635, 1
  %25 = getelementptr inbounds i8, ptr %0, i64 %24
  store i8 %23, ptr %25, align 1, !tbaa !5
  %26 = add i64 %.02635, 2
  %.not33 = icmp ult i64 %26, %4
  br i1 %.not33, label %.lr.ph, label %.critedge.thread, !llvm.loop !8

.critedge.thread:                                 ; preds = %20, %.preheader
  %27 = trunc i64 %5 to i32
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.critedge.thread, %9, %7, %3
  %.3 = phi i32 [ -1, %3 ], [ -2, %7 ], [ -3, %9 ], [ %27, %.critedge.thread ], [ -4, %.lr.ph ]
  ret i32 %.3
}

; Function Attrs: nounwind uwtable
declare hidden i64 @string_length(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @int_of_hex(i8 noundef signext) local_unnamed_addr #0

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

!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: ADCEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass