; LLVM IR from tangjz/acm-icpc :: self_training/2017-01-14/C.ll (Z6updateRiiii)

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

@que = external dso_local global [100001 x i32], align 16
@seg_tot = external dso_local global i32, align 4
@seg = external dso_local global [1800019 x %struct.Segment], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6updateRiiii(ptr noundef nonnull align 4 dereferenceable(4) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %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
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %13, ptr %9, align 4, !tbaa !9
  %14 = load i32, ptr %9, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %15
  %17 = load i32, ptr @seg_tot, align 4, !tbaa !9
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr @seg_tot, align 4, !tbaa !9
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  store i32 %18, ptr %19, align 4, !tbaa !9
  %20 = sext i32 %18 to i64
  %21 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %20
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %21, ptr align 16 %16, i64 16, i1 false), !tbaa.struct !11
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load i32, ptr %22, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.Segment, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !12
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %26, align 8, !tbaa !12
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.Segment, ptr %32, i32 0, i32 3
  %34 = load i32, ptr %33, align 4, !tbaa !14
  %35 = sext i32 %34 to i64
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100001 x i32], ptr @que, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = mul nsw i64 %35, %40
  %42 = srem i64 %41, 1000000007
  %43 = trunc i64 %42 to i32
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.Segment, ptr %47, i32 0, i32 3
  store i32 %43, ptr %48, align 4, !tbaa !14
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = icmp eq i32 %49, %50
  br i1 %51, label %52, label %53

52:                                               ; preds = %4
  store i32 1, ptr %10, align 4
  br label %81

53:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = add nsw i32 %54, %55
  %57 = ashr i32 %56, 1
  store i32 %57, ptr %11, align 4, !tbaa !9
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = icmp sle i32 %58, %59
  br i1 %60, label %61, label %70

61:                                               ; preds = %53
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = load i32, ptr %62, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.Segment, ptr %65, i32 0, i32 0
  %67 = load i32, ptr %6, align 4, !tbaa !9
  %68 = load i32, ptr %11, align 4, !tbaa !9
  %69 = load i32, ptr %8, align 4, !tbaa !9
  call void @_Z6updateRiiii(ptr noundef nonnull align 4 dereferenceable(4) %66, i32 noundef %67, i32 noundef %68, i32 noundef %69)
  br label %80

70:                                               ; preds = %53
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = load i32, ptr %71, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %73
  %75 = getelementptr inbounds %struct.Segment, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %11, align 4, !tbaa !9
  %77 = add nsw i32 %76, 1
  %78 = load i32, ptr %7, align 4, !tbaa !9
  %79 = load i32, ptr %8, align 4, !tbaa !9
  call void @_Z6updateRiiii(ptr noundef nonnull align 4 dereferenceable(4) %75, i32 noundef %77, i32 noundef %78, i32 noundef %79)
  br label %80

80:                                               ; preds = %70, %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  store i32 0, ptr %10, align 4
  br label %81

81:                                               ; preds = %80, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %82 = load i32, ptr %10, align 4
  switch i32 %82, label %84 [
    i32 0, label %83
    i32 1, label %83
  ]

83:                                               ; preds = %81, %81
  ret void

84:                                               ; preds = %81
  unreachable
}

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

; 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) #2

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

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


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

@que = external dso_local local_unnamed_addr global [100001 x i32], align 16
@seg_tot = external dso_local local_unnamed_addr global i32, align 4
@seg = external dso_local local_unnamed_addr global [1800019 x %struct.Segment], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z6updateRiiii(ptr nocapture noundef nonnull align 4 dereferenceable(4) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sext i32 %3 to i64
  %6 = getelementptr inbounds [100001 x i32], ptr @que, i64 0, i64 %5
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %37, %4
  %.tr.ph = phi ptr [ %38, %37 ], [ %0, %4 ]
  %.tr24.ph = phi i32 [ %39, %37 ], [ %1, %4 ]
  %.tr25.ph = phi i32 [ %.tr25, %37 ], [ %2, %4 ]
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.outer, %35
  %.tr = phi ptr [ %36, %35 ], [ %.tr.ph, %tailrecurse.outer ]
  %.tr25 = phi i32 [ %32, %35 ], [ %.tr25.ph, %tailrecurse.outer ]
  %7 = load i32, ptr %.tr, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %8
  %10 = load i32, ptr @seg_tot, align 4, !tbaa !5
  %11 = add nsw i32 %10, 1
  store i32 %11, ptr @seg_tot, align 4, !tbaa !5
  store i32 %11, ptr %.tr, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %13, ptr noundef nonnull align 16 dereferenceable(16) %9, i64 16, i1 false), !tbaa.struct !9
  %14 = load i32, ptr %.tr, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %15, i32 2
  %17 = load i32, ptr %16, align 8, !tbaa !10
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %16, align 8, !tbaa !10
  %19 = load i32, ptr %.tr, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %20, i32 3
  %22 = load i32, ptr %21, align 4, !tbaa !12
  %23 = sext i32 %22 to i64
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = mul nsw i64 %25, %23
  %27 = srem i64 %26, 1000000007
  %28 = trunc nsw i64 %27 to i32
  store i32 %28, ptr %21, align 4, !tbaa !12
  %29 = icmp eq i32 %.tr24.ph, %.tr25
  br i1 %29, label %40, label %30

30:                                               ; preds = %tailrecurse
  %31 = add nsw i32 %.tr25, %.tr24.ph
  %32 = ashr i32 %31, 1
  %.not = icmp slt i32 %32, %3
  %33 = load i32, ptr %.tr, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  br i1 %.not, label %37, label %35

35:                                               ; preds = %30
  %36 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %34
  br label %tailrecurse

37:                                               ; preds = %30
  %38 = getelementptr inbounds [1800019 x %struct.Segment], ptr @seg, i64 0, i64 %34, i32 1
  %39 = add nsw i32 %32, 1
  br label %tailrecurse.outer

40:                                               ; preds = %tailrecurse
  ret void
}

; 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

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

!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 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 4, !5, i64 12, i64 4, !5}
!10 = !{!11, !6, i64 8}
!11 = !{!"_ZTS7Segment", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!12 = !{!11, !6, i64 12}


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