; LLVM IR from pandax381/microps :: net (net_timer_handler)

; 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.timeval = type { i64, i64 }
%struct.net_timer = type { ptr, [16 x i8], %struct.timeval, %struct.timeval, ptr }

@timers = external hidden 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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind
declare i32 @gettimeofday(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @net_timer_handler() #3 {
  %1 = alloca ptr, align 8
  %2 = alloca %struct.timeval, align 8
  %3 = alloca %struct.timeval, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #4
  %4 = load ptr, ptr @timers, align 8, !tbaa !5
  store ptr %4, ptr %1, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %75, %0
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %79

8:                                                ; preds = %5
  %9 = call i32 @gettimeofday(ptr noundef %2, ptr noundef null) #4
  br label %10

10:                                               ; preds = %8
  %11 = getelementptr inbounds %struct.timeval, ptr %2, i32 0, i32 0
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %1, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.net_timer, ptr %13, i32 0, i32 3
  %15 = getelementptr inbounds %struct.timeval, ptr %14, i32 0, i32 0
  %16 = load i64, ptr %15, align 8, !tbaa !12
  %17 = sub nsw i64 %12, %16
  %18 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 0
  store i64 %17, ptr %18, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.timeval, ptr %2, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !14
  %21 = load ptr, ptr %1, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.net_timer, ptr %21, i32 0, i32 3
  %23 = getelementptr inbounds %struct.timeval, ptr %22, i32 0, i32 1
  %24 = load i64, ptr %23, align 8, !tbaa !15
  %25 = sub nsw i64 %20, %24
  %26 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 1
  store i64 %25, ptr %26, align 8, !tbaa !14
  %27 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = icmp slt i64 %28, 0
  br i1 %29, label %30, label %37

30:                                               ; preds = %10
  %31 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 0
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = add nsw i64 %32, -1
  store i64 %33, ptr %31, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !14
  %36 = add nsw i64 %35, 1000000
  store i64 %36, ptr %34, align 8, !tbaa !14
  br label %37

37:                                               ; preds = %30, %10
  br label %38

38:                                               ; preds = %37
  br label %39

39:                                               ; preds = %38
  %40 = load ptr, ptr %1, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.net_timer, ptr %40, i32 0, i32 2
  %42 = getelementptr inbounds %struct.timeval, ptr %41, i32 0, i32 0
  %43 = load i64, ptr %42, align 8, !tbaa !16
  %44 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 0
  %45 = load i64, ptr %44, align 8, !tbaa !9
  %46 = icmp eq i64 %43, %45
  br i1 %46, label %47, label %56

47:                                               ; preds = %39
  %48 = load ptr, ptr %1, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.net_timer, ptr %48, i32 0, i32 2
  %50 = getelementptr inbounds %struct.timeval, ptr %49, i32 0, i32 1
  %51 = load i64, ptr %50, align 8, !tbaa !17
  %52 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !14
  %54 = icmp slt i64 %51, %53
  %55 = zext i1 %54 to i32
  br label %65

56:                                               ; preds = %39
  %57 = load ptr, ptr %1, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.net_timer, ptr %57, i32 0, i32 2
  %59 = getelementptr inbounds %struct.timeval, ptr %58, i32 0, i32 0
  %60 = load i64, ptr %59, align 8, !tbaa !16
  %61 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 0
  %62 = load i64, ptr %61, align 8, !tbaa !9
  %63 = icmp slt i64 %60, %62
  %64 = zext i1 %63 to i32
  br label %65

65:                                               ; preds = %56, %47
  %66 = phi i32 [ %55, %47 ], [ %64, %56 ]
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %68, label %74

68:                                               ; preds = %65
  %69 = load ptr, ptr %1, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.net_timer, ptr %69, i32 0, i32 4
  %71 = load ptr, ptr %70, align 8, !tbaa !18
  call void %71()
  %72 = load ptr, ptr %1, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.net_timer, ptr %72, i32 0, i32 3
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %73, ptr align 8 %2, i64 16, i1 false), !tbaa.struct !19
  br label %74

