; LLVM IR from freakout42/macrocalc :: mc/mex (files)

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

%struct.BUFFER = type { ptr, ptr, i32, ptr, i32, ptr, i32, i32, [128 x i8], [128 x i8] }
%struct.LINE = type { ptr, ptr, i16, i16, [32768 x i8] }

@curbp = external global ptr, align 8
@playback = external global i32, align 4
@.str.14 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [19 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @ffwopen(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @fwclose() #1

declare void @mlwrite(ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @ffputline(ptr noundef, i32 noundef, i32 noundef) #1

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

11:                                               ; preds = %1
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %83

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call i32 @ffwopen(ptr noundef %13)
  store i32 %14, ptr %4, align 4, !tbaa !9
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %12
  call void (ptr, ...) @mlwrite(ptr noundef @.str.14)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %83

18:                                               ; preds = %12
  call void (ptr, ...) @mlwrite(ptr noundef @.str.15)
  %19 = load ptr, ptr @curbp, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.BUFFER, ptr %19, i32 0, i32 5
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = getelementptr inbounds %struct.LINE, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  store ptr %23, ptr %5, align 8, !tbaa !5
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %60, %18
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load ptr, ptr @curbp, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.BUFFER, ptr %26, i32 0, i32 5
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = icmp ne ptr %25, %28
  br i1 %29, label %30, label %66

30:                                               ; preds = %24
  %31 = load ptr, ptr @curbp, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.BUFFER, ptr %31, i32 0, i32 7
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = and i32 %33, 16
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %44

36:                                               ; preds = %30
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.LINE, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = load ptr, ptr @curbp, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.BUFFER, ptr %40, i32 0, i32 5
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = icmp eq ptr %39, %42
  br label %44

44:                                               ; preds = %36, %30
  %45 = phi i1 [ false, %30 ], [ %43, %36 ]
  %46 = zext i1 %45 to i64
  %47 = select i1 %45, i32 0, i32 1
  store i32 %47, ptr %7, align 4, !tbaa !9
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.LINE, ptr %48, i32 0, i32 4
  %50 = getelementptr inbounds [32768 x i8], ptr %49, i64 0, i64 0
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.LINE, ptr %51, i32 0, i32 3
  %53 = load i16, ptr %52, align 2, !tbaa !17
  %54 = sext i16 %53 to i32
  %55 = and i32 65535, %54
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = call i32 @ffputline(ptr noundef %50, i32 noundef %55, i32 noundef %56)
  store i32 %57, ptr %4, align 4, !tbaa !9
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %60

59:                                               ; preds = %44
  br label %66

60:                                               ; preds = %44
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %6, align 4, !tbaa !9
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.LINE, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !13
  store ptr %65, ptr %5, align 8, !tbaa !5
  br label %24, !llvm.loop !18

66:                                               ; preds = %59, %24
  %67 = load i32, ptr %4, align 4, !tbaa !9
  %68 = icmp eq i32 %67, 0
  br i1 %68, label %69, label %76

69:                                               ; preds = %66
  %70 = call i32 @fwclose()
  store i32 %70, ptr %4, align 4, !tbaa !9
  %71 = load i32, ptr %4, align 4, !tbaa !9
  %72 = icmp eq i32 %71, 0
  br i1 %72, label %73, label %75

73:                                               ; preds = %69
  %74 = load i32, ptr %6, align 4, !tbaa !9
  call void (ptr, ...) @mlwrite(ptr noundef @.str.16, i32 noundef %74)
  br label %75

75:                                               ; preds = %73, %69
  br label %78

76:                                               ; preds = %66
  %77 = call i32 @fwclose()
  br label %78

78:                                               ; preds = %76, %75
  %79 = load i32, ptr %4, align 4, !tbaa !9
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %81, label %82

81:                                               ; preds = %78
  store i32 0, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %83

82:                                               ; preds = %78
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %83

83:                                               ; preds = %82, %81, %17, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %84 = load i32, ptr %2, align 4
  ret i32 %84
}

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

!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, !6, i64 40}
!12 = !{!"BUFFER", !6, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !10, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !7, i64 56, !7, i64 184}
!13 = !{!14, !6, i64 0}
!14 = !{!"LINE", !6, i64 0, !6, i64 8, !15, i64 16, !15, i64 18, !7, i64 20}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !10, i64 52}
!17 = !{!14, !15, i64 18}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@curbp = external local_unnamed_addr global ptr, align 8
@playback = external local_unnamed_addr global i32, align 4
@.str.14 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @ffwopen(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @fwclose() local_unnamed_addr #0

declare void @mlwrite(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @ffputline(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @writeout(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @playback, align 4, !tbaa !5
  %3 = icmp eq i32 %2, 1
  br i1 %3, label %.thread, label %4

4:                                                ; preds = %1
  %5 = tail call i32 @ffwopen(ptr noundef %0)
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %7, label %6

6:                                                ; preds = %4
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.14) #2
  br label %.thread

7:                                                ; preds = %4
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.15) #2
  %8 = load ptr, ptr @curbp, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %8, i64 40
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %.01428 = load ptr, ptr %10, align 8, !tbaa !13
  %.not2229 = icmp eq ptr %.01428, %10
  br i1 %.not2229, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %27
  %11 = phi ptr [ %31, %27 ], [ %10, %7 ]
  %12 = phi ptr [ %29, %27 ], [ %8, %7 ]
  %.01431 = phi ptr [ %.014, %27 ], [ %.01428, %7 ]
  %.01330 = phi i32 [ %28, %27 ], [ 0, %7 ]
  %13 = getelementptr inbounds i8, ptr %12, i64 52
  %14 = load i32, ptr %13, align 4, !tbaa !16
  %15 = and i32 %14, 16
  %.not23 = icmp eq i32 %15, 0
  br i1 %.not23, label %19, label %16

16:                                               ; preds = %.lr.ph
  %17 = load ptr, ptr %.01431, align 8, !tbaa !13
  %18 = icmp eq ptr %17, %11
  br label %19

19:                                               ; preds = %16, %.lr.ph
  %20 = phi i1 [ false, %.lr.ph ], [ %18, %16 ]
  %not. = xor i1 %20, true
  %21 = zext i1 %not. to i32
  %22 = getelementptr inbounds i8, ptr %.01431, i64 20
  %23 = getelementptr inbounds i8, ptr %.01431, i64 18
  %24 = load i16, ptr %23, align 2, !tbaa !17
  %25 = zext i16 %24 to i32
  %26 = tail call i32 @ffputline(ptr noundef nonnull %22, i32 noundef %25, i32 noundef %21)
  %.not24 = icmp eq i32 %26, 0
  br i1 %.not24, label %27, label %34

27:                                               ; preds = %19
  %28 = add nuw nsw i32 %.01330, 1
  %.014 = load ptr, ptr %.01431, align 8, !tbaa !13
  %29 = load ptr, ptr @curbp, align 8, !tbaa !9
  %30 = getelementptr inbounds i8, ptr %29, i64 40
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  %.not22 = icmp eq ptr %.014, %31
  br i1 %.not22, label %.critedge, label %.lr.ph, !llvm.loop !18

.critedge:                                        ; preds = %27, %7
  %.013.lcssa = phi i32 [ 0, %7 ], [ %28, %27 ]
  %32 = tail call i32 @fwclose()
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %36, label %.thread

34:                                               ; preds = %19
  %35 = tail call i32 @fwclose()
  br label %.thread

36:                                               ; preds = %.critedge
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.16, i32 noundef %.013.lcssa) #2
  br label %.thread

.thread:                                          ; preds = %34, %.critedge, %36, %1, %6
  %.0 = phi i32 [ 0, %6 ], [ 1, %1 ], [ 1, %36 ], [ 0, %.critedge ], [ 0, %34 ]
  ret i32 %.0
}

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 = { "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 = { nounwind }

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 40}
!12 = !{!"BUFFER", !10, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !7, i64 56, !7, i64 184}
!13 = !{!14, !10, i64 0}
!14 = !{!"LINE", !10, i64 0, !10, i64 8, !15, i64 16, !15, i64 18, !7, i64 20}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !6, i64 52}
!17 = !{!14, !15, i64 18}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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