; LLVM IR from aap/pdp6 :: emu (mem)

; 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.Membus = type { i64, i64, i64, i64, i64, i64, ptr, [16 x ptr] }
%struct.Mem = type { %struct.Device, ptr, [4 x ptr], ptr, ptr, ptr }
%struct.Device = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.FMem = type { [16 x i64], i8, i8, i8 }
%struct.CMem = type { ptr, i32, i32, ptr, i64, i32, i8, i8, i8, i8, i8, i8 }

; 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

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

15:                                               ; preds = %4
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp ult i32 %16, 16
  br i1 %17, label %18, label %43

18:                                               ; preds = %15
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Membus, ptr %19, i32 0, i32 6
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %43

23:                                               ; preds = %18
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Membus, ptr %24, i32 0, i32 6
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %27 = getelementptr inbounds %struct.Mem, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  store ptr %28, ptr %11, align 8, !tbaa !5
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %37

31:                                               ; preds = %23
  %32 = load ptr, ptr %11, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.FMem, ptr %32, i32 0, i32 2
  %34 = load i8, ptr %33, align 1, !tbaa !17
  %35 = zext i8 %34 to i32
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %35, ptr %36, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %31, %23
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.FMem, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %7, align 4, !tbaa !9
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds [16 x i64], ptr %39, i64 0, i64 %41
  store ptr %42, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %84

43:                                               ; preds = %18, %15, %4
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Membus, ptr %44, i32 0, i32 7
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = lshr i32 %46, 14
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds [16 x ptr], ptr %45, i64 0, i64 %48
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  %51 = icmp ne ptr %50, null
  br i1 %51, label %52, label %83

52:                                               ; preds = %43
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.Membus, ptr %53, i32 0, i32 7
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = lshr i32 %55, 14
  %57 = zext i32 %56 to i64
  %58 = getelementptr inbounds [16 x ptr], ptr %54, i64 0, i64 %57
  %59 = load ptr, ptr %58, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.Mem, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !14
  store ptr %61, ptr %10, align 8, !tbaa !5
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = icmp ne ptr %62, null
  br i1 %63, label %64, label %72

64:                                               ; preds = %52
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.CMem, ptr %65, i32 0, i32 9
  %67 = load i8, ptr %66, align 1, !tbaa !19
  %68 = sext i8 %67 to i32
  %69 = icmp sge i32 %68, 0
  %70 = zext i1 %69 to i32
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %70, ptr %71, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %64, %52
  %73 = load ptr, ptr %10, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.CMem, ptr %73, i32 0, i32 3
  %75 = load ptr, ptr %74, align 8, !tbaa !21
  %76 = load i32, ptr %7, align 4, !tbaa !9
  %77 = load ptr, ptr %10, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.CMem, ptr %77, i32 0, i32 2
  %79 = load i32, ptr %78, align 4, !tbaa !22
  %80 = and i32 %76, %79
  %81 = zext i32 %80 to i64
  %82 = getelementptr inbounds i64, ptr %75, i64 %81
  store ptr %82, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %84

83:                                               ; preds = %43
  store ptr null, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %84

84:                                               ; preds = %83, %72, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %85 = load ptr, ptr %5, align 8
  ret ptr %85
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 48}
!12 = !{!"Membus", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !6, i64 48, !7, i64 56}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !6, i64 64}
!15 = !{!"Mem", !16, i64 0, !6, i64 64, !7, i64 72, !6, i64 104, !6, i64 112, !6, i64 120}
!16 = !{!"Device", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!17 = !{!18, !7, i64 129}
!18 = !{!"FMem", !7, i64 0, !7, i64 128, !7, i64 129, !7, i64 130}
!19 = !{!20, !7, i64 39}
!20 = !{!"CMem", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !13, i64 24, !10, i64 32, !7, i64 36, !7, i64 37, !7, i64 38, !7, i64 39, !7, i64 40, !7, i64 41}
!21 = !{!20, !6, i64 16}
!22 = !{!20, !10, i64 12}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @getmemref(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, ptr noundef writeonly %3) local_unnamed_addr #0 {
  %5 = icmp ne i32 %2, 0
  %6 = icmp ult i32 %1, 16
  %or.cond = and i1 %6, %5
  br i1 %or.cond, label %7, label %20

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %.not = icmp eq ptr %9, null
  br i1 %.not, label %20, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %9, i64 64
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %.not26 = icmp eq ptr %3, null
  br i1 %.not26, label %17, label %13

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %12, i64 129
  %15 = load i8, ptr %14, align 1, !tbaa !14
  %16 = zext i8 %15 to i32
  store i32 %16, ptr %3, align 4, !tbaa !16
  br label %17

17:                                               ; preds = %13, %10
  %18 = zext nneg i32 %1 to i64
  %19 = getelementptr inbounds [16 x i64], ptr %12, i64 0, i64 %18
  br label %42

20:                                               ; preds = %7, %4
  %21 = getelementptr inbounds i8, ptr %0, i64 56
  %22 = lshr i32 %1, 14
  %23 = zext nneg i32 %22 to i64
  %24 = getelementptr inbounds [16 x ptr], ptr %21, i64 0, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !18
  %.not24 = icmp eq ptr %25, null
  br i1 %.not24, label %42, label %26

26:                                               ; preds = %20
  %27 = getelementptr inbounds i8, ptr %25, i64 64
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %.not25 = icmp eq ptr %3, null
  br i1 %.not25, label %34, label %29

29:                                               ; preds = %26
  %30 = getelementptr inbounds i8, ptr %28, i64 39
  %31 = load i8, ptr %30, align 1, !tbaa !19
  %32 = icmp sgt i8 %31, -1
  %33 = zext i1 %32 to i32
  store i32 %33, ptr %3, align 4, !tbaa !16
  br label %34

34:                                               ; preds = %29, %26
  %35 = getelementptr inbounds i8, ptr %28, i64 16
  %36 = load ptr, ptr %35, align 8, !tbaa !21
  %37 = getelementptr inbounds i8, ptr %28, i64 12
  %38 = load i32, ptr %37, align 4, !tbaa !22
  %39 = and i32 %38, %1
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds i64, ptr %36, i64 %40
  br label %42

42:                                               ; preds = %20, %34, %17
  %.0 = phi ptr [ %19, %17 ], [ %41, %34 ], [ null, %20 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) 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" }

!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, !10, i64 48}
!6 = !{!"Membus", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !10, i64 48, !8, i64 56}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !10, i64 64}
!12 = !{!"Mem", !13, i64 0, !10, i64 64, !8, i64 72, !10, i64 104, !10, i64 112, !10, i64 120}
!13 = !{!"Device", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56}
!14 = !{!15, !8, i64 129}
!15 = !{!"FMem", !8, i64 0, !8, i64 128, !8, i64 129, !8, i64 130}
!16 = !{!17, !17, i64 0}
!17 = !{!"int", !8, i64 0}
!18 = !{!10, !10, i64 0}
!19 = !{!20, !8, i64 39}
!20 = !{!"CMem", !10, i64 0, !17, i64 8, !17, i64 12, !10, i64 16, !7, i64 24, !17, i64 32, !8, i64 36, !8, i64 37, !8, i64 38, !8, i64 39, !8, i64 40, !8, i64 41}
!21 = !{!20, !10, i64 16}
!22 = !{!20, !17, i64 12}


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