; LLVM IR from kala13x/libxutils :: src/crypt (crypt)

; 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.XByteBuffer = type { ptr, i64, i64, i16, i16, i32 }

@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [1 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @XCrypt_HEX(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i8 noundef zeroext %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i8, align 1
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca %struct.XByteBuffer, align 8
  %16 = alloca i32, align 4
  %17 = alloca ptr, align 8
  %18 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !9
  store i8 %4, ptr %11, align 1, !tbaa !11
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %28, label %21

21:                                               ; preds = %5
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = icmp eq ptr %22, null
  br i1 %23, label %28, label %24

24:                                               ; preds = %21
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %29, label %28

28:                                               ; preds = %24, %21, %5
  store ptr null, ptr %6, align 8
  br label %99

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store i64 0, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = load i64, ptr %30, align 8, !tbaa !9
  store i64 %31, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #3
  %32 = load i64, ptr %14, align 8, !tbaa !9
  %33 = call i32 @XByteBuffer_Init(ptr noundef %15, i64 noundef %32, i32 noundef 0)
  %34 = getelementptr inbounds %struct.XByteBuffer, ptr %15, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !12
  %36 = icmp ne i64 %35, 0
  br i1 %36, label %38, label %37

37:                                               ; preds = %29
  store ptr null, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %98

38:                                               ; preds = %29
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %89, %38
  %40 = load i64, ptr %12, align 8, !tbaa !9
  %41 = load i64, ptr %14, align 8, !tbaa !9
  %42 = icmp ult i64 %40, %41
  br i1 %42, label %43, label %92

43:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #3
  %44 = load i8, ptr %11, align 1, !tbaa !11
  %45 = zext i8 %44 to i32
  %46 = icmp ne i32 %45, 0
  %47 = zext i1 %46 to i64
  %48 = select i1 %46, ptr @.str, ptr @.str.1
  store ptr %48, ptr %17, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #3
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = call zeroext i8 @xstrused(ptr noundef %49)
  %51 = zext i8 %50 to i32
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %55

53:                                               ; preds = %43
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  br label %56

55:                                               ; preds = %43
  br label %56

56:                                               ; preds = %55, %53
  %57 = phi ptr [ %54, %53 ], [ @.str.2, %55 ]
  store ptr %57, ptr %18, align 8, !tbaa !5
  %58 = load ptr, ptr %17, align 8, !tbaa !5
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = load i64, ptr %12, align 8, !tbaa !9
  %61 = getelementptr inbounds i8, ptr %59, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !11
  %63 = zext i8 %62 to i32
  %64 = load ptr, ptr %18, align 8, !tbaa !5
  %65 = call i32 (ptr, ptr, ...) @XByteBuffer_AddFmt(ptr noundef %15, ptr noundef %58, i32 noundef %63, ptr noundef %64)
  %66 = icmp sle i32 %65, 0
  br i1 %66, label %67, label %69

67:                                               ; preds = %56
  call void @XByteBuffer_Clear(ptr noundef %15)
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %68, align 8, !tbaa !9
  store ptr null, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %86

69:                                               ; preds = %56
  %70 = load i64, ptr %10, align 8, !tbaa !9
  %71 = icmp ne i64 %70, 0
  br i1 %71, label %73, label %72

72:                                               ; preds = %69
  store i32 4, ptr %16, align 4
  br label %86

73:                                               ; preds = %69
  %74 = load i64, ptr %13, align 8, !tbaa !9
  %75 = add i64 %74, 1
  store i64 %75, ptr %13, align 8, !tbaa !9
  %76 = load i64, ptr %13, align 8, !tbaa !9
  %77 = load i64, ptr %10, align 8, !tbaa !9
  %78 = icmp eq i64 %76, %77
  br i1 %78, label %79, label %85

79:                                               ; preds = %73
  %80 = call i32 @XByteBuffer_AddByte(ptr noundef %15, i8 noundef zeroext 10)
  %81 = icmp sle i32 %80, 0
  br i1 %81, label %82, label %84

82:                                               ; preds = %79
  call void @XByteBuffer_Clear(ptr noundef %15)
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %83, align 8, !tbaa !9
  store ptr null, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %86

84:                                               ; preds = %79
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %85

85:                                               ; preds = %84, %73
  store i32 0, ptr %16, align 4
  br label %86

86:                                               ; preds = %85, %82, %72, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #3
  %87 = load i32, ptr %16, align 4
  switch i32 %87, label %98 [
    i32 0, label %88
    i32 4, label %89
  ]

88:                                               ; preds = %86
  br label %89

89:                                               ; preds = %88, %86
  %90 = load i64, ptr %12, align 8, !tbaa !9
  %91 = add i64 %90, 1
  store i64 %91, ptr %12, align 8, !tbaa !9
  br label %39, !llvm.loop !16

92:                                               ; preds = %39
  %93 = getelementptr inbounds %struct.XByteBuffer, ptr %15, i32 0, i32 2
  %94 = load i64, ptr %93, align 8, !tbaa !18
  %95 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %94, ptr %95, align 8, !tbaa !9
  %96 = getelementptr inbounds %struct.XByteBuffer, ptr %15, i32 0, i32 0
  %97 = load ptr, ptr %96, align 8, !tbaa !19
  store ptr %97, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %98

98:                                               ; preds = %92, %86, %37
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %99

99:                                               ; preds = %98, %28
  %100 = load ptr, ptr %6, align 8
  ret ptr %100
}

declare i32 @XByteBuffer_Init(ptr noundef, i64 noundef, i32 noundef) #2

declare zeroext i8 @xstrused(ptr noundef) #2

declare i32 @XByteBuffer_AddFmt(ptr noundef, ptr noundef, ...) #2

declare void @XByteBuffer_Clear(ptr noundef) #2

declare i32 @XByteBuffer_AddByte(ptr noundef, i8 noundef zeroext) #2

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 = { "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, !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, !10, i64 8}
!13 = !{!"XByteBuffer", !6, i64 0, !10, i64 8, !10, i64 16, !14, i64 24, !14, i64 26, !15, i64 28}
!14 = !{!"short", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!13, !10, i64 16}
!19 = !{!13, !6, i64 0}


; 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.XByteBuffer = type { ptr, i64, i64, i16, i16, i32 }

@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [1 x i8], align 1

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

; Function Attrs: mustprogress 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 ptr @XCrypt_HEX(ptr noundef readonly %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i8 noundef zeroext %4) local_unnamed_addr #1 {
  %6 = alloca %struct.XByteBuffer, align 8
  %7 = icmp eq ptr %0, null
  %8 = icmp eq ptr %1, null
  %or.cond = or i1 %7, %8
  br i1 %or.cond, label %45, label %9

9:                                                ; preds = %5
  %10 = load i64, ptr %1, align 8, !tbaa !5
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %45, label %11

11:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #3
  %12 = call i32 @XByteBuffer_Init(ptr noundef nonnull %6, i64 noundef %10, i32 noundef 0) #3
  %13 = getelementptr inbounds i8, ptr %6, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !9
  %.not35 = icmp eq i64 %14, 0
  br i1 %.not35, label %44, label %.preheader

.preheader:                                       ; preds = %11
  %.not36 = icmp eq i8 %4, 0
  %15 = select i1 %.not36, ptr @.str.1, ptr @.str
  %.not38 = icmp eq i64 %3, 0
  br i1 %.not38, label %.preheader.split.us, label %.preheader.split

.preheader.split.us:                              ; preds = %.preheader, %23
  %.02443.us = phi i64 [ %24, %23 ], [ 0, %.preheader ]
  %16 = call zeroext i8 @xstrused(ptr noundef %2) #3
  %.not37.us = icmp eq i8 %16, 0
  %17 = select i1 %.not37.us, ptr @.str.2, ptr %2
  %18 = getelementptr inbounds i8, ptr %0, i64 %.02443.us
  %19 = load i8, ptr %18, align 1, !tbaa !14
  %20 = zext i8 %19 to i32
  %21 = call i32 (ptr, ptr, ...) @XByteBuffer_AddFmt(ptr noundef nonnull %6, ptr noundef nonnull %15, i32 noundef %20, ptr noundef %17) #3
  %22 = icmp slt i32 %21, 1
  br i1 %22, label %.split.us, label %23

23:                                               ; preds = %.preheader.split.us
  %24 = add nuw i64 %.02443.us, 1
  %exitcond48.not = icmp eq i64 %24, %10
  br i1 %exitcond48.not, label %.split45.us, label %.preheader.split.us, !llvm.loop !15

.preheader.split:                                 ; preds = %.preheader, %39
  %.02443 = phi i64 [ %40, %39 ], [ 0, %.preheader ]
  %.02642 = phi i64 [ %.228.ph, %39 ], [ 0, %.preheader ]
  %25 = call zeroext i8 @xstrused(ptr noundef %2) #3
  %.not37 = icmp eq i8 %25, 0
  %26 = select i1 %.not37, ptr @.str.2, ptr %2
  %27 = getelementptr inbounds i8, ptr %0, i64 %.02443
  %28 = load i8, ptr %27, align 1, !tbaa !14
  %29 = zext i8 %28 to i32
  %30 = call i32 (ptr, ptr, ...) @XByteBuffer_AddFmt(ptr noundef nonnull %6, ptr noundef nonnull %15, i32 noundef %29, ptr noundef %26) #3
  %31 = icmp slt i32 %30, 1
  br i1 %31, label %.split.us, label %32

.split.us:                                        ; preds = %.preheader.split, %.preheader.split.us
  call void @XByteBuffer_Clear(ptr noundef nonnull %6) #3
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %44

32:                                               ; preds = %.preheader.split
  %33 = add i64 %.02642, 1
  %34 = icmp eq i64 %33, %3
  br i1 %34, label %35, label %39

35:                                               ; preds = %32
  %36 = call i32 @XByteBuffer_AddByte(ptr noundef nonnull %6, i8 noundef zeroext 10) #3
  %37 = icmp slt i32 %36, 1
  br i1 %37, label %38, label %39

38:                                               ; preds = %35
  call void @XByteBuffer_Clear(ptr noundef nonnull %6) #3
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %44

39:                                               ; preds = %35, %32
  %.228.ph = phi i64 [ 0, %35 ], [ %33, %32 ]
  %40 = add nuw i64 %.02443, 1
  %exitcond.not = icmp eq i64 %40, %10
  br i1 %exitcond.not, label %.split45.us, label %.preheader.split, !llvm.loop !15

.split45.us:                                      ; preds = %39, %23
  %41 = getelementptr inbounds i8, ptr %6, i64 16
  %42 = load i64, ptr %41, align 8, !tbaa !17
  store i64 %42, ptr %1, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !18
  br label %44

44:                                               ; preds = %.split.us, %38, %11, %.split45.us
  %.2 = phi ptr [ %43, %.split45.us ], [ null, %11 ], [ null, %38 ], [ null, %.split.us ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #3
  br label %45

45:                                               ; preds = %5, %9, %44
  %.3 = phi ptr [ %.2, %44 ], [ null, %9 ], [ null, %5 ]
  ret ptr %.3
}

declare i32 @XByteBuffer_Init(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #2

declare zeroext i8 @xstrused(ptr noundef) local_unnamed_addr #2

declare i32 @XByteBuffer_AddFmt(ptr noundef, ptr noundef, ...) local_unnamed_addr #2

declare void @XByteBuffer_Clear(ptr noundef) local_unnamed_addr #2

declare i32 @XByteBuffer_AddByte(ptr noundef, i8 noundef zeroext) local_unnamed_addr #2

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass