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

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

@a = external dso_local global i64, align 8
@b = external dso_local global i64, align 8
@c = external dso_local global i64, align 8
@d = external dso_local global i64, align 8
@k = external dso_local global i64, align 8
@r = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [17 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z5exgcdxxRxS_(i64 noundef, i64 noundef, ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #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 = %58, %0
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @a, ptr noundef @b, ptr noundef @c, ptr noundef @k)
  %4 = icmp eq i32 %3, 4
  br i1 %4, label %5, label %14

5:                                                ; preds = %2
  %6 = load i64, ptr @a, align 8, !tbaa !5
  %7 = load i64, ptr @b, align 8, !tbaa !5
  %8 = add nsw i64 %6, %7
  %9 = load i64, ptr @c, align 8, !tbaa !5
  %10 = add nsw i64 %8, %9
  %11 = load i64, ptr @k, align 8, !tbaa !5
  %12 = add nsw i64 %10, %11
  %13 = icmp ne i64 %12, 0
  br label %14

14:                                               ; preds = %5, %2
  %15 = phi i1 [ false, %2 ], [ %13, %5 ]
  br i1 %15, label %16, label %59

16:                                               ; preds = %14
  %17 = load i64, ptr @k, align 8, !tbaa !5
  %18 = shl i64 1, %17
  store i64 %18, ptr @k, align 8, !tbaa !5
  %19 = load i64, ptr @b, align 8, !tbaa !5
  %20 = load i64, ptr @a, align 8, !tbaa !5
  %21 = sub nsw i64 %19, %20
  %22 = load i64, ptr @k, align 8, !tbaa !5
  %23 = srem i64 %21, %22
  store i64 %23, ptr @d, align 8, !tbaa !5
  %24 = load i64, ptr @d, align 8, !tbaa !5
  %25 = icmp slt i64 %24, 0
  br i1 %25, label %26, label %30

26:                                               ; preds = %16
  %27 = load i64, ptr @k, align 8, !tbaa !5
  %28 = load i64, ptr @d, align 8, !tbaa !5
  %29 = add nsw i64 %28, %27
  store i64 %29, ptr @d, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %26, %16
  %31 = load i64, ptr @c, align 8, !tbaa !5
  %32 = load i64, ptr @k, align 8, !tbaa !5
  %33 = call noundef i64 @_Z5exgcdxxRxS_(i64 noundef %31, i64 noundef %32, ptr noundef nonnull align 8 dereferenceable(8) @a, ptr noundef nonnull align 8 dereferenceable(8) @b)
  store i64 %33, ptr @r, align 8, !tbaa !5
  %34 = load i64, ptr @a, align 8, !tbaa !5
  %35 = icmp slt i64 %34, 0
  br i1 %35, label %36, label %40

36:                                               ; preds = %30
  %37 = load i64, ptr @k, align 8, !tbaa !5
  %38 = load i64, ptr @a, align 8, !tbaa !5
  %39 = add nsw i64 %38, %37
  store i64 %39, ptr @a, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %36, %30
  %41 = load i64, ptr @d, align 8, !tbaa !5
  %42 = load i64, ptr @r, align 8, !tbaa !5
  %43 = srem i64 %41, %42
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %47

45:                                               ; preds = %40
  %46 = call i32 @puts(ptr noundef @.str.1)
  br label %58

47:                                               ; preds = %40
  %48 = load i64, ptr @d, align 8, !tbaa !5
  %49 = load i64, ptr @r, align 8, !tbaa !5
  %50 = sdiv i64 %48, %49
  %51 = load i64, ptr @a, align 8, !tbaa !5
  %52 = mul nsw i64 %50, %51
  %53 = load i64, ptr @k, align 8, !tbaa !5
  %54 = load i64, ptr @r, align 8, !tbaa !5
  %55 = sdiv i64 %53, %54
  %56 = srem i64 %52, %55
  %57 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %56)
  br label %58

58:                                               ; preds = %47, %45
  br label %2, !llvm.loop !9

59:                                               ; preds = %14
  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 = !{!"long long", !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"

@a = external dso_local global i64, align 8
@b = external dso_local global i64, align 8
@c = external dso_local global i64, align 8
@d = external dso_local local_unnamed_addr global i64, align 8
@k = external dso_local global i64, align 8
@r = external dso_local local_unnamed_addr global i64, align 8
@.str = external hidden unnamed_addr constant [17 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z5exgcdxxRxS_(i64 noundef, i64 noundef, ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) 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 @a, ptr noundef nonnull @b, ptr noundef nonnull @c, ptr noundef nonnull @k)
  %2 = icmp eq i32 %1, 4
  br i1 %2, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %0, %33
  %3 = load i64, ptr @a, align 8, !tbaa !5
  %4 = load i64, ptr @b, align 8, !tbaa !5
  %5 = add nsw i64 %4, %3
  %6 = load i64, ptr @c, align 8, !tbaa !5
  %7 = add nsw i64 %5, %6
  %8 = load i64, ptr @k, align 8, !tbaa !5
  %9 = sub i64 0, %8
  %.not = icmp eq i64 %7, %9
  br i1 %.not, label %.critedge, label %10

10:                                               ; preds = %.lr.ph
  %11 = shl nuw i64 1, %8
  store i64 %11, ptr @k, align 8, !tbaa !5
  %12 = sub nsw i64 %4, %3
  %13 = add i64 %11, -1
  %spec.select = and i64 %12, %13
  store i64 %spec.select, ptr @d, align 8, !tbaa !5
  %14 = tail call noundef i64 @_Z5exgcdxxRxS_(i64 noundef %6, i64 noundef %11, ptr noundef nonnull align 8 dereferenceable(8) @a, ptr noundef nonnull align 8 dereferenceable(8) @b)
  store i64 %14, ptr @r, align 8, !tbaa !5
  %15 = load i64, ptr @a, align 8, !tbaa !5
  %16 = icmp slt i64 %15, 0
  br i1 %16, label %17, label %20

17:                                               ; preds = %10
  %18 = load i64, ptr @k, align 8, !tbaa !5
  %19 = add nsw i64 %18, %15
  store i64 %19, ptr @a, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %17, %10
  %21 = phi i64 [ %19, %17 ], [ %15, %10 ]
  %22 = load i64, ptr @d, align 8, !tbaa !5
  %23 = srem i64 %22, %14
  %24 = sdiv i64 %22, %14
  %.not2 = icmp eq i64 %23, 0
  br i1 %.not2, label %27, label %25

25:                                               ; preds = %20
  %26 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %33

27:                                               ; preds = %20
  %28 = mul nsw i64 %21, %24
  %29 = load i64, ptr @k, align 8, !tbaa !5
  %30 = sdiv i64 %29, %14
  %31 = srem i64 %28, %30
  %32 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %31)
  br label %33

33:                                               ; preds = %27, %25
  %34 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @a, ptr noundef nonnull @b, ptr noundef nonnull @c, ptr noundef nonnull @k)
  %35 = icmp eq i32 %34, 4
  br i1 %35, label %.lr.ph, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %.lr.ph, %33, %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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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