; 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 = external hidden unnamed_addr constant [46 x i8], align 1

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

18:                                               ; preds = %1
  call void @merry_set_errno(i64 noundef 1)
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.MerryCore, ptr %19, i32 0, i32 5
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = getelementptr inbounds i64, ptr %21, i64 1
  store i64 1, ptr %22, align 8, !tbaa !14
  store i32 1, ptr %4, align 4
  br label %61

23:                                               ; preds = %1
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.MerryCore, ptr %24, i32 0, i32 5
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = getelementptr inbounds i64, ptr %26, i64 0
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MerryCore, ptr %30, i32 0, i32 5
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = getelementptr inbounds i64, ptr %32, i64 7
  %34 = load i64, ptr %33, align 8, !tbaa !14
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.MerryCore, ptr %35, i32 0, i32 5
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %38 = getelementptr inbounds i64, ptr %37, i64 8
  %39 = load i64, ptr %38, align 8, !tbaa !14
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MerryCore, ptr %40, i32 0, i32 5
  %42 = load ptr, ptr %41, align 8, !tbaa !13
  %43 = getelementptr inbounds i64, ptr %42, i64 9
  %44 = load i64, ptr %43, align 8, !tbaa !14
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.MerryCore, ptr %45, i32 0, i32 5
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = getelementptr inbounds i64, ptr %47, i64 10
  %49 = load i64, ptr %48, align 8, !tbaa !14
  %50 = call i64 (i64, ...) @syscall(i64 noundef %28, ptr noundef %29, i64 noundef %34, i64 noundef %39, i64 noundef %44, i64 noundef %49) #4
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.MerryCore, ptr %51, i32 0, i32 5
  %53 = load ptr, ptr %52, align 8, !tbaa !13
  %54 = getelementptr inbounds i64, ptr %53, i64 0
  store i64 %50, ptr %54, align 8, !tbaa !14
  %55 = call i64 @merry_get_errno()
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.MerryCore, ptr %56, i32 0, i32 5
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  %59 = getelementptr inbounds i64, ptr %58, i64 1
  store i64 %55, ptr %59, align 8, !tbaa !14
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %60) #4
  store i32 0, ptr %4, align 4
  br label %61

61:                                               ; preds = %23, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  %62 = load i32, ptr %4, align 4
  switch i32 %62, label %64 [
    i32 0, label %63
    i32 1, label %63
  ]

63:                                               ; preds = %61, %61
  ret void

64:                                               ; preds = %61
  unreachable
}

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

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

declare ptr @merry_dmemory_get_bytes_maybe_over_multiple_pages_upto(ptr noundef, i64 noundef, i8 noundef signext) #3

declare void @merry_set_errno(i64 noundef) #3

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

declare i64 @merry_get_errno() #3

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

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

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 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

!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 16}
!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 = !{!10, !6, i64 40}
!14 = !{!11, !11, i64 0}


; 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 = external hidden unnamed_addr constant [46 x i8], align 1

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

12:                                               ; preds = %1
  tail call void @merry_set_errno(i64 noundef 1) #5
  %13 = load ptr, ptr %6, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  store i64 1, ptr %14, align 8, !tbaa !14
  br label %31

15:                                               ; preds = %1
  %16 = load ptr, ptr %6, align 8, !tbaa !13
  %17 = load i64, ptr %16, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %16, i64 56
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %16, i64 64
  %21 = load i64, ptr %20, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %16, i64 72
  %23 = load i64, ptr %22, align 8, !tbaa !14
  %24 = getelementptr inbounds i8, ptr %16, i64 80
  %25 = load i64, ptr %24, align 8, !tbaa !14
  %26 = tail call i64 (i64, ...) @syscall(i64 noundef %17, ptr noundef nonnull %10, i64 noundef %19, i64 noundef %21, i64 noundef %23, i64 noundef %25) #5
  %27 = load ptr, ptr %6, align 8, !tbaa !13
  store i64 %26, ptr %27, align 8, !tbaa !14
  %28 = tail call i64 @merry_get_errno() #5
  %29 = load ptr, ptr %6, align 8, !tbaa !13
  %30 = getelementptr inbounds i8, ptr %29, i64 8
  store i64 %28, ptr %30, align 8, !tbaa !14
  tail call void @free(ptr noundef nonnull %10) #5
  br label %31

31:                                               ; preds = %15, %12
  ret void
}

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

declare ptr @merry_dmemory_get_bytes_maybe_over_multiple_pages_upto(ptr noundef, i64 noundef, i8 noundef signext) local_unnamed_addr #2

declare void @merry_set_errno(i64 noundef) local_unnamed_addr #2

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

declare i64 @merry_get_errno() local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #4

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 = { 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 #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 "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #5 = { 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 16}
!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 = !{!10, !6, i64 40}
!14 = !{!11, !11, i64 0}


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