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

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @LinG_SwapBuffers() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = load i64, ptr @LinG_mode, align 8, !tbaa !5
  %4 = and i64 %3, 2
  %5 = icmp ne i64 %4, 0
  br i1 %5, label %45, label %6

6:                                                ; preds = %0
  %7 = load i64, ptr @LinG_mode, align 8, !tbaa !5
  %8 = and i64 %7, 1
  %9 = icmp ne i64 %8, 0
  br i1 %9, label %10, label %18

10:                                               ; preds = %6
  %11 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %12 = load ptr, ptr @LinG_graph_mem, align 8, !tbaa !9
  %13 = load i32, ptr @LinG_width, align 4, !tbaa !11
  %14 = load i32, ptr @LinG_height, align 4, !tbaa !11
  %15 = load ptr, ptr %11, align 8, !tbaa !13
  %16 = getelementptr inbounds ptr, ptr %15, i64 10
  %17 = load ptr, ptr %16, align 8
  call void %17(ptr noundef nonnull align 8 dereferenceable(14128) %11, ptr noundef %12, i32 noundef %13, i32 noundef %14)
  br label %44

18:                                               ; preds = %6
  %19 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = getelementptr inbounds ptr, ptr %20, i64 8
  %22 = load ptr, ptr %21, align 8
  %23 = call noundef i32 %22(ptr noundef nonnull align 8 dereferenceable(14128) %19)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %24 = load i32, ptr @LinG_width, align 4, !tbaa !11
  store i32 %24, ptr %1, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %25 = load i32, ptr @LinG_height, align 4, !tbaa !11
  store i32 %25, ptr %2, align 4, !tbaa !11
  %26 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %27 = call noundef i32 @_ZNK11GraphicPort5WidthEv(ptr noundef nonnull align 8 dereferenceable(14128) %26)
  store i32 %27, ptr @LinG_width, align 4, !tbaa !11
  %28 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %29 = call noundef i32 @_ZNK11GraphicPort6HeightEv(ptr noundef nonnull align 8 dereferenceable(14128) %28)
  store i32 %29, ptr @LinG_height, align 4, !tbaa !11
  %30 = load i32, ptr @LinG_width, align 4, !tbaa !11
  %31 = load i32, ptr %1, align 4, !tbaa !11
  %32 = icmp eq i32 %30, %31
  br i1 %32, label %33, label %43

33:                                               ; preds = %18
  %34 = load i32, ptr @LinG_height, align 4, !tbaa !11
  %35 = load i32, ptr %2, align 4, !tbaa !11
  %36 = icmp eq i32 %34, %35
  br i1 %36, label %37, label %43

37:                                               ; preds = %33
  %38 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %39 = load ptr, ptr @LinG_graph_mem, align 8, !tbaa !9
  %40 = load ptr, ptr %38, align 8, !tbaa !13
  %41 = getelementptr inbounds ptr, ptr %40, i64 9
  %42 = load ptr, ptr %41, align 8
  call void %42(ptr noundef nonnull align 8 dereferenceable(14128) %38, ptr noundef %39)
  br label %43

43:                                               ; preds = %37, %33, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %44

44:                                               ; preds = %43, %10
  br label %45

45:                                               ; preds = %44, %0
  %46 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = getelementptr inbounds ptr, ptr %47, i64 8
  %49 = load ptr, ptr %48, align 8
  %50 = call noundef i32 %49(ptr noundef nonnull align 8 dereferenceable(14128) %46)
  ret void
}

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"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_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

; Function Attrs: mustprogress uwtable
define dso_local void @LinG_SwapBuffers() local_unnamed_addr #0 {
  %1 = load i64, ptr @LinG_mode, align 8, !tbaa !5
  %2 = and i64 %1, 2
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %3, label %33

3:                                                ; preds = %0
  %4 = and i64 %1, 1
  %.not2 = icmp eq i64 %4, 0
  %5 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  br i1 %.not2, label %13, label %6

6:                                                ; preds = %3
  %7 = load ptr, ptr @LinG_graph_mem, align 8, !tbaa !9
  %8 = load i32, ptr @LinG_width, align 4, !tbaa !11
  %9 = load i32, ptr @LinG_height, align 4, !tbaa !11
  %10 = load ptr, ptr %5, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %10, i64 80
  %12 = load ptr, ptr %11, align 8
  tail call void %12(ptr noundef nonnull align 8 dereferenceable(14128) %5, ptr noundef %7, i32 noundef %8, i32 noundef %9)
  br label %33

13:                                               ; preds = %3
  %14 = load ptr, ptr %5, align 8, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %14, i64 64
  %16 = load ptr, ptr %15, align 8
  %17 = tail call noundef i32 %16(ptr noundef nonnull align 8 dereferenceable(14128) %5)
  %18 = load i32, ptr @LinG_width, align 4, !tbaa !11
  %19 = load i32, ptr @LinG_height, align 4, !tbaa !11
  %20 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %21 = tail call noundef i32 @_ZNK11GraphicPort5WidthEv(ptr noundef nonnull align 8 dereferenceable(14128) %20)
  store i32 %21, ptr @LinG_width, align 4, !tbaa !11
  %22 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %23 = tail call noundef i32 @_ZNK11GraphicPort6HeightEv(ptr noundef nonnull align 8 dereferenceable(14128) %22)
  store i32 %23, ptr @LinG_height, align 4, !tbaa !11
  %24 = load i32, ptr @LinG_width, align 4, !tbaa !11
  %25 = icmp eq i32 %24, %18
  %26 = icmp eq i32 %23, %19
  %or.cond = select i1 %25, i1 %26, i1 false
  br i1 %or.cond, label %27, label %33

27:                                               ; preds = %13
  %28 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %29 = load ptr, ptr @LinG_graph_mem, align 8, !tbaa !9
  %30 = load ptr, ptr %28, align 8, !tbaa !13
  %31 = getelementptr inbounds i8, ptr %30, i64 72
  %32 = load ptr, ptr %31, align 8
  tail call void %32(ptr noundef nonnull align 8 dereferenceable(14128) %28, ptr noundef %29)
  br label %33

33:                                               ; preds = %13, %27, %6, %0
  %34 = load ptr, ptr @LinG_GP, align 8, !tbaa !9
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = getelementptr inbounds i8, ptr %35, i64 64
  %37 = load ptr, ptr %36, align 8
  %38 = tail call noundef i32 %37(ptr noundef nonnull align 8 dereferenceable(14128) %34)
  ret void
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK11GraphicPort6HeightEv(ptr noundef nonnull align 8 dereferenceable(14128)) local_unnamed_addr #1 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 = { 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" }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"vtable pointer", !8, i64 0}


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