; LLVM IR from tangjz/acm-icpc :: 51nod/1426.ll (Z8seg_initii)

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

@seg = external dso_local global [2000003 x %struct.Seg], align 16
@str = external dso_local global [1000001 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z8seg_initii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.Seg, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.Seg, align 4
  %12 = alloca { i64, i32 }, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = call noundef i32 @_Z7seg_idxii(i32 noundef %13, i32 noundef %14)
  store i32 %15, ptr %5, align 4, !tbaa !5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %42

19:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 12, ptr %6) #5
  %20 = getelementptr inbounds %struct.Seg, ptr %6, i32 0, i32 0
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1000001 x i8], ptr @str, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9
  %26 = sext i8 %25 to i32
  %27 = icmp eq i32 %26, 40
  %28 = zext i1 %27 to i32
  store i32 %28, ptr %20, align 4, !tbaa !10
  %29 = getelementptr inbounds %struct.Seg, ptr %6, i32 0, i32 1
  store i32 0, ptr %29, align 4, !tbaa !12
  %30 = getelementptr inbounds %struct.Seg, ptr %6, i32 0, i32 2
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sub nsw i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [1000001 x i8], ptr @str, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !9
  %36 = sext i8 %35 to i32
  %37 = icmp eq i32 %36, 41
  %38 = zext i1 %37 to i32
  store i32 %38, ptr %30, align 4, !tbaa !13
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %40
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %41, ptr align 4 %6, i64 12, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 12, ptr %6) #5
  store i32 1, ptr %7, align 4
  br label %69

42:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = add nsw i32 %43, %44
  %46 = ashr i32 %45, 1
  store i32 %46, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %47 = load i32, ptr %3, align 4, !tbaa !5
  %48 = load i32, ptr %8, align 4, !tbaa !5
  %49 = call noundef i32 @_Z7seg_idxii(i32 noundef %47, i32 noundef %48)
  store i32 %49, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = call noundef i32 @_Z7seg_idxii(i32 noundef %51, i32 noundef %52)
  store i32 %53, ptr %10, align 4, !tbaa !5
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z8seg_initii(i32 noundef %54, i32 noundef %55)
  %56 = load i32, ptr %8, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  %58 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z8seg_initii(i32 noundef %57, i32 noundef %58)
  call void @llvm.lifetime.start.p0(i64 12, ptr %11) #5
  %59 = load i32, ptr %9, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %60
  %62 = load i32, ptr %10, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %63
  %65 = call { i64, i32 } @_Z9seg_mergeRK3SegS1_(ptr noundef nonnull align 4 dereferenceable(12) %61, ptr noundef nonnull align 4 dereferenceable(12) %64)
  store { i64, i32 } %65, ptr %12, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %11, ptr align 8 %12, i64 12, i1 false)
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %67
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %68, ptr align 4 %11, i64 12, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 12, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  store i32 0, ptr %7, align 4
  br label %69

69:                                               ; preds = %42, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  %70 = load i32, ptr %7, align 4
  switch i32 %70, label %72 [
    i32 0, label %71
    i32 1, label %71
  ]

71:                                               ; preds = %69, %69
  ret void

72:                                               ; preds = %69
  unreachable
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) #2

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local { i64, i32 } @_Z9seg_mergeRK3SegS1_(ptr noundef nonnull align 4 dereferenceable(12), ptr noundef nonnull align 4 dereferenceable(12)) #4

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"_ZTS3Seg", !6, i64 0, !6, i64 4, !6, i64 8}
!12 = !{!11, !6, i64 4}
!13 = !{!11, !6, i64 8}
!14 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 4, !5}


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

@seg = external dso_local global [2000003 x %struct.Seg], align 16
@str = external dso_local local_unnamed_addr global [1000001 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z8seg_initii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %1)
  %4 = icmp eq i32 %0, %1
  br i1 %4, label %5, label %17

5:                                                ; preds = %2
  %6 = add nsw i32 %0, -1
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [1000001 x i8], ptr @str, i64 0, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !5
  %10 = icmp eq i8 %9, 40
  %11 = zext i1 %10 to i32
  %12 = icmp eq i8 %9, 41
  %13 = zext i1 %12 to i32
  %14 = sext i32 %3 to i64
  %15 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %14
  store i32 %11, ptr %15, align 4, !tbaa !8
  %.sroa.48.0..sroa_idx = getelementptr inbounds i8, ptr %15, i64 4
  store i32 0, ptr %.sroa.48.0..sroa_idx, align 4, !tbaa !8
  br label %common.ret

common.ret:                                       ; preds = %5, %17
  %16 = phi i64 [ %14, %5 ], [ %28, %17 ]
  %.sink = phi i32 [ %13, %5 ], [ %.fca.1.extract, %17 ]
  %.sroa.5.0..sroa_idx = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %16, i32 2
  store i32 %.sink, ptr %.sroa.5.0..sroa_idx, align 4, !tbaa !8
  ret void

17:                                               ; preds = %2
  %18 = add nsw i32 %1, %0
  %19 = ashr i32 %18, 1
  %20 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %19)
  %21 = add nsw i32 %19, 1
  %22 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %21, i32 noundef %1)
  tail call void @_Z8seg_initii(i32 noundef %0, i32 noundef %19)
  tail call void @_Z8seg_initii(i32 noundef %21, i32 noundef %1)
  %23 = sext i32 %20 to i64
  %24 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %23
  %25 = sext i32 %22 to i64
  %26 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %25
  %27 = tail call { i64, i32 } @_Z9seg_mergeRK3SegS1_(ptr noundef nonnull align 4 dereferenceable(12) %24, ptr noundef nonnull align 4 dereferenceable(12) %26)
  %.fca.0.extract = extractvalue { i64, i32 } %27, 0
  %.fca.1.extract = extractvalue { i64, i32 } %27, 1
  %28 = sext i32 %3 to i64
  %29 = getelementptr inbounds [2000003 x %struct.Seg], ptr @seg, i64 0, i64 %28
  store i64 %.fca.0.extract, ptr %29, align 4
  br label %common.ret
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local { i64, i32 } @_Z9seg_mergeRK3SegS1_(ptr noundef nonnull align 4 dereferenceable(12), ptr noundef nonnull align 4 dereferenceable(12)) local_unnamed_addr #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" }
attributes #2 = { 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" }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}


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