; 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.3 = external hidden unnamed_addr constant [30 x i8], align 1
@stderr = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [101 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @merry_syscall_open(ptr noundef) #0

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

declare i64 @merry_get_errno() #2

; Function Attrs: nounwind uwtable
declare dso_local void @merry_syscall_read(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @merry_syscall_write(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @merry_exec_syscall(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr @stdout, align 8, !tbaa !5
  %4 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %3, ptr noundef @.str.3, ptr noundef null) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.MerryCore, ptr %5, i32 0, i32 5
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = getelementptr inbounds i64, ptr %7, i64 0
  %9 = load i64, ptr %8, align 8, !tbaa !13
  switch i64 %9, label %16 [
    i64 2, label %10
    i64 0, label %12
    i64 1, label %14
  ]

10:                                               ; preds = %1
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  call void @merry_syscall_open(ptr noundef %11)
  br label %33

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  call void @merry_syscall_read(ptr noundef %13)
  br label %33

14:                                               ; preds = %1
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  call void @merry_syscall_write(ptr noundef %15)
  br label %33

16:                                               ; preds = %1
  %17 = load ptr, ptr @stderr, align 8, !tbaa !5
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.MerryCore, ptr %18, i32 0, i32 5
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = getelementptr inbounds i64, ptr %20, i64 0
  %22 = load i64, ptr %21, align 8, !tbaa !13
  %23 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef @.str.4, i64 noundef %22) #3
  %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 !9
  %27 = getelementptr inbounds i64, ptr %26, i64 0
  store i64 0, ptr %27, align 8, !tbaa !13
  %28 = call i64 @merry_get_errno()
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.MerryCore, ptr %29, i32 0, i32 5
  %31 = load ptr, ptr %30, align 8, !tbaa !9
  %32 = getelementptr inbounds i64, ptr %31, i64 1
  store i64 %28, ptr %32, align 8, !tbaa !13
  br label %33

33:                                               ; preds = %16, %14, %12, %10
  ret void
}

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


; 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.3 = external hidden unnamed_addr constant [30 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [101 x i8], align 1

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

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

declare i64 @merry_get_errno() local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
define dso_local void @merry_exec_syscall(ptr noundef %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.3, ptr noundef null) #3
  %4 = getelementptr inbounds i8, ptr %0, i64 40
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %6 = load i64, ptr %5, align 8, !tbaa !13
  switch i64 %6, label %10 [
    i64 2, label %7
    i64 0, label %8
    i64 1, label %9
  ]

7:                                                ; preds = %1
  tail call void @merry_syscall_open(ptr noundef nonnull %0)
  br label %17

8:                                                ; preds = %1
  tail call void @merry_syscall_read(ptr noundef nonnull %0)
  br label %17

9:                                                ; preds = %1
  tail call void @merry_syscall_write(ptr noundef nonnull %0)
  br label %17

10:                                               ; preds = %1
  %11 = load ptr, ptr @stderr, align 8, !tbaa !5
  %12 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef nonnull @.str.4, i64 noundef %6) #4
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  store i64 0, ptr %13, align 8, !tbaa !13
  %14 = tail call i64 @merry_get_errno() #3
  %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
  br label %17

17:                                               ; preds = %10, %9, %8, %7
  ret void
}

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 }
attributes #4 = { cold 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}


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