; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Tagl/Lib/gport.ll (ZN11GraphicPort4MakeEPKciii)

; 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.GraphicPort_VC_Entry = type { ptr, i32 }

@_ZN11GraphicPort8_entriesE = external dso_local global i32, align 4
@_ZN11GraphicPort6_entryE = external dso_local global [50 x %struct.GraphicPort_VC_Entry], align 16
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [76 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

; Function Attrs: nounwind
declare ptr @getenv(ptr noundef) #1

; Function Attrs: inlinehint mustprogress nounwind willreturn memory(read) uwtable
declare i32 @atoi(ptr noundef nonnull) #2

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

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

14:                                               ; preds = %40, %4
  %15 = load i32, ptr %10, align 4, !tbaa !9
  %16 = load i32, ptr @_ZN11GraphicPort8_entriesE, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %43

18:                                               ; preds = %14
  %19 = load i32, ptr %10, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [50 x %struct.GraphicPort_VC_Entry], ptr @_ZN11GraphicPort6_entryE, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.GraphicPort_VC_Entry, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 8, !tbaa !11
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %39, label %25

25:                                               ; preds = %18
  %26 = load i32, ptr %10, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [50 x %struct.GraphicPort_VC_Entry], ptr @_ZN11GraphicPort6_entryE, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.GraphicPort_VC_Entry, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 16, !tbaa !13
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = call noundef ptr %30(ptr noundef %31, i32 noundef %32, i32 noundef %33, i32 noundef %34)
  store ptr %35, ptr %11, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %39

37:                                               ; preds = %25
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %38, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %83

39:                                               ; preds = %25, %18
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %10, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %10, align 4, !tbaa !9
  br label %14, !llvm.loop !14

43:                                               ; preds = %14
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %70, %43
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = load i32, ptr @_ZN11GraphicPort8_entriesE, align 4, !tbaa !9
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %73

48:                                               ; preds = %44
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [50 x %struct.GraphicPort_VC_Entry], ptr @_ZN11GraphicPort6_entryE, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.GraphicPort_VC_Entry, ptr %51, i32 0, i32 1
  %53 = load i32, ptr %52, align 8, !tbaa !11
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %69

55:                                               ; preds = %48
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [50 x %struct.GraphicPort_VC_Entry], ptr @_ZN11GraphicPort6_entryE, i64 0, i64 %57
  %59 = getelementptr inbounds %struct.GraphicPort_VC_Entry, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 16, !tbaa !13
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = call noundef ptr %60(ptr noundef %61, i32 noundef %62, i32 noundef %63, i32 noundef %64)
  store ptr %65, ptr %11, align 8, !tbaa !5
  %66 = icmp ne ptr %65, null
  br i1 %66, label %67, label %69

67:                                               ; preds = %55
  %68 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %68, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %83

69:                                               ; preds = %55, %48
  br label %70

70:                                               ; preds = %69
  %71 = load i32, ptr %10, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %10, align 4, !tbaa !9
  br label %44, !llvm.loop !16

73:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %74 = call ptr @getenv(ptr noundef @.str.2) #5
  store ptr %74, ptr %13, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %82

76:                                               ; preds = %73
  %77 = load ptr, ptr %13, align 8, !tbaa !5
  %78 = call i32 @atoi(ptr noundef %77) #6
  %79 = icmp sge i32 %78, 1
  br i1 %79, label %80, label %82

80:                                               ; preds = %76
  %81 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  br label %82

82:                                               ; preds = %80, %76, %73
  store ptr null, ptr %5, align 8
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  br label %83

83:                                               ; preds = %82, %67, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %84 = load ptr, ptr %5, align 8
  ret ptr %84
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint mustprogress nounwind willreturn memory(read) 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 = { "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 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 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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, !10, i64 8}
!12 = !{!"_ZTS20GraphicPort_VC_Entry", !6, i64 0, !10, i64 8}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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.GraphicPort_VC_Entry = type { ptr, i32 }

@_ZN11GraphicPort8_entriesE = external dso_local local_unnamed_addr global i32, align 4
@_ZN11GraphicPort6_entryE = external dso_local local_unnamed_addr global [50 x %struct.GraphicPort_VC_Entry], align 16
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [76 x i8], align 1

; Function Attrs: nofree nounwind memory(read)
declare noundef ptr @getenv(ptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nofree nounwind willreturn memory(read) uwtable
declare i32 @atoi(ptr nocapture noundef nonnull) local_unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN11GraphicPort4MakeEPKciii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #3 align 2 {
  %5 = load i32, ptr @_ZN11GraphicPort8_entriesE, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.preheader:                                       ; preds = %15
  %7 = icmp sgt i32 %16, 0
  br i1 %7, label %.lr.ph31, label %._crit_edge

.lr.ph:                                           ; preds = %4, %15
  %8 = phi i32 [ %16, %15 ], [ %5, %4 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %15 ], [ 0, %4 ]
  %9 = getelementptr inbounds [50 x %struct.GraphicPort_VC_Entry], ptr @_ZN11GraphicPort6_entryE, i64 0, i64 %indvars.iv
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !9
  %.not25 = icmp eq i32 %11, 0
  br i1 %.not25, label %12, label %15

12:                                               ; preds = %.lr.ph
  %13 = load ptr, ptr %9, align 16, !tbaa !12
  %14 = tail call noundef ptr %13(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %.not26 = icmp eq ptr %14, null
  br i1 %.not26, label %._crit_edge38, label %.loopexit

._crit_edge38:                                    ; preds = %12
  %.pre = load i32, ptr @_ZN11GraphicPort8_entriesE, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %._crit_edge38, %.lr.ph
  %16 = phi i32 [ %.pre, %._crit_edge38 ], [ %8, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = sext i32 %16 to i64
  %18 = icmp slt i64 %indvars.iv.next, %17
  br i1 %18, label %.lr.ph, label %.preheader, !llvm.loop !13

.lr.ph31:                                         ; preds = %.preheader, %26
  %19 = phi i32 [ %27, %26 ], [ %16, %.preheader ]
  %indvars.iv35 = phi i64 [ %indvars.iv.next36, %26 ], [ 0, %.preheader ]
  %20 = getelementptr inbounds [50 x %struct.GraphicPort_VC_Entry], ptr @_ZN11GraphicPort6_entryE, i64 0, i64 %indvars.iv35
  %21 = getelementptr inbounds i8, ptr %20, i64 8
  %22 = load i32, ptr %21, align 8, !tbaa !9
  %.not23 = icmp eq i32 %22, 0
  br i1 %.not23, label %26, label %23

23:                                               ; preds = %.lr.ph31
  %24 = load ptr, ptr %20, align 16, !tbaa !12
  %25 = tail call noundef ptr %24(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %.not24 = icmp eq ptr %25, null
  br i1 %.not24, label %._crit_edge39, label %.loopexit

._crit_edge39:                                    ; preds = %23
  %.pre40 = load i32, ptr @_ZN11GraphicPort8_entriesE, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %._crit_edge39, %.lr.ph31
  %27 = phi i32 [ %.pre40, %._crit_edge39 ], [ %19, %.lr.ph31 ]
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %28 = sext i32 %27 to i64
  %29 = icmp slt i64 %indvars.iv.next36, %28
  br i1 %29, label %.lr.ph31, label %._crit_edge, !llvm.loop !15

._crit_edge:                                      ; preds = %26, %4, %.preheader
  %30 = tail call ptr @getenv(ptr noundef nonnull @.str.2) #4
  %.not = icmp eq ptr %30, null
  br i1 %.not, label %.loopexit, label %31

31:                                               ; preds = %._crit_edge
  %32 = tail call i32 @atoi(ptr nocapture noundef %30) #5
  %33 = icmp sgt i32 %32, 0
  br i1 %33, label %34, label %.loopexit

34:                                               ; preds = %31
  %35 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  br label %.loopexit

.loopexit:                                        ; preds = %12, %23, %._crit_edge, %31, %34
  %.0 = phi ptr [ null, %34 ], [ null, %31 ], [ null, %._crit_edge ], [ %25, %23 ], [ %14, %12 ]
  ret ptr %.0
}

attributes #0 = { nofree nounwind memory(read) "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 nofree nounwind willreturn memory(read) 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 = { 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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

!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, !6, i64 8}
!10 = !{!"_ZTS20GraphicPort_VC_Entry", !11, i64 0, !6, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass