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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @core_malloc(i64 noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca i8, align 1
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i16, align 2
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 16776960, ptr %4, align 8, !tbaa !5
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = icmp uge i64 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %123

20:                                               ; preds = %1
  %21 = call i32 @core_lock(i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %22 = call ptr @get_blocks(ptr noundef %6)
  store ptr %22, ptr %7, align 8, !tbaa !9
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = add i64 %23, 2
  store i64 %24, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  %25 = load i64, ptr %3, align 8, !tbaa !5
  %26 = udiv i64 %25, 256
  %27 = trunc i64 %26 to i16
  store i16 %27, ptr %8, align 2, !tbaa !11
  %28 = load i64, ptr %3, align 8, !tbaa !5
  %29 = urem i64 %28, 256
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %20
  %32 = load i16, ptr %8, align 2, !tbaa !11
  %33 = add i16 %32, 1
  store i16 %33, ptr %8, align 2, !tbaa !11
  br label %34

34:                                               ; preds = %31, %20
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  store i8 0, ptr %9, align 1, !tbaa !13
  br label %35

35:                                               ; preds = %115, %34
  %36 = load i8, ptr %9, align 1, !tbaa !13
  %37 = zext i8 %36 to i32
  %38 = load i8, ptr %6, align 1, !tbaa !13
  %39 = zext i8 %38 to i32
  %40 = icmp slt i32 %37, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %35
  store i32 2, ptr %5, align 4
  br label %118

42:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %43 = load ptr, ptr %7, align 8, !tbaa !9
  %44 = load i8, ptr %9, align 1, !tbaa !13
  %45 = zext i8 %44 to i64
  %46 = getelementptr inbounds %struct.BlockStruct, ptr %43, i64 %45
  %47 = getelementptr inbounds %struct.BlockStruct, ptr %46, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !14
  %49 = udiv i64 %48, 256
  store i64 %49, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %50 = load ptr, ptr %7, align 8, !tbaa !9
  %51 = load i8, ptr %9, align 1, !tbaa !13
  %52 = zext i8 %51 to i64
  %53 = getelementptr inbounds %struct.BlockStruct, ptr %50, i64 %52
  %54 = getelementptr inbounds %struct.BlockStruct, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !16
  store ptr %55, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #3
  store i16 0, ptr %12, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store i64 0, ptr %13, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %101, %42
  %57 = load i64, ptr %13, align 8, !tbaa !5
  %58 = load i64, ptr %10, align 8, !tbaa !5
  %59 = icmp ult i64 %57, %58
  br i1 %59, label %60, label %111

60:                                               ; preds = %56
  %61 = load i16, ptr %12, align 2, !tbaa !11
  %62 = zext i16 %61 to i32
  %63 = load i16, ptr %8, align 2, !tbaa !11
  %64 = zext i16 %63 to i32
  %65 = icmp eq i32 %62, %64
  br i1 %65, label %66, label %85

66:                                               ; preds = %60
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %67 = load i64, ptr %13, align 8, !tbaa !5
  %68 = load i16, ptr %8, align 2, !tbaa !11
  %69 = zext i16 %68 to i64
  %70 = sub i64 %67, %69
  store i64 %70, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  %71 = load ptr, ptr %7, align 8, !tbaa !9
  %72 = load i8, ptr %9, align 1, !tbaa !13
  %73 = zext i8 %72 to i64
  %74 = getelementptr inbounds %struct.BlockStruct, ptr %71, i64 %73
  %75 = getelementptr inbounds %struct.BlockStruct, ptr %74, i32 0, i32 0
  %76 = load ptr, ptr %75, align 8, !tbaa !16
  %77 = load i64, ptr %14, align 8, !tbaa !5
  %78 = mul i64 256, %77
  %79 = getelementptr inbounds i8, ptr %76, i64 %78
  store ptr %79, ptr %15, align 8, !tbaa !9
  %80 = load i16, ptr %8, align 2, !tbaa !11
  %81 = load ptr, ptr %15, align 8, !tbaa !9
  store i16 %80, ptr %81, align 2, !tbaa !11
  %82 = call i32 @core_unlock(i32 noundef 0)
  %83 = load ptr, ptr %15, align 8, !tbaa !9
  %84 = getelementptr inbounds i8, ptr %83, i64 2
  store ptr %84, ptr %2, align 8
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  br label %112

85:                                               ; preds = %60
  %86 = load ptr, ptr %11, align 8, !tbaa !9
  %87 = load i16, ptr %86, align 2, !tbaa !11
  %88 = zext i16 %87 to i32
  %89 = icmp eq i32 %88, 0
  br i1 %89, label %90, label %95

90:                                               ; preds = %85
  %91 = load i16, ptr %12, align 2, !tbaa !11
  %92 = add i16 %91, 1
  store i16 %92, ptr %12, align 2, !tbaa !11
  %93 = load i64, ptr %13, align 8, !tbaa !5
  %94 = add i64 %93, 1
  store i64 %94, ptr %13, align 8, !tbaa !5
  br label %101

95:                                               ; preds = %85
  store i16 0, ptr %12, align 2, !tbaa !11
  %96 = load ptr, ptr %11, align 8, !tbaa !9
  %97 = load i16, ptr %96, align 2, !tbaa !11
  %98 = zext i16 %97 to i64
  %99 = load i64, ptr %13, align 8, !tbaa !5
  %100 = add i64 %99, %98
  store i64 %100, ptr %13, align 8, !tbaa !5
  br label %101

101:                                              ; preds = %95, %90
  %102 = load ptr, ptr %7, align 8, !tbaa !9
  %103 = load i8, ptr %9, align 1, !tbaa !13
  %104 = zext i8 %103 to i64
  %105 = getelementptr inbounds %struct.BlockStruct, ptr %102, i64 %104
  %106 = getelementptr inbounds %struct.BlockStruct, ptr %105, i32 0, i32 0
  %107 = load ptr, ptr %106, align 8, !tbaa !16
  %108 = load i64, ptr %13, align 8, !tbaa !5
  %109 = mul i64 256, %108
  %110 = getelementptr inbounds i8, ptr %107, i64 %109
  store ptr %110, ptr %11, align 8, !tbaa !9
  br label %56, !llvm.loop !17

111:                                              ; preds = %56
  store i32 0, ptr %5, align 4
  br label %112

112:                                              ; preds = %111, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %113 = load i32, ptr %5, align 4
  switch i32 %113, label %118 [
    i32 0, label %114
  ]

114:                                              ; preds = %112
  br label %115

115:                                              ; preds = %114
  %116 = load i8, ptr %9, align 1, !tbaa !13
  %117 = add i8 %116, 1
  store i8 %117, ptr %9, align 1, !tbaa !13
  br label %35, !llvm.loop !19

118:                                              ; preds = %112, %41
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  %119 = load i32, ptr %5, align 4
  switch i32 %119, label %122 [
    i32 2, label %120
  ]

120:                                              ; preds = %118
  %121 = call i32 @core_unlock(i32 noundef 0)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %122

122:                                              ; preds = %120, %118
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  br label %123

123:                                              ; preds = %122, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %124 = load ptr, ptr %2, align 8
  ret ptr %124
}

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

declare i32 @core_lock(i32 noundef) #2

declare ptr @get_blocks(ptr noundef) #2

declare i32 @core_unlock(i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !6, i64 8}
!15 = !{!"BlockStruct", !10, i64 0, !6, i64 8}
!16 = !{!15, !10, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

; Function Attrs: nounwind uwtable
define dso_local ptr @core_malloc(i64 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i8, align 1
  %3 = icmp ugt i64 %0, 16776959
  br i1 %3, label %34, label %4

4:                                                ; preds = %1
  %5 = tail call i32 @core_lock(i32 noundef 0) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %2) #4
  %6 = call ptr @get_blocks(ptr noundef nonnull %2) #4
  %7 = add nuw nsw i64 %0, 2
  %8 = lshr i64 %7, 8
  %9 = trunc nuw i64 %8 to i16
  %10 = and i64 %7, 255
  %.not = icmp ne i64 %10, 0
  %11 = zext i1 %.not to i16
  %spec.select = add i16 %11, %9
  %12 = load i8, ptr %2, align 1, !tbaa !5
  %.not4655.not = icmp eq i8 %12, 0
  br i1 %.not4655.not, label %.thread48, label %.lr.ph58.preheader

.lr.ph58.preheader:                               ; preds = %4
  %wide.trip.count = zext i8 %12 to i64
  br label %.lr.ph58

.lr.ph58:                                         ; preds = %.lr.ph58.preheader, %._crit_edge
  %indvars.iv = phi i64 [ 0, %.lr.ph58.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %13 = getelementptr inbounds %struct.BlockStruct, ptr %6, i64 %indvars.iv
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !8
  %16 = lshr i64 %15, 8
  %17 = load ptr, ptr %13, align 8, !tbaa !12
  %.not4551.not = icmp ult i64 %15, 256
  br i1 %.not4551.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph58, %19
  %.03554 = phi i64 [ %.136, %19 ], [ 0, %.lr.ph58 ]
  %.03753 = phi i16 [ %.138, %19 ], [ 0, %.lr.ph58 ]
  %.03952 = phi ptr [ %24, %19 ], [ %17, %.lr.ph58 ]
  %18 = icmp eq i16 %.03753, %spec.select
  br i1 %18, label %25, label %19

19:                                               ; preds = %.lr.ph
  %20 = load i16, ptr %.03952, align 2, !tbaa !13
  %21 = icmp eq i16 %20, 0
  %22 = add i16 %.03753, 1
  %.138 = select i1 %21, i16 %22, i16 0
  %narrow = call i16 @llvm.umax.i16(i16 %20, i16 1)
  %.136.v = zext i16 %narrow to i64
  %.136 = add nuw nsw i64 %.03554, %.136.v
  %23 = shl i64 %.136, 8
  %24 = getelementptr inbounds i8, ptr %17, i64 %23
  %.not45 = icmp ult i64 %.136, %16
  br i1 %.not45, label %.lr.ph, label %._crit_edge, !llvm.loop !15

._crit_edge:                                      ; preds = %19, %.lr.ph58
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.thread48, label %.lr.ph58, !llvm.loop !17

25:                                               ; preds = %.lr.ph
  %26 = zext i16 %spec.select to i64
  %27 = sub nsw i64 %.03554, %26
  %28 = shl i64 %27, 8
  %29 = getelementptr inbounds i8, ptr %17, i64 %28
  store i16 %spec.select, ptr %29, align 2, !tbaa !13
  %30 = call i32 @core_unlock(i32 noundef 0) #4
  %31 = getelementptr inbounds i8, ptr %29, i64 2
  br label %33

.thread48:                                        ; preds = %._crit_edge, %4
  %32 = call i32 @core_unlock(i32 noundef 0) #4
  br label %33

33:                                               ; preds = %25, %.thread48
  %.3 = phi ptr [ null, %.thread48 ], [ %31, %25 ]
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %2) #4
  br label %34

34:                                               ; preds = %1, %33
  %.4 = phi ptr [ %.3, %33 ], [ null, %1 ]
  ret ptr %.4
}

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

declare i32 @core_lock(i32 noundef) local_unnamed_addr #2

declare ptr @get_blocks(ptr noundef) local_unnamed_addr #2

declare i32 @core_unlock(i32 noundef) local_unnamed_addr #2

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.umax.i16(i16, i16) #3

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 8}
!9 = !{!"BlockStruct", !10, i64 0, !11, i64 8}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"long", !6, i64 0}
!12 = !{!9, !10, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !6, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; Effective O3 passes: ConstraintEliminationPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass