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

; 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.point_t = type { i32, i32 }
%struct.wbstartstruct_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [4 x %struct.wbplayerstruct_t] }
%struct.wbplayerstruct_t = type { i32, i32, i32, i32, i32, [4 x i32], i32 }
%struct.patch_t = type { i16, i16, i16, i16, [8 x i32] }

@wbs = external hidden global ptr, align 8
@lnodes = external hidden global [4 x [9 x %struct.point_t]], align 16
@.str = external hidden unnamed_addr constant [34 x i8], align 1

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

declare void @V_DrawPatch(i32 noundef, i32 noundef, i32 noundef, 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 @WI_drawOnLnode(i32 noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  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 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !5
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %94, %2
  %14 = load ptr, ptr @wbs, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.wbstartstruct_t, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4 x [9 x %struct.point_t]], ptr @lnodes, i64 0, i64 %17
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [9 x %struct.point_t], ptr %18, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.point_t, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !13
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds ptr, ptr %24, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.patch_t, ptr %28, i32 0, i32 2
  %30 = load i16, ptr %29, align 4, !tbaa !15
  %31 = sext i16 %30 to i32
  %32 = sub nsw i32 %23, %31
  store i32 %32, ptr %6, align 4, !tbaa !5
  %33 = load ptr, ptr @wbs, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.wbstartstruct_t, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [4 x [9 x %struct.point_t]], ptr @lnodes, i64 0, i64 %36
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [9 x %struct.point_t], ptr %37, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.point_t, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !18
  %43 = load ptr, ptr %4, align 8, !tbaa !9
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds ptr, ptr %43, i64 %45
  %47 = load ptr, ptr %46, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.patch_t, ptr %47, i32 0, i32 3
  %49 = load i16, ptr %48, align 2, !tbaa !19
  %50 = sext i16 %49 to i32
  %51 = sub nsw i32 %42, %50
  store i32 %51, ptr %7, align 4, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load ptr, ptr %4, align 8, !tbaa !9
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds ptr, ptr %53, i64 %55
  %57 = load ptr, ptr %56, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.patch_t, ptr %57, i32 0, i32 0
  %59 = load i16, ptr %58, align 4, !tbaa !20
  %60 = sext i16 %59 to i32
  %61 = add nsw i32 %52, %60
  store i32 %61, ptr %8, align 4, !tbaa !5
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = load ptr, ptr %4, align 8, !tbaa !9
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds ptr, ptr %63, i64 %65
  %67 = load ptr, ptr %66, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.patch_t, ptr %67, i32 0, i32 1
  %69 = load i16, ptr %68, align 2, !tbaa !21
  %70 = sext i16 %69 to i32
  %71 = add nsw i32 %62, %70
  store i32 %71, ptr %9, align 4, !tbaa !5
  %72 = load i32, ptr %6, align 4, !tbaa !5
  %73 = icmp sge i32 %72, 0
  br i1 %73, label %74, label %84

74:                                               ; preds = %13
  %75 = load i32, ptr %8, align 4, !tbaa !5
  %76 = icmp slt i32 %75, 320
  br i1 %76, label %77, label %84

77:                                               ; preds = %74
  %78 = load i32, ptr %7, align 4, !tbaa !5
  %79 = icmp sge i32 %78, 0
  br i1 %79, label %80, label %84

80:                                               ; preds = %77
  %81 = load i32, ptr %9, align 4, !tbaa !5
  %82 = icmp slt i32 %81, 200
  br i1 %82, label %83, label %84

83:                                               ; preds = %80
  store i32 1, ptr %10, align 4, !tbaa !5
  br label %87

84:                                               ; preds = %80, %77, %74, %13
  %85 = load i32, ptr %5, align 4, !tbaa !5
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %5, align 4, !tbaa !5
  br label %87

87:                                               ; preds = %84, %83
  br label %88

88:                                               ; preds = %87
  %89 = load i32, ptr %10, align 4, !tbaa !5
  %90 = icmp ne i32 %89, 0
  br i1 %90, label %94, label %91

91:                                               ; preds = %88
  %92 = load i32, ptr %5, align 4, !tbaa !5
  %93 = icmp ne i32 %92, 2
  br label %94

94:                                               ; preds = %91, %88
  %95 = phi i1 [ false, %88 ], [ %93, %91 ]
  br i1 %95, label %13, label %96, !llvm.loop !22

96:                                               ; preds = %94
  %97 = load i32, ptr %10, align 4, !tbaa !5
  %98 = icmp ne i32 %97, 0
  br i1 %98, label %99, label %136

99:                                               ; preds = %96
  %100 = load i32, ptr %5, align 4, !tbaa !5
  %101 = icmp slt i32 %100, 2
  br i1 %101, label %102, label %136

102:                                              ; preds = %99
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %103 = load ptr, ptr @wbs, align 8, !tbaa !9
  %104 = getelementptr inbounds %struct.wbstartstruct_t, ptr %103, i32 0, i32 0
  %105 = load i32, ptr %104, align 4, !tbaa !11
  %106 = sext i32 %105 to i64
  %107 = getelementptr inbounds [4 x [9 x %struct.point_t]], ptr @lnodes, i64 0, i64 %106
  %108 = load i32, ptr %3, align 4, !tbaa !5
  %109 = sext i32 %108 to i64
  %110 = getelementptr inbounds [9 x %struct.point_t], ptr %107, i64 0, i64 %109
  %111 = getelementptr inbounds %struct.point_t, ptr %110, i32 0, i32 0
  %112 = load i32, ptr %111, align 8, !tbaa !13
  %113 = mul nsw i32 %112, 320
  %114 = add nsw i32 %113, 160
  %115 = sdiv i32 %114, 320
  store i32 %115, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %116 = load ptr, ptr @wbs, align 8, !tbaa !9
  %117 = getelementptr inbounds %struct.wbstartstruct_t, ptr %116, i32 0, i32 0
  %118 = load i32, ptr %117, align 4, !tbaa !11
  %119 = sext i32 %118 to i64
  %120 = getelementptr inbounds [4 x [9 x %struct.point_t]], ptr @lnodes, i64 0, i64 %119
  %121 = load i32, ptr %3, align 4, !tbaa !5
  %122 = sext i32 %121 to i64
  %123 = getelementptr inbounds [9 x %struct.point_t], ptr %120, i64 0, i64 %122
  %124 = getelementptr inbounds %struct.point_t, ptr %123, i32 0, i32 1
  %125 = load i32, ptr %124, align 4, !tbaa !18
  %126 = mul nsw i32 %125, 200
  %127 = add nsw i32 %126, 100
  %128 = sdiv i32 %127, 200
  store i32 %128, ptr %12, align 4, !tbaa !5
  %129 = load i32, ptr %11, align 4, !tbaa !5
  %130 = load i32, ptr %12, align 4, !tbaa !5
  %131 = load ptr, ptr %4, align 8, !tbaa !9
  %132 = load i32, ptr %5, align 4, !tbaa !5
  %133 = sext i32 %132 to i64
  %134 = getelementptr inbounds ptr, ptr %131, i64 %133
  %135 = load ptr, ptr %134, align 8, !tbaa !9
  call void @V_DrawPatch(i32 noundef %129, i32 noundef %130, i32 noundef 0, ptr noundef %135)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %140

136:                                              ; preds = %99, %96
  %137 = load i32, ptr %3, align 4, !tbaa !5
  %138 = add nsw i32 %137, 1
  %139 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %138)
  br label %140

140:                                              ; preds = %136, %102
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, 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
  ret void
}

declare i32 @printf(ptr noundef, ...) #1

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, !6, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !7, i64 40}
!13 = !{!14, !6, i64 0}
!14 = !{!"", !6, i64 0, !6, i64 4}
!15 = !{!16, !17, i64 4}
!16 = !{!"", !17, i64 0, !17, i64 2, !17, i64 4, !17, i64 6, !7, i64 8}
!17 = !{!"short", !7, i64 0}
!18 = !{!14, !6, i64 4}
!19 = !{!16, !17, i64 6}
!20 = !{!16, !17, i64 0}
!21 = !{!16, !17, i64 2}
!22 = distinct !{!22, !23}
!23 = !{!"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"

%struct.point_t = type { i32, i32 }

@wbs = external hidden local_unnamed_addr global ptr, align 8
@lnodes = external hidden local_unnamed_addr global [4 x [9 x %struct.point_t]], align 16
@.str = external hidden unnamed_addr constant [34 x i8], align 1

declare void @V_DrawPatch(i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @WI_drawOnLnode(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #1 {
  %3 = load ptr, ptr @wbs, align 8, !tbaa !5
  %4 = load i32, ptr %3, align 4, !tbaa !9
  %5 = sext i32 %4 to i64
  %6 = sext i32 %0 to i64
  %7 = getelementptr inbounds [4 x [9 x %struct.point_t]], ptr @lnodes, i64 0, i64 %5, i64 %6
  %8 = load i32, ptr %7, align 8, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %7, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !14
  br label %11

11:                                               ; preds = %11, %2
  %.0 = phi i32 [ 0, %2 ], [ %.1, %11 ]
  %12 = zext nneg i32 %.0 to i64
  %13 = getelementptr inbounds ptr, ptr %1, i64 %12
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 4
  %16 = load i16, ptr %15, align 4, !tbaa !15
  %17 = sext i16 %16 to i32
  %18 = sub nsw i32 %8, %17
  %19 = getelementptr inbounds i8, ptr %14, i64 6
  %20 = load i16, ptr %19, align 2, !tbaa !18
  %21 = sext i16 %20 to i32
  %22 = sub nsw i32 %10, %21
  %23 = load i16, ptr %14, align 4, !tbaa !19
  %24 = sext i16 %23 to i32
  %25 = add nsw i32 %18, %24
  %26 = getelementptr inbounds i8, ptr %14, i64 2
  %27 = load i16, ptr %26, align 2, !tbaa !20
  %28 = sext i16 %27 to i32
  %29 = add nsw i32 %22, %28
  %30 = icmp sgt i32 %18, -1
  %31 = icmp slt i32 %25, 320
  %or.cond = select i1 %30, i1 %31, i1 false
  %32 = icmp sgt i32 %22, -1
  %or.cond3 = select i1 %or.cond, i1 %32, i1 false
  %33 = icmp slt i32 %29, 200
  %or.cond5 = select i1 %or.cond3, i1 %33, i1 false
  %not.or.cond5 = xor i1 %or.cond5, true
  %34 = zext i1 %not.or.cond5 to i32
  %.1 = add nuw nsw i32 %.0, %34
  %35 = icmp eq i32 %.1, 2
  %.not36 = select i1 %or.cond5, i1 true, i1 %35
  br i1 %.not36, label %36, label %11, !llvm.loop !21

36:                                               ; preds = %11
  %37 = icmp ult i32 %.1, 2
  %or.cond7 = select i1 %or.cond5, i1 %37, i1 false
  br i1 %or.cond7, label %38, label %48

38:                                               ; preds = %36
  %39 = mul nsw i32 %8, 320
  %40 = add nsw i32 %39, 160
  %41 = sdiv i32 %40, 320
  %42 = mul nsw i32 %10, 200
  %43 = add nsw i32 %42, 100
  %44 = sdiv i32 %43, 200
  %45 = zext nneg i32 %.1 to i64
  %46 = getelementptr inbounds ptr, ptr %1, i64 %45
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  tail call void @V_DrawPatch(i32 noundef %41, i32 noundef %44, i32 noundef 0, ptr noundef %47) #3
  br label %51

48:                                               ; preds = %36
  %49 = add nsw i32 %0, 1
  %50 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %49)
  br label %51

51:                                               ; preds = %48, %38
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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 = { nofree nounwind "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !7, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !11, i64 0}
!13 = !{!"", !11, i64 0, !11, i64 4}
!14 = !{!13, !11, i64 4}
!15 = !{!16, !17, i64 4}
!16 = !{!"", !17, i64 0, !17, i64 2, !17, i64 4, !17, i64 6, !7, i64 8}
!17 = !{!"short", !7, i64 0}
!18 = !{!16, !17, i64 6}
!19 = !{!16, !17, i64 0}
!20 = !{!16, !17, i64 2}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass