; LLVM IR from tangjz/acm-icpc :: poj (1061)

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

@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4
@m = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@l = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z5exgcdiiRiS_(i32 noundef, i32 noundef, ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #1 {
  %1 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  br label %2

2:                                                ; preds = %62, %0
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @x, ptr noundef @y, ptr noundef @m, ptr noundef @n, ptr noundef @l)
  %4 = icmp eq i32 %3, 5
  br i1 %4, label %5, label %63

5:                                                ; preds = %2
  %6 = load i32, ptr @m, align 4, !tbaa !5
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = sub nsw i32 %6, %7
  %9 = load i32, ptr @l, align 4, !tbaa !5
  %10 = srem i32 %8, %9
  store i32 %10, ptr @m, align 4, !tbaa !5
  %11 = load i32, ptr @m, align 4, !tbaa !5
  %12 = icmp slt i32 %11, 0
  br i1 %12, label %13, label %17

13:                                               ; preds = %5
  %14 = load i32, ptr @l, align 4, !tbaa !5
  %15 = load i32, ptr @m, align 4, !tbaa !5
  %16 = add nsw i32 %15, %14
  store i32 %16, ptr @m, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %13, %5
  %18 = load i32, ptr @y, align 4, !tbaa !5
  %19 = load i32, ptr @x, align 4, !tbaa !5
  %20 = sub nsw i32 %18, %19
  %21 = load i32, ptr @l, align 4, !tbaa !5
  %22 = srem i32 %20, %21
  store i32 %22, ptr @n, align 4, !tbaa !5
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %25, label %29

25:                                               ; preds = %17
  %26 = load i32, ptr @l, align 4, !tbaa !5
  %27 = load i32, ptr @n, align 4, !tbaa !5
  %28 = add nsw i32 %27, %26
  store i32 %28, ptr @n, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %25, %17
  %30 = load i32, ptr @m, align 4, !tbaa !5
  %31 = load i32, ptr @l, align 4, !tbaa !5
  %32 = call noundef i32 @_Z5exgcdiiRiS_(i32 noundef %30, i32 noundef %31, ptr noundef nonnull align 4 dereferenceable(4) @x, ptr noundef nonnull align 4 dereferenceable(4) @y)
  store i32 %32, ptr @r, align 4, !tbaa !5
  %33 = load i32, ptr @x, align 4, !tbaa !5
  %34 = icmp slt i32 %33, 0
  br i1 %34, label %35, label %39

35:                                               ; preds = %29
  %36 = load i32, ptr @l, align 4, !tbaa !5
  %37 = load i32, ptr @x, align 4, !tbaa !5
  %38 = add nsw i32 %37, %36
  store i32 %38, ptr @x, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %35, %29
  %40 = load i32, ptr @n, align 4, !tbaa !5
  %41 = load i32, ptr @r, align 4, !tbaa !5
  %42 = srem i32 %40, %41
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %46

44:                                               ; preds = %39
  %45 = call i32 @puts(ptr noundef @.str.1)
  br label %62

46:                                               ; preds = %39
  %47 = load i32, ptr @n, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = load i32, ptr @r, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = sdiv i64 %48, %50
  %52 = load i32, ptr @x, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = mul nsw i64 %51, %53
  %55 = load i32, ptr @l, align 4, !tbaa !5
  %56 = load i32, ptr @r, align 4, !tbaa !5
  %57 = sdiv i32 %55, %56
  %58 = sext i32 %57 to i64
  %59 = srem i64 %54, %58
  %60 = trunc i64 %59 to i32
  %61 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %60)
  br label %62

62:                                               ; preds = %46, %44
  br label %2, !llvm.loop !9

63:                                               ; preds = %2
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

declare i32 @puts(ptr noundef) #2

declare i32 @printf(ptr noundef, ...) #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 = { mustprogress norecurse 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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4
@m = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@l = external dso_local global i32, align 4
@r = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z5exgcdiiRiS_(i32 noundef, i32 noundef, ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @x, ptr noundef nonnull @y, ptr noundef nonnull @m, ptr noundef nonnull @n, ptr noundef nonnull @l)
  %2 = icmp eq i32 %1, 5
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %40
  %3 = load i32, ptr @m, align 4, !tbaa !5
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %5 = sub nsw i32 %3, %4
  %6 = load i32, ptr @l, align 4, !tbaa !5
  %7 = srem i32 %5, %6
  %8 = icmp slt i32 %7, 0
  %9 = select i1 %8, i32 %6, i32 0
  %spec.select = add nsw i32 %7, %9
  store i32 %spec.select, ptr @m, align 4, !tbaa !5
  %10 = load i32, ptr @y, align 4, !tbaa !5
  %11 = load i32, ptr @x, align 4, !tbaa !5
  %12 = sub nsw i32 %10, %11
  %13 = srem i32 %12, %6
  %14 = icmp slt i32 %13, 0
  %15 = select i1 %14, i32 %6, i32 0
  %storemerge3 = add nsw i32 %13, %15
  store i32 %storemerge3, ptr @n, align 4, !tbaa !5
  %16 = tail call noundef i32 @_Z5exgcdiiRiS_(i32 noundef %spec.select, i32 noundef %6, ptr noundef nonnull align 4 dereferenceable(4) @x, ptr noundef nonnull align 4 dereferenceable(4) @y)
  store i32 %16, ptr @r, align 4, !tbaa !5
  %17 = load i32, ptr @x, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %.lr.ph
  %20 = load i32, ptr @l, align 4, !tbaa !5
  %21 = add nsw i32 %20, %17
  store i32 %21, ptr @x, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %19, %.lr.ph
  %23 = phi i32 [ %21, %19 ], [ %17, %.lr.ph ]
  %24 = load i32, ptr @n, align 4, !tbaa !5
  %25 = srem i32 %24, %16
  %.not = icmp eq i32 %25, 0
  br i1 %.not, label %28, label %26

26:                                               ; preds = %22
  %27 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %40

28:                                               ; preds = %22
  %29 = sext i32 %24 to i64
  %30 = sext i32 %16 to i64
  %31 = sdiv i64 %29, %30
  %32 = sext i32 %23 to i64
  %33 = mul nsw i64 %31, %32
  %34 = load i32, ptr @l, align 4, !tbaa !5
  %35 = sdiv i32 %34, %16
  %36 = sext i32 %35 to i64
  %37 = srem i64 %33, %36
  %38 = trunc nsw i64 %37 to i32
  %39 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %38)
  br label %40

40:                                               ; preds = %28, %26
  %41 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @x, ptr noundef nonnull @y, ptr noundef nonnull @m, ptr noundef nonnull @n, ptr noundef nonnull @l)
  %42 = icmp eq i32 %41, 5
  br i1 %42, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %40, %0
  ret i32 0
}

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) 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 = { mustprogress norecurse 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" }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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