; LLVM IR from kala13x/libxutils :: src/net (ws)

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

; 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
declare dso_local i64 @XWebFrame_GetPayloadLength(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XWebFrame_Unmask(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.xweb_frame_, ptr %9, i32 0, i32 8
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = icmp ne i8 %11, 0
  br i1 %12, label %14, label %13

13:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  br label %56

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = call i64 @XWebFrame_GetPayloadLength(ptr noundef %15)
  store i64 %16, ptr %5, align 8, !tbaa !15
  %17 = load i64, ptr %5, align 8, !tbaa !15
  %18 = icmp ne i64 %17, 0
  br i1 %18, label %20, label %19

19:                                               ; preds = %14
  store i32 9, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %55

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.xweb_frame_, ptr %21, i32 0, i32 1
  %23 = getelementptr inbounds %struct.XByteBuffer, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.xweb_frame_, ptr %25, i32 0, i32 3
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = getelementptr inbounds i8, ptr %24, i64 %27
  store ptr %28, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.xweb_frame_, ptr %29, i32 0, i32 4
  store ptr %30, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %4, align 8, !tbaa !15
  br label %31

31:                                               ; preds = %49, %20
  %32 = load i64, ptr %4, align 8, !tbaa !15
  %33 = load i64, ptr %5, align 8, !tbaa !15
  %34 = icmp ult i64 %32, %33
  br i1 %34, label %35, label %52

35:                                               ; preds = %31
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = load i64, ptr %4, align 8, !tbaa !15
  %38 = urem i64 %37, 4
  %39 = getelementptr inbounds i8, ptr %36, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !18
  %41 = zext i8 %40 to i32
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load i64, ptr %4, align 8, !tbaa !15
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !18
  %46 = zext i8 %45 to i32
  %47 = xor i32 %46, %41
  %48 = trunc i32 %47 to i8
  store i8 %48, ptr %44, align 1, !tbaa !18
  br label %49

49:                                               ; preds = %35
  %50 = load i64, ptr %4, align 8, !tbaa !15
  %51 = add i64 %50, 1
  store i64 %51, ptr %4, align 8, !tbaa !15
  br label %31, !llvm.loop !19

52:                                               ; preds = %31
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.xweb_frame_, ptr %53, i32 0, i32 8
  store i8 0, ptr %54, align 1, !tbaa !9
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %55

55:                                               ; preds = %52, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %56

56:                                               ; preds = %55, %13
  %57 = load i32, ptr %2, align 4
  ret i32 %57
}

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, !7, i64 63}
!10 = !{!"xweb_frame_", !11, i64 0, !12, i64 8, !13, i64 40, !13, i64 48, !11, i64 56, !7, i64 60, !7, i64 61, !7, i64 62, !7, i64 63, !7, i64 64}
!11 = !{!"int", !7, i64 0}
!12 = !{!"XByteBuffer", !6, i64 0, !13, i64 8, !13, i64 16, !14, i64 24, !14, i64 26, !11, i64 28}
!13 = !{!"long", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!10, !6, i64 8}
!17 = !{!10, !13, i64 48}
!18 = !{!7, !7, 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 i64 @XWebFrame_GetPayloadLength(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 10) i32 @XWebFrame_Unmask(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 63
  %3 = load i8, ptr %2, align 1, !tbaa !5
  %.not = icmp eq i8 %3, 0
  br i1 %.not, label %30, label %4

4:                                                ; preds = %1
  %5 = tail call i64 @XWebFrame_GetPayloadLength(ptr noundef nonnull %0)
  %.not16 = icmp eq i64 %5, 0
  br i1 %.not16, label %30, label %iter.check

iter.check:                                       ; preds = %4
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !14
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  %9 = load i64, ptr %8, align 8, !tbaa !15
  %10 = getelementptr inbounds i8, ptr %7, i64 %9
  %11 = getelementptr inbounds i8, ptr %0, i64 56
  %xtraiter = and i64 %5, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %vec.epilog.scalar.ph.prol.loopexit, label %vec.epilog.scalar.ph.prol

vec.epilog.scalar.ph.prol:                        ; preds = %iter.check
  %12 = load i8, ptr %11, align 1, !tbaa !16
  %13 = load i8, ptr %10, align 1, !tbaa !16
  %14 = xor i8 %13, %12
  store i8 %14, ptr %10, align 1, !tbaa !16
  br label %vec.epilog.scalar.ph.prol.loopexit

vec.epilog.scalar.ph.prol.loopexit:               ; preds = %vec.epilog.scalar.ph.prol, %iter.check
  %.01417.unr = phi i64 [ 0, %iter.check ], [ 1, %vec.epilog.scalar.ph.prol ]
  %15 = icmp eq i64 %5, 1
  br i1 %15, label %.loopexit, label %vec.epilog.scalar.ph

vec.epilog.scalar.ph:                             ; preds = %vec.epilog.scalar.ph.prol.loopexit, %vec.epilog.scalar.ph
  %.01417 = phi i64 [ %29, %vec.epilog.scalar.ph ], [ %.01417.unr, %vec.epilog.scalar.ph.prol.loopexit ]
  %16 = and i64 %.01417, 3
  %17 = getelementptr inbounds i8, ptr %11, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !16
  %19 = getelementptr inbounds i8, ptr %10, i64 %.01417
  %20 = load i8, ptr %19, align 1, !tbaa !16
  %21 = xor i8 %20, %18
  store i8 %21, ptr %19, align 1, !tbaa !16
  %22 = add nuw i64 %.01417, 1
  %23 = and i64 %22, 3
  %24 = getelementptr inbounds i8, ptr %11, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !16
  %26 = getelementptr inbounds i8, ptr %10, i64 %22
  %27 = load i8, ptr %26, align 1, !tbaa !16
  %28 = xor i8 %27, %25
  store i8 %28, ptr %26, align 1, !tbaa !16
  %29 = add nuw i64 %.01417, 2
  %exitcond.not.1 = icmp eq i64 %29, %5
  br i1 %exitcond.not.1, label %.loopexit, label %vec.epilog.scalar.ph, !llvm.loop !17

.loopexit:                                        ; preds = %vec.epilog.scalar.ph, %vec.epilog.scalar.ph.prol.loopexit
  store i8 0, ptr %2, align 1, !tbaa !5
  br label %30

30:                                               ; preds = %.loopexit, %4, %1
  %.1 = phi i32 [ 0, %1 ], [ 0, %.loopexit ], [ 9, %4 ]
  ret i32 %.1
}

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, !8, i64 63}
!6 = !{!"xweb_frame_", !7, i64 0, !10, i64 8, !12, i64 40, !12, i64 48, !7, i64 56, !8, i64 60, !8, i64 61, !8, i64 62, !8, i64 63, !8, i64 64}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"XByteBuffer", !11, i64 0, !12, i64 8, !12, i64 16, !13, i64 24, !13, i64 26, !7, i64 28}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!"short", !8, i64 0}
!14 = !{!6, !11, i64 8}
!15 = !{!6, !12, i64 48}
!16 = !{!8, !8, i64 0}
!17 = distinct !{!17, !18, !19}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopLoadEliminationPass,LoopRotatePass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass