; 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.WINDOW = type { ptr, ptr, ptr, ptr, i32, ptr, i32, i32, i32, i32, i32, i32 }

@curbp = external global ptr, align 8
@wheadp = external global ptr, align 8
@.str.23 = external hidden unnamed_addr constant [17 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @writeout(ptr noundef) #2

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

14:                                               ; preds = %2
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %56

15:                                               ; preds = %2
  %16 = load ptr, ptr @curbp, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.BUFFER, ptr %16, i32 0, i32 8
  %18 = getelementptr inbounds [128 x i8], ptr %17, i64 0, i64 0
  %19 = load i8, ptr %18, align 8, !tbaa !13
  %20 = sext i8 %19 to i32
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %15
  call void (ptr, ...) @mlwrite(ptr noundef @.str.23)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %56

23:                                               ; preds = %15
  %24 = load ptr, ptr @curbp, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.BUFFER, ptr %24, i32 0, i32 8
  %26 = getelementptr inbounds [128 x i8], ptr %25, i64 0, i64 0
  %27 = call i32 @writeout(ptr noundef %26)
  store i32 %27, ptr %7, align 4, !tbaa !5
  %28 = icmp eq i32 %27, 1
  br i1 %28, label %29, label %54

29:                                               ; preds = %23
  %30 = load ptr, ptr @curbp, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.BUFFER, ptr %30, i32 0, i32 7
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = and i32 %32, -3
  store i32 %33, ptr %31, align 4, !tbaa !11
  %34 = load ptr, ptr @wheadp, align 8, !tbaa !9
  store ptr %34, ptr %6, align 8, !tbaa !9
  br label %35

35:                                               ; preds = %49, %29
  %36 = load ptr, ptr %6, align 8, !tbaa !9
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %53

38:                                               ; preds = %35
  %39 = load ptr, ptr %6, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.WINDOW, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = load ptr, ptr @curbp, align 8, !tbaa !9
  %43 = icmp eq ptr %41, %42
  br i1 %43, label %44, label %49

44:                                               ; preds = %38
  %45 = load ptr, ptr %6, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.WINDOW, ptr %45, i32 0, i32 11
  %47 = load i32, ptr %46, align 4, !tbaa !16
  %48 = or i32 %47, 16
  store i32 %48, ptr %46, align 4, !tbaa !16
  br label %49

49:                                               ; preds = %44, %38
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.WINDOW, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !17
  store ptr %52, ptr %6, align 8, !tbaa !9
  br label %35, !llvm.loop !18

53:                                               ; preds = %35
  br label %54

54:                                               ; preds = %53, %23
  %55 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %55, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %56

56:                                               ; preds = %54, %22, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %57 = load i32, ptr %3, align 4
  ret i32 %57
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!"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, !6, i64 52}
!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 = !{!7, !7, i64 0}
!14 = !{!15, !10, i64 8}
!15 = !{!"WINDOW", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68}
!16 = !{!15, !6, i64 68}
!17 = !{!15, !10, i64 0}
!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
@wheadp = external local_unnamed_addr global ptr, align 8
@.str.23 = external hidden unnamed_addr constant [17 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @filesave(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = load ptr, ptr @curbp, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 52
  %5 = load i32, ptr %4, align 4, !tbaa !9
  %6 = and i32 %5, 2
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %.loopexit, label %8

8:                                                ; preds = %2
  %9 = getelementptr inbounds i8, ptr %3, i64 56
  %10 = load i8, ptr %9, align 8, !tbaa !12
  %11 = icmp eq i8 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %8
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.23) #2
  br label %.loopexit

13:                                               ; preds = %8
  %14 = tail call i32 @writeout(ptr noundef nonnull %9)
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %16, label %.loopexit

16:                                               ; preds = %13
  %17 = load ptr, ptr @curbp, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 52
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = and i32 %19, -3
  store i32 %20, ptr %18, align 4, !tbaa !9
  %.056 = load ptr, ptr @wheadp, align 8, !tbaa !5
  %.not7 = icmp eq ptr %.056, null
  br i1 %.not7, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %16, %28
  %.058 = phi ptr [ %.05, %28 ], [ %.056, %16 ]
  %21 = getelementptr inbounds i8, ptr %.058, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  %23 = icmp eq ptr %22, %17
  br i1 %23, label %24, label %28

24:                                               ; preds = %.lr.ph
  %25 = getelementptr inbounds i8, ptr %.058, i64 68
  %26 = load i32, ptr %25, align 4, !tbaa !15
  %27 = or i32 %26, 16
  store i32 %27, ptr %25, align 4, !tbaa !15
  br label %28

28:                                               ; preds = %24, %.lr.ph
  %.05 = load ptr, ptr %.058, align 8, !tbaa !5
  %.not = icmp eq ptr %.05, null
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !16

.loopexit:                                        ; preds = %28, %16, %13, %2, %12
  %.0 = phi i32 [ 0, %12 ], [ 1, %2 ], [ %14, %13 ], [ 1, %16 ], [ 1, %28 ]
  ret i32 %.0
}

attributes #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 = { 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 = { 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, !11, i64 52}
!10 = !{!"BUFFER", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !7, i64 56, !7, i64 184}
!11 = !{!"int", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"WINDOW", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68}
!15 = !{!14, !11, i64 68}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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