; LLVM IR from paintdream/DontStarveLuaJIT :: src/luajit/src (lj_alloc)

; 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.malloc_chunk = type { i64, i64, ptr, ptr }
%struct.malloc_state = type { i32, i32, i64, i64, ptr, ptr, i64, i64, [66 x ptr], [32 x ptr], %struct.malloc_segment }
%struct.malloc_segment = type { ptr, i64, ptr }

; Function Attrs: nounwind uwtable
define hidden ptr @lj_alloc_create() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  store i64 131072, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = call ptr @CALL_MMAP(i64 noundef %9)
  store ptr %10, ptr %3, align 8, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !9
  %12 = icmp ne ptr %11, inttoptr (i64 -1 to ptr)
  br i1 %12, label %13, label %70

13:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 872, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %14 = load ptr, ptr %3, align 8, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !9
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %17 = ptrtoint ptr %16 to i64
  %18 = and i64 %17, 7
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %13
  br label %28

21:                                               ; preds = %13
  %22 = load ptr, ptr %3, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %22, i64 16
  %24 = ptrtoint ptr %23 to i64
  %25 = and i64 %24, 7
  %26 = sub i64 8, %25
  %27 = and i64 %26, 7
  br label %28

28:                                               ; preds = %21, %20
  %29 = phi i64 [ 0, %20 ], [ %27, %21 ]
  %30 = getelementptr inbounds i8, ptr %14, i64 %29
  store ptr %30, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = getelementptr inbounds i8, ptr %31, i64 16
  store ptr %32, ptr %7, align 8, !tbaa !9
  %33 = load ptr, ptr %7, align 8, !tbaa !9
  %34 = load i64, ptr %4, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %33, i8 0, i64 %34, i1 false)
  %35 = load i64, ptr %4, align 8, !tbaa !5
  %36 = or i64 %35, 1
  %37 = or i64 %36, 2
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.malloc_chunk, ptr %38, i32 0, i32 1
  store i64 %37, ptr %39, align 8, !tbaa !11
  %40 = load ptr, ptr %3, align 8, !tbaa !9
  %41 = load ptr, ptr %7, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.malloc_state, ptr %41, i32 0, i32 10
  %43 = getelementptr inbounds %struct.malloc_segment, ptr %42, i32 0, i32 0
  store ptr %40, ptr %43, align 8, !tbaa !13
  %44 = load i64, ptr %2, align 8, !tbaa !5
  %45 = load ptr, ptr %7, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.malloc_state, ptr %45, i32 0, i32 10
  %47 = getelementptr inbounds %struct.malloc_segment, ptr %46, i32 0, i32 1
  store i64 %44, ptr %47, align 8, !tbaa !17
  %48 = load ptr, ptr %7, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.malloc_state, ptr %48, i32 0, i32 7
  store i64 255, ptr %49, align 8, !tbaa !18
  %50 = load ptr, ptr %7, align 8, !tbaa !9
  call void @init_bins(ptr noundef %50)
  %51 = load ptr, ptr %7, align 8, !tbaa !9
  %52 = getelementptr inbounds i8, ptr %51, i64 -16
  %53 = load ptr, ptr %7, align 8, !tbaa !9
  %54 = getelementptr inbounds i8, ptr %53, i64 -16
  %55 = getelementptr inbounds %struct.malloc_chunk, ptr %54, i32 0, i32 1
  %56 = load i64, ptr %55, align 8, !tbaa !11
  %57 = and i64 %56, -4
  %58 = getelementptr inbounds i8, ptr %52, i64 %57
  store ptr %58, ptr %5, align 8, !tbaa !9
  %59 = load ptr, ptr %7, align 8, !tbaa !9
  %60 = load ptr, ptr %5, align 8, !tbaa !9
  %61 = load ptr, ptr %3, align 8, !tbaa !9
  %62 = load i64, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds i8, ptr %61, i64 %62
  %64 = load ptr, ptr %5, align 8, !tbaa !9
  %65 = ptrtoint ptr %63 to i64
  %66 = ptrtoint ptr %64 to i64
  %67 = sub i64 %65, %66
  %68 = sub i64 %67, 64
  call void @init_top(ptr noundef %59, ptr noundef %60, i64 noundef %68)
  %69 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %69, ptr %1, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %71

