; LLVM IR from fcimeson/cbTSP :: cbLKH/SRC (fscanint)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @fscanint(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 1, ptr %8, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %21, %2
  %11 = call ptr @__ctype_b_loc() #5
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = call i32 @getc(ptr noundef %13)
  store i32 %14, ptr %7, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i16, ptr %12, i64 %15
  %17 = load i16, ptr %16, align 2, !tbaa !11
  %18 = zext i16 %17 to i32
  %19 = and i32 %18, 8192
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %10
  br label %10, !llvm.loop !13

22:                                               ; preds = %10
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = icmp eq i32 %23, 45
  br i1 %24, label %28, label %25

25:                                               ; preds = %22
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 43
  br i1 %27, label %28, label %41

28:                                               ; preds = %25, %22
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = icmp eq i32 %29, 45
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  store i32 -1, ptr %8, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %31, %28
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = call i32 @getc(ptr noundef %33)
  store i32 %34, ptr %7, align 4, !tbaa !9
  %35 = icmp eq i32 %34, -1
  br i1 %35, label %36, label %40

36:                                               ; preds = %32
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = call i32 @ungetc(i32 noundef %37, ptr noundef %38)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %80

40:                                               ; preds = %32
  br label %41

41:                                               ; preds = %40, %25
  %42 = call ptr @__ctype_b_loc() #5
  %43 = load ptr, ptr %42, align 8, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i16, ptr %43, i64 %45
  %47 = load i16, ptr %46, align 2, !tbaa !11
  %48 = zext i16 %47 to i32
  %49 = and i32 %48, 2048
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %55, label %51

51:                                               ; preds = %41
  %52 = load i32, ptr %7, align 4, !tbaa !9
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = call i32 @ungetc(i32 noundef %52, ptr noundef %53)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %80

55:                                               ; preds = %41
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = sub nsw i32 %56, 48
  store i32 %57, ptr %6, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %69, %55
  %59 = call ptr @__ctype_b_loc() #5
  %60 = load ptr, ptr %59, align 8, !tbaa !5
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = call i32 @getc(ptr noundef %61)
  store i32 %62, ptr %7, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i16, ptr %60, i64 %63
  %65 = load i16, ptr %64, align 2, !tbaa !11
  %66 = zext i16 %65 to i32
  %67 = and i32 %66, 2048
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %69, label %75

69:                                               ; preds = %58
  %70 = load i32, ptr %6, align 4, !tbaa !9
  %71 = mul nsw i32 10, %70
  %72 = load i32, ptr %7, align 4, !tbaa !9
  %73 = sub nsw i32 %72, 48
  %74 = add nsw i32 %71, %73
  store i32 %74, ptr %6, align 4, !tbaa !9
  br label %58, !llvm.loop !15

75:                                               ; preds = %58
  %76 = load i32, ptr %8, align 4, !tbaa !9
  %77 = load i32, ptr %6, align 4, !tbaa !9
  %78 = mul nsw i32 %76, %77
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  store i32 %78, ptr %79, align 4, !tbaa !9
  store i32 1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %80

80:                                               ; preds = %75, %51, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %81 = load i32, ptr %3, align 4
  ret i32 %81
}

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() #2

declare i32 @getc(ptr noundef) #3

declare i32 @ungetc(i32 noundef, ptr noundef) #3

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind willreturn memory(none) "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 = { "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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(none) }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

; Function Attrs: nofree nounwind uwtable
define dso_local range(i32 0, 2) i32 @fscanint(ptr nocapture noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = tail call ptr @__ctype_b_loc() #3
  br label %4

4:                                                ; preds = %4, %2
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = tail call i32 @getc(ptr noundef %0)
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds i16, ptr %5, i64 %7
  %9 = load i16, ptr %8, align 2, !tbaa !9
  %10 = and i16 %9, 8192
  %.not = icmp eq i16 %10, 0
  br i1 %.not, label %11, label %4, !llvm.loop !11

11:                                               ; preds = %4
  switch i32 %6, label %18 [
    i32 45, label %12
    i32 43, label %13
  ]

12:                                               ; preds = %11
  br label %13

13:                                               ; preds = %11, %12
  %.0 = phi i32 [ -1, %12 ], [ 1, %11 ]
  %14 = tail call i32 @getc(ptr noundef %0)
  %15 = icmp eq i32 %14, -1
  br i1 %15, label %16, label %._crit_edge30

._crit_edge30:                                    ; preds = %13
  %.pre = sext i32 %14 to i64
  br label %18

16:                                               ; preds = %13
  %17 = tail call i32 @ungetc(i32 noundef -1, ptr noundef %0)
  br label %43

18:                                               ; preds = %._crit_edge30, %11
  %.pre-phi = phi i64 [ %.pre, %._crit_edge30 ], [ %7, %11 ]
  %.018 = phi i32 [ %14, %._crit_edge30 ], [ %6, %11 ]
  %.1 = phi i32 [ %.0, %._crit_edge30 ], [ 1, %11 ]
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds i16, ptr %19, i64 %.pre-phi
  %21 = load i16, ptr %20, align 2, !tbaa !9
  %22 = and i16 %21, 2048
  %.not24 = icmp eq i16 %22, 0
  br i1 %.not24, label %23, label %25

23:                                               ; preds = %18
  %24 = tail call i32 @ungetc(i32 noundef %.018, ptr noundef %0)
  br label %43

25:                                               ; preds = %18
  %26 = add nsw i32 %.018, -48
  %27 = tail call i32 @getc(ptr noundef %0)
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i16, ptr %19, i64 %28
  %30 = load i16, ptr %29, align 2, !tbaa !9
  %31 = and i16 %30, 2048
  %.not2527 = icmp eq i16 %31, 0
  br i1 %.not2527, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %25, %.lr.ph
  %32 = phi i32 [ %37, %.lr.ph ], [ %27, %25 ]
  %.01928 = phi i32 [ %35, %.lr.ph ], [ %26, %25 ]
  %33 = mul nsw i32 %.01928, 10
  %34 = add i32 %33, -48
  %35 = add i32 %34, %32
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = tail call i32 @getc(ptr noundef %0)
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i16, ptr %36, i64 %38
  %40 = load i16, ptr %39, align 2, !tbaa !9
  %41 = and i16 %40, 2048
  %.not25 = icmp eq i16 %41, 0
  br i1 %.not25, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %25
  %.019.lcssa = phi i32 [ %26, %25 ], [ %35, %.lr.ph ]
  %42 = mul nsw i32 %.019.lcssa, %.1
  store i32 %42, ptr %1, align 4, !tbaa !14
  br label %43

43:                                               ; preds = %._crit_edge, %23, %16
  %.020 = phi i32 [ 0, %16 ], [ 1, %._crit_edge ], [ 0, %23 ]
  ret i32 %.020
}

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() local_unnamed_addr #1

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

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

attributes #0 = { nofree 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 #1 = { mustprogress nofree nosync nounwind willreturn memory(none) "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" }
attributes #3 = { nounwind willreturn memory(none) }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}


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