; 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.1 = external hidden unnamed_addr constant [51 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_read(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  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.1, ptr noundef null) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %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
  %13 = load i64, ptr %3, align 8, !tbaa !13
  %14 = call ptr @llvm.stacksave.p0()
  store ptr %14, ptr %4, align 8
  %15 = alloca i8, i64 %13, align 16
  store i64 %13, ptr %5, align 8
  %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 0
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MerryCore, ptr %21, i32 0, i32 5
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = getelementptr inbounds i64, ptr %23, i64 6
  %25 = load i64, ptr %24, align 8, !tbaa !13
  %26 = load i64, ptr %3, align 8, !tbaa !13
  %27 = call i64 (i64, ...) @syscall(i64 noundef %20, i64 noundef %25, ptr noundef %15, i64 noundef %26) #5
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.MerryCore, ptr %28, i32 0, i32 5
  %30 = load ptr, ptr %29, align 8, !tbaa !9
  %31 = getelementptr inbounds i64, ptr %30, i64 0
  store i64 %27, ptr %31, align 8, !tbaa !13
  %32 = call i64 @merry_get_errno()
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.MerryCore, ptr %33, i32 0, i32 5
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = getelementptr inbounds i64, ptr %35, i64 1
  store i64 %32, ptr %36, align 8, !tbaa !13
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.MerryCore, ptr %37, i32 0, i32 2
  %39 = load ptr, 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 !9
  %43 = getelementptr inbounds i64, ptr %42, i64 7
  %44 = load i64, ptr %43, align 8, !tbaa !13
  %45 = load i64, ptr %3, align 8, !tbaa !13
  %46 = call zeroext i8 @merry_dmemory_write_bytes_maybe_over_multiple_pages(ptr noundef %39, i64 noundef %44, i64 noundef %45, ptr noundef %15)
  %47 = zext i8 %46 to i32
  %48 = icmp eq i32 %47, 1
  br i1 %48, label %49, label %58

49:                                               ; preds = %1
  call void @merry_set_errno(i64 noundef 1)
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.MerryCore, ptr %50, i32 0, i32 5
  %52 = load ptr, ptr %51, align 8, !tbaa !9
  %53 = getelementptr inbounds i64, ptr %52, i64 1
  store i64 1, ptr %53, align 8, !tbaa !13
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.MerryCore, ptr %54, i32 0, i32 5
  %56 = load ptr, ptr %55, align 8, !tbaa !9
  %57 = getelementptr inbounds i64, ptr %56, i64 0
  store i64 -1, ptr %57, align 8, !tbaa !13
  br label %58

58:                                               ; preds = %49, %1
  %59 = load ptr, ptr %4, align 8
  call void @llvm.stackrestore.p0(ptr %59)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #4

declare zeroext i8 @merry_dmemory_write_bytes_maybe_over_multiple_pages(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #4

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 = { nocallback nofree nosync nounwind willreturn }
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 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.1 = external hidden unnamed_addr constant [51 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_read(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.1, 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 = alloca i8, i64 %7, align 16
  %9 = load i64, ptr %5, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %5, i64 48
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = call i64 (i64, ...) @syscall(i64 noundef %9, i64 noundef %11, ptr noundef nonnull %8, i64 noundef %7) #4
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  store i64 %12, ptr %13, align 8, !tbaa !13
  %14 = call i64 @merry_get_errno() #4
  %15 = load ptr, ptr %4, align 8, !tbaa !9
  %16 = getelementptr inbounds i8, ptr %15, i64 8
  store i64 %14, ptr %16, align 8, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %15, i64 56
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = call zeroext i8 @merry_dmemory_write_bytes_maybe_over_multiple_pages(ptr noundef %18, i64 noundef %20, i64 noundef %7, ptr noundef nonnull %8) #4
  %22 = icmp eq i8 %21, 1
  br i1 %22, label %23, label %26

23:                                               ; preds = %1
  call void @merry_set_errno(i64 noundef 1) #4
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  store i64 1, ptr %25, align 8, !tbaa !13
  store i64 -1, ptr %24, align 8, !tbaa !13
  br label %26

26:                                               ; preds = %23, %1
  ret void
}

declare zeroext i8 @merry_dmemory_write_bytes_maybe_over_multiple_pages(ptr noundef, i64 noundef, i64 noundef, ptr 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,TailCallElimPass