; 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.mapnode_t = type { i16, i16, i16, i16, [2 x [4 x i16]], [2 x i16] }
%struct.node_t = type { i32, i32, i32, i32, [2 x [4 x i32]], [2 x i16] }

@numnodes = external dso_local global i32, align 4
@nodes = external dso_local global ptr, align 8

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

declare i32 @W_LumpLength(i32 noundef) #1

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

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

declare void @Z_Free(ptr noundef) #1

; 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 @P_LoadNodes(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = 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 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = call i32 @W_LumpLength(i32 noundef %9)
  %11 = sext i32 %10 to i64
  %12 = udiv i64 %11, 28
  %13 = trunc i64 %12 to i32
  store i32 %13, ptr @numnodes, align 4, !tbaa !5
  %14 = load i32, ptr @numnodes, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = mul i64 %15, 52
  %17 = trunc i64 %16 to i32
  %18 = call ptr @Z_Malloc(i32 noundef %17, i32 noundef 50, ptr noundef null)
  store ptr %18, ptr @nodes, align 8, !tbaa !9
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = call ptr @W_CacheLumpNum(i32 noundef %19, i32 noundef 1)
  store ptr %20, ptr %3, align 8, !tbaa !9
  %21 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %21, ptr %7, align 8, !tbaa !9
  %22 = load ptr, ptr @nodes, align 8, !tbaa !9
  store ptr %22, ptr %8, align 8, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %102, %1
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = load i32, ptr @numnodes, align 4, !tbaa !5
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %27, label %109

27:                                               ; preds = %23
  %28 = load ptr, ptr %7, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.mapnode_t, ptr %28, i32 0, i32 0
  %30 = load i16, ptr %29, align 2, !tbaa !11
  %31 = sext i16 %30 to i32
  %32 = shl i32 %31, 16
  %33 = load ptr, ptr %8, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.node_t, ptr %33, i32 0, i32 0
  store i32 %32, ptr %34, align 4, !tbaa !14
  %35 = load ptr, ptr %7, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.mapnode_t, ptr %35, i32 0, i32 1
  %37 = load i16, ptr %36, align 2, !tbaa !16
  %38 = sext i16 %37 to i32
  %39 = shl i32 %38, 16
  %40 = load ptr, ptr %8, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.node_t, ptr %40, i32 0, i32 1
  store i32 %39, ptr %41, align 4, !tbaa !17
  %42 = load ptr, ptr %7, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.mapnode_t, ptr %42, i32 0, i32 2
  %44 = load i16, ptr %43, align 2, !tbaa !18
  %45 = sext i16 %44 to i32
  %46 = shl i32 %45, 16
  %47 = load ptr, ptr %8, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.node_t, ptr %47, i32 0, i32 2
  store i32 %46, ptr %48, align 4, !tbaa !19
  %49 = load ptr, ptr %7, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.mapnode_t, ptr %49, i32 0, i32 3
  %51 = load i16, ptr %50, align 2, !tbaa !20
  %52 = sext i16 %51 to i32
  %53 = shl i32 %52, 16
  %54 = load ptr, ptr %8, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.node_t, ptr %54, i32 0, i32 3
  store i32 %53, ptr %55, align 4, !tbaa !21
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %98, %27
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = icmp slt i32 %57, 2
  br i1 %58, label %59, label %101

59:                                               ; preds = %56
  %60 = load ptr, ptr %7, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.mapnode_t, ptr %60, i32 0, i32 5
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [2 x i16], ptr %61, i64 0, i64 %63
  %65 = load i16, ptr %64, align 2, !tbaa !22
  %66 = load ptr, ptr %8, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.node_t, ptr %66, i32 0, i32 5
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [2 x i16], ptr %67, i64 0, i64 %69
  store i16 %65, ptr %70, align 2, !tbaa !22
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %71

71:                                               ; preds = %94, %59
  %72 = load i32, ptr %6, align 4, !tbaa !5
  %73 = icmp slt i32 %72, 4
  br i1 %73, label %74, label %97

74:                                               ; preds = %71
  %75 = load ptr, ptr %7, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct.mapnode_t, ptr %75, i32 0, i32 4
  %77 = load i32, ptr %5, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [2 x [4 x i16]], ptr %76, i64 0, i64 %78
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [4 x i16], ptr %79, i64 0, i64 %81
  %83 = load i16, ptr %82, align 2, !tbaa !22
  %84 = sext i16 %83 to i32
  %85 = shl i32 %84, 16
  %86 = load ptr, ptr %8, align 8, !tbaa !9
  %87 = getelementptr inbounds %struct.node_t, ptr %86, i32 0, i32 4
  %88 = load i32, ptr %5, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [2 x [4 x i32]], ptr %87, i64 0, i64 %89
  %91 = load i32, ptr %6, align 4, !tbaa !5
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [4 x i32], ptr %90, i64 0, i64 %92
  store i32 %85, ptr %93, align 4, !tbaa !5
  br label %94

94:                                               ; preds = %74
  %95 = load i32, ptr %6, align 4, !tbaa !5
  %96 = add nsw i32 %95, 1
  store i32 %96, ptr %6, align 4, !tbaa !5
  br label %71, !llvm.loop !23

97:                                               ; preds = %71
  br label %98

98:                                               ; preds = %97
  %99 = load i32, ptr %5, align 4, !tbaa !5
  %100 = add nsw i32 %99, 1
  store i32 %100, ptr %5, align 4, !tbaa !5
  br label %56, !llvm.loop !25

101:                                              ; preds = %56
  br label %102

102:                                              ; preds = %101
  %103 = load i32, ptr %4, align 4, !tbaa !5
  %104 = add nsw i32 %103, 1
  store i32 %104, ptr %4, align 4, !tbaa !5
  %105 = load ptr, ptr %8, align 8, !tbaa !9
  %106 = getelementptr inbounds %struct.node_t, ptr %105, i32 1
  store ptr %106, ptr %8, align 8, !tbaa !9
  %107 = load ptr, ptr %7, align 8, !tbaa !9
  %108 = getelementptr inbounds %struct.mapnode_t, ptr %107, i32 1
  store ptr %108, ptr %7, align 8, !tbaa !9
  br label %23, !llvm.loop !26

109:                                              ; preds = %23
  %110 = load ptr, ptr %3, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %110)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, 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
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { 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, i64 4, !13, i64 6, !7, i64 8, !7, i64 24}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !7, i64 16, !7, i64 48}
!16 = !{!12, !13, i64 2}
!17 = !{!15, !6, i64 4}
!18 = !{!12, !13, i64 4}
!19 = !{!15, !6, i64 8}
!20 = !{!12, !13, i64 6}
!21 = !{!15, !6, i64 12}
!22 = !{!13, !13, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = distinct !{!25, !24}
!26 = distinct !{!26, !24}


; 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"

@numnodes = external dso_local local_unnamed_addr global i32, align 4
@nodes = external dso_local local_unnamed_addr global ptr, align 8

declare i32 @W_LumpLength(i32 noundef) local_unnamed_addr #0

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

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

declare void @Z_Free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadNodes(i32 noundef %0) local_unnamed_addr #1 {
  %2 = tail call i32 @W_LumpLength(i32 noundef %0) #2
  %3 = sext i32 %2 to i64
  %4 = udiv i64 %3, 28
  %5 = trunc i64 %4 to i32
  store i32 %5, ptr @numnodes, align 4, !tbaa !5
  %6 = mul i32 %5, 52
  %7 = tail call ptr @Z_Malloc(i32 noundef %6, i32 noundef 50, ptr noundef null) #2
  store ptr %7, ptr @nodes, align 8, !tbaa !9
  %8 = tail call ptr @W_CacheLumpNum(i32 noundef %0, i32 noundef 1) #2
  %9 = load i32, ptr @numnodes, align 4, !tbaa !5
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph.preheader, label %._crit_edge

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.037 = phi ptr [ %32, %.lr.ph ], [ %11, %.lr.ph.preheader ]
  %.02936 = phi ptr [ %33, %.lr.ph ], [ %8, %.lr.ph.preheader ]
  %.03235 = phi i32 [ %31, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %12 = load <4 x i16>, ptr %.02936, align 2, !tbaa !11
  %13 = sext <4 x i16> %12 to <4 x i32>
  %14 = shl nsw <4 x i32> %13, <i32 16, i32 16, i32 16, i32 16>
  store <4 x i32> %14, ptr %.037, align 4, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %.02936, i64 24
  %16 = getelementptr inbounds i8, ptr %.037, i64 48
  %17 = getelementptr inbounds i8, ptr %.02936, i64 8
  %18 = getelementptr inbounds i8, ptr %.037, i64 16
  %19 = load i16, ptr %15, align 2, !tbaa !11
  store i16 %19, ptr %16, align 2, !tbaa !11
  %20 = load <4 x i16>, ptr %17, align 2, !tbaa !11
  %21 = sext <4 x i16> %20 to <4 x i32>
  %22 = shl nsw <4 x i32> %21, <i32 16, i32 16, i32 16, i32 16>
  store <4 x i32> %22, ptr %18, align 4, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %.02936, i64 26
  %24 = load i16, ptr %23, align 2, !tbaa !11
  %25 = getelementptr inbounds i8, ptr %.037, i64 50
  store i16 %24, ptr %25, align 2, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %.02936, i64 16
  %27 = getelementptr inbounds i8, ptr %.037, i64 32
  %28 = load <4 x i16>, ptr %26, align 2, !tbaa !11
  %29 = sext <4 x i16> %28 to <4 x i32>
  %30 = shl nsw <4 x i32> %29, <i32 16, i32 16, i32 16, i32 16>
  store <4 x i32> %30, ptr %27, align 4, !tbaa !5
  %31 = add nuw nsw i32 %.03235, 1
  %32 = getelementptr inbounds i8, ptr %.037, i64 52
  %33 = getelementptr inbounds i8, ptr %.02936, i64 28
  %34 = load i32, ptr @numnodes, align 4, !tbaa !5
  %35 = icmp slt i32 %31, %34
  br i1 %35, label %.lr.ph, label %._crit_edge, !llvm.loop !13

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

attributes #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 = { 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 = !{!"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,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass