; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Tagl/Lib (LinG)

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

@LinG_GP = external dso_local global ptr, align 8
@LinG_graph_mem = external dso_local global ptr, align 8
@LinG_colormap = external dso_local global ptr, align 8
@LinG_mode = external dso_local global i64, align 8
@LinG_width = external dso_local global i32, align 4
@LinG_height = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local i32 @LinG_OpenScreen(i32 noundef %0, i32 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @init_gport_X()
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = call noundef ptr @_ZN11GraphicPort4MakeEPKciii(ptr noundef @.str, i32 noundef %9, i32 noundef %10, i32 noundef 101)
  store ptr %11, ptr @LinG_GP, align 8, !tbaa !11
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  br label %68

14:                                               ; preds = %3
  %15 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %15, ptr @LinG_width, align 4, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %16, ptr @LinG_height, align 4, !tbaa !5
  %17 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %17, ptr @LinG_mode, align 8, !tbaa !9
  %18 = load i64, ptr @LinG_mode, align 8, !tbaa !9
  %19 = and i64 %18, 1
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %29, label %21

21:                                               ; preds = %14
  %22 = load i64, ptr @LinG_mode, align 8, !tbaa !9
  %23 = and i64 %22, 2
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %29

25:                                               ; preds = %21
  %26 = load ptr, ptr @LinG_GP, align 8, !tbaa !11
  %27 = call noundef i32 @_ZNK11GraphicPort13BytesPerPixelEv(ptr noundef nonnull align 8 dereferenceable(14128) %26)
  %28 = icmp ne i32 %27, 1
  br i1 %28, label %29, label %56

29:                                               ; preds = %25, %21, %14
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = mul nsw i32 %30, %31
  %33 = sext i32 %32 to i64
  %34 = call noalias noundef nonnull ptr @_Znam(i64 noundef %33) #7
  store ptr %34, ptr @LinG_graph_mem, align 8, !tbaa !11
  %35 = icmp ne ptr %34, null
  br i1 %35, label %37, label %36

36:                                               ; preds = %29
  call void @LinG_CloseScreen()
  store i32 0, ptr %4, align 4
  br label %68

37:                                               ; preds = %29
  %38 = load i64, ptr @LinG_mode, align 8, !tbaa !9
  %39 = and i64 %38, -3
  store i64 %39, ptr @LinG_mode, align 8, !tbaa !9
  %40 = call noalias noundef nonnull ptr @_Znam(i64 noundef 256) #7
  store ptr %40, ptr @LinG_colormap, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #8
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %52, %37
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = icmp slt i32 %42, 256
  br i1 %43, label %45, label %44

44:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #8
  br label %55

45:                                               ; preds = %41
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = trunc i32 %46 to i8
  %48 = load ptr, ptr @LinG_colormap, align 8, !tbaa !11
  %49 = load i32, ptr %8, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  store i8 %47, ptr %51, align 1, !tbaa !13
  br label %52

52:                                               ; preds = %45
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %8, align 4, !tbaa !5
  br label %41, !llvm.loop !14

55:                                               ; preds = %44
  br label %61

56:                                               ; preds = %25
  %57 = load ptr, ptr @LinG_GP, align 8, !tbaa !11
  %58 = call noundef ptr @_ZNK11GraphicPort8GraphMemEv(ptr noundef nonnull align 8 dereferenceable(14128) %57)
  store ptr %58, ptr @LinG_graph_mem, align 8, !tbaa !11
  %59 = load ptr, ptr @LinG_GP, align 8, !tbaa !11
  %60 = call noundef ptr @_ZNK11GraphicPort8ColormapEv(ptr noundef nonnull align 8 dereferenceable(14128) %59)
  store ptr %60, ptr @LinG_colormap, align 8, !tbaa !11
  br label %61

61:                                               ; preds = %56, %55
  %62 = load ptr, ptr @LinG_GP, align 8, !tbaa !11
  call void @_ZN11GraphicPort5ClearEh(ptr noundef nonnull align 8 dereferenceable(14128) %62, i8 noundef zeroext 0)
  %63 = load ptr, ptr @LinG_GP, align 8, !tbaa !11
  %64 = load ptr, ptr %63, align 8, !tbaa !16
  %65 = getelementptr inbounds ptr, ptr %64, i64 8
  %66 = load ptr, ptr %65, align 8
  %67 = call noundef i32 %66(ptr noundef nonnull align 8 dereferenceable(14128) %63)
  store i32 1, ptr %4, align 4
  br label %68

68:                                               ; preds = %61, %36, %13
  %69 = load i32, ptr %4, align 4
  ret i32 %69
}

declare void @init_gport_X() #1

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK11GraphicPort13BytesPerPixelEv(ptr noundef nonnull align 8 dereferenceable(14128)) #2 align 2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @LinG_CloseScreen() #4

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK11GraphicPort8GraphMemEv(ptr noundef nonnull align 8 dereferenceable(14128)) #2 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK11GraphicPort8ColormapEv(ptr noundef nonnull align 8 dereferenceable(14128)) #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN11GraphicPort5ClearEh(ptr noundef nonnull align 8 dereferenceable(14128), i8 noundef zeroext) #6 align 2

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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 = { nobuiltin allocsize(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 #4 = { mustprogress 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 #5 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { inlinehint mustprogress 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 #7 = { builtin allocsize(0) }
attributes #8 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"vtable pointer", !8, i64 0}


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

@LinG_GP = external dso_local local_unnamed_addr global ptr, align 8
@LinG_graph_mem = external dso_local local_unnamed_addr global ptr, align 8
@LinG_colormap = external dso_local local_unnamed_addr global ptr, align 8
@LinG_mode = external dso_local local_unnamed_addr global i64, align 8
@LinG_width = external dso_local local_unnamed_addr global i32, align 4
@LinG_height = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local range(i32 0, 2) i32 @LinG_OpenScreen(i32 noundef %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  tail call void @init_gport_X()
  %4 = tail call noundef ptr @_ZN11GraphicPort4MakeEPKciii(ptr noundef nonnull @.str, i32 noundef %0, i32 noundef %1, i32 noundef 101)
  store ptr %4, ptr @LinG_GP, align 8, !tbaa !5
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %40, label %5

5:                                                ; preds = %3
  store i32 %0, ptr @LinG_width, align 4, !tbaa !9
  store i32 %1, ptr @LinG_height, align 4, !tbaa !9
  store i64 %2, ptr @LinG_mode, align 8, !tbaa !11
  %6 = and i64 %2, 3
  %or.cond.not = icmp eq i64 %6, 2
  br i1 %or.cond.not, label %7, label %9

7:                                                ; preds = %5
  %8 = tail call noundef i32 @_ZNK11GraphicPort13BytesPerPixelEv(ptr noundef nonnull align 8 dereferenceable(14128) %4)
  %.not15 = icmp eq i32 %8, 1
  br i1 %.not15, label %29, label %._crit_edge

._crit_edge:                                      ; preds = %7
  %.pre = load i64, ptr @LinG_mode, align 8, !tbaa !11
  br label %9

9:                                                ; preds = %._crit_edge, %5
  %10 = phi i64 [ %.pre, %._crit_edge ], [ %2, %5 ]
  %11 = mul nsw i32 %1, %0
  %12 = sext i32 %11 to i64
  %13 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %12) #5
  store ptr %13, ptr @LinG_graph_mem, align 8, !tbaa !5
  %14 = and i64 %10, -3
  store i64 %14, ptr @LinG_mode, align 8, !tbaa !11
  %15 = tail call noalias noundef nonnull dereferenceable(256) ptr @_Znam(i64 noundef 256) #5
  store ptr %15, ptr @LinG_colormap, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %16, %9
  %indvars.iv = phi i64 [ 0, %9 ], [ %indvars.iv.next.3, %16 ]
  %17 = trunc i64 %indvars.iv to i8
  %18 = load ptr, ptr @LinG_colormap, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 %indvars.iv
  store i8 %17, ptr %19, align 1, !tbaa !13
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = trunc i64 %indvars.iv.next to i8
  %21 = load ptr, ptr @LinG_colormap, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 %indvars.iv.next
  store i8 %20, ptr %22, align 1, !tbaa !13
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %23 = trunc i64 %indvars.iv.next.1 to i8
  %24 = load ptr, ptr @LinG_colormap, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 %indvars.iv.next.1
  store i8 %23, ptr %25, align 1, !tbaa !13
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %26 = trunc i64 %indvars.iv.next.2 to i8
  %27 = load ptr, ptr @LinG_colormap, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 %indvars.iv.next.2
  store i8 %26, ptr %28, align 1, !tbaa !13
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, 256
  br i1 %exitcond.not.3, label %.loopexit, label %16, !llvm.loop !14

29:                                               ; preds = %7
  %30 = load ptr, ptr @LinG_GP, align 8, !tbaa !5
  %31 = tail call noundef ptr @_ZNK11GraphicPort8GraphMemEv(ptr noundef nonnull align 8 dereferenceable(14128) %30)
  store ptr %31, ptr @LinG_graph_mem, align 8, !tbaa !5
  %32 = load ptr, ptr @LinG_GP, align 8, !tbaa !5
  %33 = tail call noundef ptr @_ZNK11GraphicPort8ColormapEv(ptr noundef nonnull align 8 dereferenceable(14128) %32)
  store ptr %33, ptr @LinG_colormap, align 8, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %16, %29
  %34 = load ptr, ptr @LinG_GP, align 8, !tbaa !5
  tail call void @_ZN11GraphicPort5ClearEh(ptr noundef nonnull align 8 dereferenceable(14128) %34, i8 noundef zeroext 0)
  %35 = load ptr, ptr @LinG_GP, align 8, !tbaa !5
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  %37 = getelementptr inbounds i8, ptr %36, i64 64
  %38 = load ptr, ptr %37, align 8
  %39 = tail call noundef i32 %38(ptr noundef nonnull align 8 dereferenceable(14128) %35)
  br label %40

40:                                               ; preds = %3, %.loopexit
  %.012 = phi i32 [ 1, %.loopexit ], [ 0, %3 ]
  ret i32 %.012
}

declare void @init_gport_X() local_unnamed_addr #1

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK11GraphicPort13BytesPerPixelEv(ptr noundef nonnull align 8 dereferenceable(14128)) local_unnamed_addr #2 align 2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK11GraphicPort8GraphMemEv(ptr noundef nonnull align 8 dereferenceable(14128)) local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK11GraphicPort8ColormapEv(ptr noundef nonnull align 8 dereferenceable(14128)) local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN11GraphicPort5ClearEh(ptr noundef nonnull align 8 dereferenceable(14128), i8 noundef zeroext) local_unnamed_addr #4 align 2

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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 = { nobuiltin allocsize(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 #4 = { inlinehint mustprogress 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 #5 = { builtin allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"vtable pointer", !8, i64 0}


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