74:                                               ; preds = %68, %65
  br label %75

75:                                               ; preds = %74
  %76 = load ptr, ptr %1, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.net_timer, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !21
  store ptr %78, ptr %1, align 8, !tbaa !5
  br label %5, !llvm.loop !22

79:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret i32 0
}

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

!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 = !{!"timeval", !11, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !11, i64 40}
!13 = !{!"net_timer", !6, i64 0, !7, i64 8, !10, i64 24, !10, i64 40, !6, i64 56}
!14 = !{!10, !11, i64 8}
!15 = !{!13, !11, i64 48}
!16 = !{!13, !11, i64 24}
!17 = !{!13, !11, i64 32}
!18 = !{!13, !6, i64 56}
!19 = !{i64 0, i64 8, !20, i64 8, i64 8, !20}
!20 = !{!11, !11, i64 0}
!21 = !{!13, !6, i64 0}
!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.timeval = type { i64, i64 }

@timers = external hidden local_unnamed_addr global ptr, align 8

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @net_timer_handler() local_unnamed_addr #3 {
  %1 = alloca %struct.timeval, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #4
  %.015 = load ptr, ptr @timers, align 8, !tbaa !5
  %.not16 = icmp eq ptr %.015, null
  br i1 %.not16, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0
  %2 = getelementptr inbounds i8, ptr %1, i64 8
  br label %3

3:                                                ; preds = %.lr.ph, %27
  %.017 = phi ptr [ %.015, %.lr.ph ], [ %.0, %27 ]
  %4 = call i32 @gettimeofday(ptr noundef nonnull %1, ptr noundef null) #4
  %5 = load i64, ptr %1, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %.017, i64 40
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %8 = sub nsw i64 %5, %7
  %9 = load i64, ptr %2, align 8, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %.017, i64 48
  %11 = load i64, ptr %10, align 8, !tbaa !15
  %12 = sub nsw i64 %9, %11
  %.lobit = ashr i64 %12, 63
  %.sroa.0.0 = add nsw i64 %.lobit, %8
  %13 = getelementptr inbounds i8, ptr %.017, i64 24
  %14 = load i64, ptr %13, align 8, !tbaa !16
  %15 = icmp eq i64 %14, %.sroa.0.0
  br i1 %15, label %16, label %22

16:                                               ; preds = %3
  %17 = icmp slt i64 %12, 0
  %18 = add nsw i64 %12, 1000000
  %.sroa.7.0 = select i1 %17, i64 %18, i64 %12
  %19 = getelementptr inbounds i8, ptr %.017, i64 32
  %20 = load i64, ptr %19, align 8, !tbaa !17
  %21 = icmp slt i64 %20, %.sroa.7.0
  br i1 %21, label %24, label %27

22:                                               ; preds = %3
  %23 = icmp slt i64 %14, %.sroa.0.0
  br i1 %23, label %24, label %27

24:                                               ; preds = %16, %22
  %25 = getelementptr inbounds i8, ptr %.017, i64 56
  %26 = load ptr, ptr %25, align 8, !tbaa !18
  tail call void %26() #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %6, ptr noundef nonnull align 8 dereferenceable(16) %1, i64 16, i1 false), !tbaa.struct !19
  br label %27

27:                                               ; preds = %16, %22, %24
  %.0 = load ptr, ptr %.017, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %3, !llvm.loop !21

._crit_edge:                                      ; preds = %27, %0
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #4
  ret i32 0
}

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

!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 = !{!"timeval", !11, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !11, i64 40}
!13 = !{!"net_timer", !6, i64 0, !7, i64 8, !10, i64 24, !10, i64 40, !6, i64 56}
!14 = !{!10, !11, i64 8}
!15 = !{!13, !11, i64 48}
!16 = !{!13, !11, i64 24}
!17 = !{!13, !11, i64 32}
!18 = !{!13, !6, i64 56}
!19 = !{i64 0, i64 8, !20, i64 8, i64 8, !20}
!20 = !{!11, !11, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass