; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (p_setup)

; 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.mapvertex_t = type { i16, i16 }
%struct.vertex_t = type { i32, i32 }

@numvertexes = external dso_local global i32, align 4
@vertexes = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadVertexes(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = call i32 @W_LumpLength(i32 noundef %7)
  %9 = sext i32 %8 to i64
  %10 = udiv i64 %9, 4
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr @numvertexes, align 4, !tbaa !5
  %12 = load i32, ptr @numvertexes, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = mul i64 %13, 8
  %15 = trunc i64 %14 to i32
  %16 = call ptr @Z_Malloc(i32 noundef %15, i32 noundef 50, ptr noundef null)
  store ptr %16, ptr @vertexes, align 8, !tbaa !9
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = call ptr @W_CacheLumpNum(i32 noundef %17, i32 noundef 1)
  store ptr %18, ptr %3, align 8, !tbaa !9
  %19 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %19, ptr %5, align 8, !tbaa !9
  %20 = load ptr, ptr @vertexes, align 8, !tbaa !9
  store ptr %20, ptr %6, align 8, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %40, %1
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr @numvertexes, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %25, label %47

25:                                               ; preds = %21
  %26 = load ptr, ptr %5, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.mapvertex_t, ptr %26, i32 0, i32 0
  %28 = load i16, ptr %27, align 2, !tbaa !11
  %29 = sext i16 %28 to i32
  %30 = shl i32 %29, 16
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.vertex_t, ptr %31, i32 0, i32 0
  store i32 %30, ptr %32, align 4, !tbaa !14
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.mapvertex_t, ptr %33, i32 0, i32 1
  %35 = load i16, ptr %34, align 2, !tbaa !16
  %36 = sext i16 %35 to i32
  %37 = shl i32 %36, 16
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.vertex_t, ptr %38, i32 0, i32 1
  store i32 %37, ptr %39, align 4, !tbaa !17
  br label %40

40:                                               ; preds = %25
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %4, align 4, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.vertex_t, ptr %43, i32 1
  store ptr %44, ptr %6, align 8, !tbaa !9
  %45 = load ptr, ptr %5, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.mapvertex_t, ptr %45, i32 1
  store ptr %46, ptr %5, align 8, !tbaa !9
  br label %21, !llvm.loop !18

47:                                               ; preds = %21
  %48 = load ptr, ptr %3, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %48)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

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

declare i32 @W_LumpLength(i32 noundef) #2

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) #2

declare ptr @W_CacheLumpNum(i32 noundef, i32 noundef) #2

declare void @Z_Free(ptr 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 = !{!"int", !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, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"", !6, i64 0, !6, i64 4}
!16 = !{!12, !13, i64 2}
!17 = !{!15, !6, i64 4}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@numvertexes = external dso_local local_unnamed_addr global i32, align 4
@vertexes = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadVertexes(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @W_LumpLength(i32 noundef %0) #2
  %3 = ashr i32 %2, 2
  store i32 %3, ptr @numvertexes, align 4, !tbaa !5
  %4 = shl i32 %3, 3
  %5 = tail call ptr @Z_Malloc(i32 noundef %4, i32 noundef 50, ptr noundef null) #2
  store ptr %5, ptr @vertexes, align 8, !tbaa !9
  %6 = tail call ptr @W_CacheLumpNum(i32 noundef %0, i32 noundef 1) #2
  %7 = load i32, ptr @numvertexes, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %9 = load ptr, ptr @vertexes, align 8, !tbaa !9
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.015 = phi ptr [ %14, %.lr.ph ], [ %9, %.lr.ph.preheader ]
  %.01114 = phi ptr [ %15, %.lr.ph ], [ %6, %.lr.ph.preheader ]
  %.01213 = phi i32 [ %13, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %10 = load <2 x i16>, ptr %.01114, align 2, !tbaa !11
  %11 = sext <2 x i16> %10 to <2 x i32>
  %12 = shl nsw <2 x i32> %11, <i32 16, i32 16>
  store <2 x i32> %12, ptr %.015, align 4, !tbaa !5
  %13 = add nuw nsw i32 %.01213, 1
  %14 = getelementptr inbounds i8, ptr %.015, i64 8
  %15 = getelementptr inbounds i8, ptr %.01114, i64 4
  %16 = load i32, ptr @numvertexes, align 4, !tbaa !5
  %17 = icmp slt i32 %13, %16
  br i1 %17, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %1
  tail call void @Z_Free(ptr noundef %6) #2
  ret void
}

declare i32 @W_LumpLength(i32 noundef) local_unnamed_addr #1

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare ptr @W_CacheLumpNum(i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @Z_Free(ptr noundef) local_unnamed_addr #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 = { "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 = !{!"int", !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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass