program main
  implicit none
  integer :: K
  integer :: out
  integer :: l,m,n
  integer t1, t2, t_rate, t_max, diff
  read(*,*) K
  !call system_clock(t1)   ! 開始時を記録  
  out = 0
  do n = 1,k
     do m = 1, K
        do l = 1,K
           out = out + gcd(gcd(l,m),n)
        end do
     end do
  end do
  write(*,'(i0)') out
!!  call system_clock(t2, t_rate, t_max)
!!  if ( t2 < t1 ) then
!!    diff = (t_max - t1) + t2 + 1
!!  else
!!    diff = t2 - t1
!!  endif
!!  print "(A, F10.3)", "time it took was:", diff/dble(t_rate)    
  stop
contains
  function gcd(a,b) result(ret)
    integer :: a,b
    integer :: m,n
    integer :: k
    integer :: ret
    m = a
    n = b
    do
       k = mod(m, n)
       if (k == 0) then
          ret = n
          return 
       end if
       ! step 4
       m = n
       n = k
    end do
  end function gcd
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"

@_QQcl.e0f3b644e91005a340a45b3536a0227f = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_1809/source.f\00"
@_QQcl.28693029 = linkonce constant [4 x i8] c"(i0)"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

; Function Attrs: noreturn
define void @_QQmain() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e0f3b644e91005a340a45b3536a0227f, i32 7)
  %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 %.preheader15.us.us, label %._crit_edge

.preheader15.us.us:                               ; preds = %0, %._crit_edge18.split.us.us.us
  %8 = phi i64 [ %27, %._crit_edge18.split.us.us.us ], [ %6, %0 ]
  %9 = phi i32 [ %26, %._crit_edge18.split.us.us.us ], [ 1, %0 ]
  %.020.us.us = phi i32 [ %17, %._crit_edge18.split.us.us.us ], [ 0, %0 ]
  br label %.preheader14.us.us.us

.preheader14.us.us.us:                            ; preds = %._crit_edge.us.us.us, %.preheader15.us.us
  %10 = phi i64 [ %6, %.preheader15.us.us ], [ %24, %._crit_edge.us.us.us ]
  %11 = phi i32 [ 1, %.preheader15.us.us ], [ %23, %._crit_edge.us.us.us ]
  %.117.us.us.us = phi i32 [ %.020.us.us, %.preheader15.us.us ], [ %17, %._crit_edge.us.us.us ]
  br label %.preheader.us.us.us

12:                                               ; preds = %.preheader.us.us.us, %12
  %.07.i.us.us.us = phi i32 [ %13, %12 ], [ %11, %.preheader.us.us.us ]
  %.0.i.us.us.us = phi i32 [ %.07.i.us.us.us, %12 ], [ %22, %.preheader.us.us.us ]
  %13 = srem i32 %.0.i.us.us.us, %.07.i.us.us.us
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %_QFPgcd.exit.us.us.us, label %12

_QFPgcd.exit.us.us.us:                            ; preds = %12, %_QFPgcd.exit.us.us.us
  %.07.i5.us.us.us = phi i32 [ %15, %_QFPgcd.exit.us.us.us ], [ %9, %12 ]
  %.0.i6.us.us.us = phi i32 [ %.07.i5.us.us.us, %_QFPgcd.exit.us.us.us ], [ %.07.i.us.us.us, %12 ]
  %15 = srem i32 %.0.i6.us.us.us, %.07.i5.us.us.us
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %_QFPgcd.exit7.us.us.us, label %_QFPgcd.exit.us.us.us

_QFPgcd.exit7.us.us.us:                           ; preds = %_QFPgcd.exit.us.us.us
  %17 = add i32 %.07.i5.us.us.us, %.216.us.us.us
  %18 = add nuw i32 %22, 1
  %19 = add nsw i64 %21, -1
  %20 = icmp sgt i64 %21, 1
  br i1 %20, label %.preheader.us.us.us, label %._crit_edge.us.us.us

.preheader.us.us.us:                              ; preds = %_QFPgcd.exit7.us.us.us, %.preheader14.us.us.us
  %21 = phi i64 [ %6, %.preheader14.us.us.us ], [ %19, %_QFPgcd.exit7.us.us.us ]
  %22 = phi i32 [ 1, %.preheader14.us.us.us ], [ %18, %_QFPgcd.exit7.us.us.us ]
  %.216.us.us.us = phi i32 [ %.117.us.us.us, %.preheader14.us.us.us ], [ %17, %_QFPgcd.exit7.us.us.us ]
  br label %12

._crit_edge.us.us.us:                             ; preds = %_QFPgcd.exit7.us.us.us
  %23 = add nuw i32 %11, 1
  %24 = add nsw i64 %10, -1
  %25 = icmp sgt i64 %10, 1
  br i1 %25, label %.preheader14.us.us.us, label %._crit_edge18.split.us.us.us

._crit_edge18.split.us.us.us:                     ; preds = %._crit_edge.us.us.us
  %26 = add nuw i32 %9, 1
  %27 = add nsw i64 %8, -1
  %28 = icmp sgt i64 %8, 1
  br i1 %28, label %.preheader15.us.us, label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge18.split.us.us.us, %0
  %.0.lcssa = phi i32 [ 0, %0 ], [ %17, %._crit_edge18.split.us.us.us ]
  %29 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e0f3b644e91005a340a45b3536a0227f, i32 17)
  %30 = call i1 @_FortranAioOutputInteger32(ptr %29, i32 %.0.lcssa)
  %31 = call i32 @_FortranAioEndIoStatement(ptr %29)
  %32 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable
}

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

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

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

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 @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

declare {} @_FortranAStopStatement(i32, i1, i1) local_unnamed_addr

attributes #0 = { noreturn }
attributes #1 = { nofree norecurse nosync nounwind memory(argmem: read) }

!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"}