70:                                               ; preds = %0
  store ptr null, ptr %1, align 8
  store i32 1, ptr %8, align 4
  br label %71

71:                                               ; preds = %70, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  %72 = load ptr, ptr %1, align 8
  ret ptr %72
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @CALL_MMAP(i64 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
declare hidden void @init_bins(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @init_top(ptr noundef, ptr noundef, i64 noundef) #0

; 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 = { alwaysinline 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"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, !6, i64 8}
!12 = !{!"malloc_chunk", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!14, !10, i64 840}
!14 = !{!"malloc_state", !15, i64 0, !15, i64 4, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 32, !6, i64 40, !6, i64 48, !7, i64 56, !7, i64 584, !16, i64 840}
!15 = !{!"int", !7, i64 0}
!16 = !{!"malloc_segment", !10, i64 0, !6, i64 8, !10, i64 16}
!17 = !{!14, !6, i64 848}
!18 = !{!14, !6, i64 48}


; 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 hidden noundef ptr @lj_alloc_create() local_unnamed_addr #0 {
  %1 = tail call ptr @CALL_MMAP(i64 noundef 131072)
  %.not = icmp eq ptr %1, inttoptr (i64 -1 to ptr)
  br i1 %.not, label %21, label %2

2:                                                ; preds = %0
  %3 = getelementptr inbounds i8, ptr %1, i64 16
  %4 = ptrtoint ptr %3 to i64
  %5 = sub i64 0, %4
  %6 = and i64 %5, 7
  %7 = getelementptr inbounds i8, ptr %1, i64 %6
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(872) %8, i8 0, i64 872, i1 false)
  %9 = getelementptr inbounds i8, ptr %7, i64 8
  store i64 875, ptr %9, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %7, i64 856
  store ptr %1, ptr %10, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %7, i64 864
  store i64 131072, ptr %11, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %7, i64 64
  store i64 255, ptr %12, align 8, !tbaa !16
  tail call void @init_bins(ptr noundef nonnull %8)
  %13 = load i64, ptr %9, align 8, !tbaa !5
  %14 = and i64 %13, -4
  %15 = getelementptr inbounds i8, ptr %7, i64 %14
  %16 = getelementptr inbounds i8, ptr %1, i64 131072
  %17 = ptrtoint ptr %16 to i64
  %18 = ptrtoint ptr %15 to i64
  %19 = add i64 %17, -64
  %20 = sub i64 %19, %18
  tail call void @init_top(ptr noundef nonnull %8, ptr noundef %15, i64 noundef %20)
  br label %21

21:                                               ; preds = %0, %2
  %.0 = phi ptr [ %8, %2 ], [ null, %0 ]
  ret ptr %.0
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

; Function Attrs: nounwind uwtable
declare hidden void @init_top(ptr noundef, ptr noundef, i64 noundef) 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" }
attributes #1 = { alwaysinline 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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, !7, i64 8}
!6 = !{!"malloc_chunk", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !10, i64 840}
!12 = !{!"malloc_state", !13, i64 0, !13, i64 4, !7, i64 8, !7, i64 16, !10, i64 24, !10, i64 32, !7, i64 40, !7, i64 48, !8, i64 56, !8, i64 584, !14, i64 840}
!13 = !{!"int", !8, i64 0}
!14 = !{!"malloc_segment", !10, i64 0, !7, i64 8, !10, i64 16}
!15 = !{!12, !7, i64 848}
!16 = !{!12, !7, i64 48}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass