; LLVM IR from Aeternum-Veritas/Merry :: merry (merry_syscall_hdlr)

; 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.MerryCore = type { ptr, ptr, ptr, ptr, ptr, ptr, i64, i64, i64, i64, %struct.MerryFlagRegister, i8, i8, i64, ptr, i64, i8, i64, ptr }
%struct.MerryFlagRegister = type { i64 }

@stdout = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [49 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

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

declare void @merry_set_errno(i64 noundef) #2

; Function Attrs: nounwind
declare i64 @syscall(i64 noundef, ...) #0

declare i64 @merry_get_errno() #2

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

; Function Attrs: nounwind uwtable
define dso_local void @merry_syscall_write(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr @stdout, align 8, !tbaa !5
  %7 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %6, ptr noundef @.str.2, ptr noundef null) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.MerryCore, ptr %8, i32 0, i32 5
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = getelementptr inbounds i64, ptr %10, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !13
  store i64 %12, ptr %3, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.MerryCore, ptr %13, i32 0, i32 2
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.MerryCore, ptr %16, i32 0, i32 5
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = getelementptr inbounds i64, ptr %18, i64 7
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = load i64, ptr %3, align 8, !tbaa !13
  %22 = call ptr @merry_dmemory_get_bytes_maybe_over_multiple_pages(ptr noundef %15, i64 noundef %20, i64 noundef %21)
  store ptr %22, ptr %4, align 8, !tbaa !5
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = icmp eq ptr %23, null
  br i1 %24, label %25, label %30

25:                                               ; preds = %1
  call void @merry_set_errno(i64 noundef 1)
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.MerryCore, ptr %26, i32 0, i32 5
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %29 = getelementptr inbounds i64, ptr %28, i64 1
  store i64 1, ptr %29, align 8, !tbaa !13
  store i32 1, ptr %5, align 4
  br label %53

30:                                               ; preds = %1
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.MerryCore, ptr %31, i32 0, i32 5
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  %34 = getelementptr inbounds i64, ptr %33, i64 0
  %35 = load i64, ptr %34, align 8, !tbaa !13
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.MerryCore, ptr %36, i32 0, i32 5
  %38 = load ptr, ptr %37, align 8, !tbaa !9
  %39 = getelementptr inbounds i64, ptr %38, i64 6
  %40 = load i64, ptr %39, align 8, !tbaa !13
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = load i64, ptr %3, align 8, !tbaa !13
  %43 = call i64 (i64, ...) @syscall(i64 noundef %35, i64 noundef %40, ptr noundef %41, i64 noundef %42) #4
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.MerryCore, ptr %44, i32 0, i32 5
  %46 = load ptr, ptr %45, align 8, !tbaa !9
  %47 = getelementptr inbounds i64, ptr %46, i64 0
  store i64 %43, ptr %47, align 8, !tbaa !13
  %48 = call i64 @merry_get_errno()
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.MerryCore, ptr %49, i32 0, i32 5
  %51 = load ptr, ptr %50, align 8, !tbaa !9
  %52 = getelementptr inbounds i64, ptr %51, i64 1
  store i64 %48, ptr %52, align 8, !tbaa !13
  store i32 0, ptr %5, align 4
  br label %53

53:                                               ; preds = %30, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  %54 = load i32, ptr %5, align 4
  switch i32 %54, label %56 [
    i32 0, label %55
    i32 1, label %55
  ]

55:                                               ; preds = %53, %53
  ret void

56:                                               ; preds = %53
  unreachable
}

declare ptr @merry_dmemory_get_bytes_maybe_over_multiple_pages(ptr noundef, i64 noundef, i64 noundef) #2

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #4 = { 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, !6, i64 40}
!10 = !{!"MerryCore", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !12, i64 80, !7, i64 88, !7, i64 89, !11, i64 96, !6, i64 104, !11, i64 112, !7, i64 120, !11, i64 128, !6, i64 136}
!11 = !{!"long", !7, i64 0}
!12 = !{!"MerryFlagRegister", !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 1, !11, i64 1, !11, i64 1, !11, i64 1, !11, i64 4}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 16}


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

@stdout = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [49 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

declare void @merry_set_errno(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i64 @syscall(i64 noundef, ...) local_unnamed_addr #2

declare i64 @merry_get_errno() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @merry_syscall_write(ptr nocapture noundef readonly %0) local_unnamed_addr #3 {
  %2 = load ptr, ptr @stdout, align 8, !tbaa !5
  %3 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %2, ptr noundef nonnull @.str.2, ptr noundef null) #4
  %4 = getelementptr inbounds i8, ptr %0, i64 40
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %5, i64 64
  %7 = load i64, ptr %6, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %5, i64 56
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = tail call ptr @merry_dmemory_get_bytes_maybe_over_multiple_pages(ptr noundef %9, i64 noundef %11, i64 noundef %7) #4
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %1
  tail call void @merry_set_errno(i64 noundef 1) #4
  br label %23

15:                                               ; preds = %1
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  %17 = load i64, ptr %16, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %16, i64 48
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = tail call i64 (i64, ...) @syscall(i64 noundef %17, i64 noundef %19, ptr noundef nonnull %12, i64 noundef %7) #4
  %21 = load ptr, ptr %4, align 8, !tbaa !9
  store i64 %20, ptr %21, align 8, !tbaa !13
  %22 = tail call i64 @merry_get_errno() #4
  br label %23

23:                                               ; preds = %15, %14
  %.sink = phi i64 [ %22, %15 ], [ 1, %14 ]
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  store i64 %.sink, ptr %25, align 8, !tbaa !13
  ret void
}

declare ptr @merry_dmemory_get_bytes_maybe_over_multiple_pages(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 "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 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 #4 = { 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, !6, i64 40}
!10 = !{!"MerryCore", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !12, i64 80, !7, i64 88, !7, i64 89, !11, i64 96, !6, i64 104, !11, i64 112, !7, i64 120, !11, i64 128, !6, i64 136}
!11 = !{!"long", !7, i64 0}
!12 = !{!"MerryFlagRegister", !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 0, !11, i64 1, !11, i64 1, !11, i64 1, !11, i64 1, !11, i64 4}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass