; LLVM IR from asllop/AppOS :: core/mem (mem_internal)

; 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 void @internal_free(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %1
  br label %40

10:                                               ; preds = %1
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 -2
  store ptr %12, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #2
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = load i16, ptr %13, align 2, !tbaa !9
  store i16 %14, ptr %3, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %15 = load i16, ptr %3, align 2, !tbaa !9
  %16 = zext i16 %15 to i32
  %17 = sub nsw i32 %16, 1
  %18 = trunc i32 %17 to i16
  store i16 %18, ptr %4, align 2, !tbaa !9
  br label %19

19:                                               ; preds = %38, %10
  br label %20

20:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = load i16, ptr %4, align 2, !tbaa !9
  %23 = zext i16 %22 to i32
  %24 = mul nsw i32 256, %23
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %21, i64 %25
  store ptr %26, ptr %5, align 8, !tbaa !5
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  store i16 0, ptr %27, align 2, !tbaa !9
  %28 = load i16, ptr %4, align 2, !tbaa !9
  %29 = zext i16 %28 to i32
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %20
  store i32 3, ptr %6, align 4
  br label %36

32:                                               ; preds = %20
  %33 = load i16, ptr %4, align 2, !tbaa !9
  %34 = add i16 %33, -1
  store i16 %34, ptr %4, align 2, !tbaa !9
  br label %35

35:                                               ; preds = %32
  store i32 0, ptr %6, align 4
  br label %36

36:                                               ; preds = %35, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %37 = load i32, ptr %6, align 4
  switch i32 %37, label %41 [
    i32 0, label %38
    i32 3, label %39
  ]

38:                                               ; preds = %36
  br label %19

39:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #2
  br label %40

40:                                               ; preds = %39, %9
  ret void

41:                                               ; preds = %36
  unreachable
}

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 = !{!"short", !7, 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"

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @internal_free(ptr noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %2

2:                                                ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 -2
  %4 = load i16, ptr %3, align 2, !tbaa !5
  %5 = add i16 %4, -1
  %xtraiter = and i16 %4, 3
  %lcmp.mod.not = icmp eq i16 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %2, %.prol.preheader
  %.09.in.prol = phi i16 [ %.09.prol, %.prol.preheader ], [ %4, %2 ]
  %prol.iter = phi i16 [ %prol.iter.next, %.prol.preheader ], [ 0, %2 ]
  %.09.prol = add i16 %.09.in.prol, -1
  %6 = zext i16 %.09.prol to i64
  %7 = shl nuw nsw i64 %6, 8
  %8 = getelementptr inbounds i8, ptr %3, i64 %7
  store i16 0, ptr %8, align 2, !tbaa !5
  %prol.iter.next = add i16 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i16 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.prol.loopexit, label %.prol.preheader, !llvm.loop !9

.prol.loopexit:                                   ; preds = %.prol.preheader, %2
  %.09.in.unr = phi i16 [ %4, %2 ], [ %.09.prol, %.prol.preheader ]
  %9 = icmp ult i16 %5, 3
  br i1 %9, label %.loopexit, label %.new

.new:                                             ; preds = %.prol.loopexit, %.new
  %.09.in = phi i16 [ %.09.3, %.new ], [ %.09.in.unr, %.prol.loopexit ]
  %.09 = add i16 %.09.in, -1
  %10 = zext i16 %.09 to i64
  %11 = shl nuw nsw i64 %10, 8
  %12 = getelementptr inbounds i8, ptr %3, i64 %11
  store i16 0, ptr %12, align 2, !tbaa !5
  %.09.1 = add i16 %.09.in, -2
  %13 = zext i16 %.09.1 to i64
  %14 = shl nuw nsw i64 %13, 8
  %15 = getelementptr inbounds i8, ptr %3, i64 %14
  store i16 0, ptr %15, align 2, !tbaa !5
  %.09.2 = add i16 %.09.in, -3
  %16 = zext i16 %.09.2 to i64
  %17 = shl nuw nsw i64 %16, 8
  %18 = getelementptr inbounds i8, ptr %3, i64 %17
  store i16 0, ptr %18, align 2, !tbaa !5
  %.09.3 = add i16 %.09.in, -4
  %19 = zext i16 %.09.3 to i64
  %20 = shl nuw nsw i64 %19, 8
  %21 = getelementptr inbounds i8, ptr %3, i64 %20
  store i16 0, ptr %21, align 2, !tbaa !5
  %22 = icmp eq i16 %.09.3, 0
  br i1 %22, label %.loopexit, label %.new

.loopexit:                                        ; preds = %.prol.loopexit, %.new, %1
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass