program main
    implicit none
    integer k, a, b, c
    integer :: total = 0
    read *, k

    do a=1, k
        do b = 1, k
            do c = 1, k
                total = total + euclid(a, euclid(c, b))
            enddo
        enddo
    enddo
    print *, total

contains
    integer function euclid(a, b)
        integer, intent(in) :: a,b
        integer bigger, smaller, rem

        if ( a < b ) then
            bigger = b
            smaller = a
        else
            bigger = a
            smaller = b
        endif

        do
            rem = mod(bigger, smaller)
            if ( rem == 0 ) then
                euclid = smaller
                exit
            else
                bigger = smaller
                smaller = rem
            endif
        enddo
    end function
end program main

; ModuleID = 'FIRModule'
source_filename = "FIRModule"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@_QFEtotal = internal unnamed_addr global i32 0
@_QQcl.cd8fbcaed489dec1fb3cddc641de00b3 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_1807/source.f\00"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

define void @_QQmain() local_unnamed_addr {
  %1 = alloca i32, align 4
  %2 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.cd8fbcaed489dec1fb3cddc641de00b3, i32 5)
  %3 = call i1 @_FortranAioInputInteger(ptr %2, ptr nonnull %1, i32 4)
  %4 = call i32 @_FortranAioEndIoStatement(ptr %2)
  %5 = load i32, ptr %1, align 4, !tbaa !3
  %6 = sext i32 %5 to i64
  %7 = icmp sgt i32 %5, 0
  br i1 %7, label %.preheader14.us.us.preheader, label %._crit_edge

.preheader14.us.us.preheader:                     ; preds = %0
  %_QFEtotal.promoted = load i32, ptr @_QFEtotal, align 4, !tbaa !3
  br label %.preheader14.us.us

.preheader14.us.us:                               ; preds = %.preheader14.us.us.preheader, %._crit_edge19.split.us.us.us
  %8 = phi i64 [ %30, %._crit_edge19.split.us.us.us ], [ %6, %.preheader14.us.us.preheader ]
  %9 = phi i32 [ %29, %._crit_edge19.split.us.us.us ], [ 1, %.preheader14.us.us.preheader ]
  %.lcssa.us.lcssa2223.us.us = phi i32 [ %22, %._crit_edge19.split.us.us.us ], [ %_QFEtotal.promoted, %.preheader14.us.us.preheader ]
  br label %.preheader.us.us.us

.preheader.us.us.us:                              ; preds = %._crit_edge.us.us.us, %.preheader14.us.us
  %10 = phi i64 [ %6, %.preheader14.us.us ], [ %27, %._crit_edge.us.us.us ]
  %11 = phi i32 [ 1, %.preheader14.us.us ], [ %26, %._crit_edge.us.us.us ]
  %.lcssa1718.us.us.us = phi i32 [ %.lcssa.us.lcssa2223.us.us, %.preheader14.us.us ], [ %22, %._crit_edge.us.us.us ]
  br label %12

12:                                               ; preds = %_QFPeuclid.exit7.us.us.us, %.preheader.us.us.us
  %13 = phi i64 [ %6, %.preheader.us.us.us ], [ %24, %_QFPeuclid.exit7.us.us.us ]
  %14 = phi i32 [ 1, %.preheader.us.us.us ], [ %23, %_QFPeuclid.exit7.us.us.us ]
  %15 = phi i32 [ %.lcssa1718.us.us.us, %.preheader.us.us.us ], [ %22, %_QFPeuclid.exit7.us.us.us ]
  %..i.us.us.us = call i32 @llvm.smax.i32(i32 %14, i32 %11)
  %.12.i.us.us.us = call i32 @llvm.smin.i32(i32 %14, i32 %11)
  br label %16

16:                                               ; preds = %16, %12
  %.07.i.us.us.us = phi i32 [ %..i.us.us.us, %12 ], [ %.0.i.us.us.us, %16 ]
  %.0.i.us.us.us = phi i32 [ %.12.i.us.us.us, %12 ], [ %17, %16 ]
  %17 = srem i32 %.07.i.us.us.us, %.0.i.us.us.us
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %_QFPeuclid.exit.us.us.us, label %16

_QFPeuclid.exit.us.us.us:                         ; preds = %16
  %..i3.us.us.us = call i32 @llvm.smax.i32(i32 %9, i32 %.0.i.us.us.us)
  %.12.i4.us.us.us = call i32 @llvm.smin.i32(i32 %9, i32 %.0.i.us.us.us)
  br label %19

19:                                               ; preds = %19, %_QFPeuclid.exit.us.us.us
  %.07.i5.us.us.us = phi i32 [ %..i3.us.us.us, %_QFPeuclid.exit.us.us.us ], [ %.0.i6.us.us.us, %19 ]
  %.0.i6.us.us.us = phi i32 [ %.12.i4.us.us.us, %_QFPeuclid.exit.us.us.us ], [ %20, %19 ]
  %20 = srem i32 %.07.i5.us.us.us, %.0.i6.us.us.us
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %_QFPeuclid.exit7.us.us.us, label %19

_QFPeuclid.exit7.us.us.us:                        ; preds = %19
  %22 = add i32 %.0.i6.us.us.us, %15
  %23 = add nuw i32 %14, 1
  %24 = add nsw i64 %13, -1
  %25 = icmp sgt i64 %13, 1
  br i1 %25, label %12, label %._crit_edge.us.us.us

._crit_edge.us.us.us:                             ; preds = %_QFPeuclid.exit7.us.us.us
  %26 = add nuw i32 %11, 1
  %27 = add nsw i64 %10, -1
  %28 = icmp sgt i64 %10, 1
  br i1 %28, label %.preheader.us.us.us, label %._crit_edge19.split.us.us.us

._crit_edge19.split.us.us.us:                     ; preds = %._crit_edge.us.us.us
  %29 = add nuw i32 %9, 1
  %30 = add nsw i64 %8, -1
  %31 = icmp sgt i64 %8, 1
  br i1 %31, label %.preheader14.us.us, label %._crit_edge.split.us.split.us

._crit_edge.split.us.split.us:                    ; preds = %._crit_edge19.split.us.us.us
  store i32 %22, ptr @_QFEtotal, align 4, !tbaa !3
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.split.us.split.us, %0
  %32 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.cd8fbcaed489dec1fb3cddc641de00b3, i32 14)
  %33 = load i32, ptr @_QFEtotal, align 4, !tbaa !3
  %34 = call i1 @_FortranAioOutputInteger32(ptr %32, i32 %33)
  %35 = call i32 @_FortranAioEndIoStatement(ptr %32)
  ret void
}

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: read)
define i32 @_QFPeuclid(ptr nocapture readonly %0, ptr nocapture readonly %1) local_unnamed_addr #0 {
  %3 = load i32, ptr %0, align 4, !tbaa !3
  %4 = load i32, ptr %1, align 4, !tbaa !3
  %. = tail call i32 @llvm.smax.i32(i32 %3, i32 %4)
  %.12 = tail call i32 @llvm.smin.i32(i32 %3, i32 %4)
  br label %5

5:                                                ; preds = %5, %2
  %.07 = phi i32 [ %., %2 ], [ %.0, %5 ]
  %.0 = phi i32 [ %.12, %2 ], [ %6, %5 ]
  %6 = srem i32 %.07, %.0
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %5

8:                                                ; preds = %5
  ret i32 %.0
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare ptr @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
