; LLVM IR from parallella/pal :: src/base (p_open)

; 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.pal_global = type { [7 x ptr], %struct.team, ptr, ptr, ptr, ptr, %struct.TINYMT32_T, i32 }
%struct.team = type { ptr, ptr, i32, i32, i32, %union.p_coords, %union.p_coords, %union.p_coords }
%union.p_coords = type { %struct.anon }
%struct.anon = type { i32, i32, i32 }
%struct.TINYMT32_T = type { [4 x i32], i32, i32, i32 }
%struct.dev = type { ptr, i32, %union.p_coords, %union.p_coords }
%struct.dev_ops = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@__pal_global = external global %struct.pal_global, align 8

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

18:                                               ; preds = %4
  %19 = call ptr @p_ref_err(i32 noundef 22)
  store ptr %19, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %86

20:                                               ; preds = %4
  %21 = call noalias ptr @malloc(i64 noundef 64) #7
  store ptr %21, ptr %11, align 8, !tbaa !5
  %22 = load ptr, ptr %11, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %26, label %24

24:                                               ; preds = %20
  %25 = call ptr @p_ref_err(i32 noundef 12)
  store ptr %25, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %86

26:                                               ; preds = %20
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load ptr, ptr %11, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.team, ptr %28, i32 0, i32 1
  store ptr %27, ptr %29, align 8, !tbaa !11
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = load ptr, ptr %11, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.team, ptr %31, i32 0, i32 4
  store i32 %30, ptr %32, align 8, !tbaa !13
  %33 = load ptr, ptr %11, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.team, ptr %33, i32 0, i32 5
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %34, ptr align 4 %35, i64 12, i1 false)
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.team, ptr %36, i32 0, i32 6
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %37, ptr align 4 %38, i64 12, i1 false)
  %39 = load i32, ptr %7, align 4, !tbaa !9
  switch i32 %39, label %54 [
    i32 0, label %40
    i32 2, label %43
    i32 1, label %47
  ]

40:                                               ; preds = %26
  %41 = load ptr, ptr %11, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.team, ptr %41, i32 0, i32 7
  store i32 -1, ptr %42, align 4, !tbaa !14
  br label %55

43:                                               ; preds = %26
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.team, ptr %44, i32 0, i32 7
  %46 = getelementptr inbounds %struct.anon, ptr %45, i32 0, i32 2
  store i32 -1, ptr %46, align 4, !tbaa !14
  br label %47

47:                                               ; preds = %43, %26
  %48 = load ptr, ptr %11, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.team, ptr %48, i32 0, i32 7
  %50 = getelementptr inbounds %struct.anon, ptr %49, i32 0, i32 1
  store i32 -1, ptr %50, align 4, !tbaa !14
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.team, ptr %51, i32 0, i32 7
  %53 = getelementptr inbounds %struct.anon, ptr %52, i32 0, i32 0
  store i32 -1, ptr %53, align 4, !tbaa !14
  br label %55

54:                                               ; preds = %26
  store i32 22, ptr %13, align 4, !tbaa !9
  br label %82

55:                                               ; preds = %47, %40
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.dev, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !15
  %59 = getelementptr inbounds %struct.dev_ops, ptr %58, i32 0, i32 3
  %60 = load ptr, ptr %59, align 8, !tbaa !17
  %61 = load ptr, ptr %11, align 8, !tbaa !5
  %62 = call ptr %60(ptr noundef %61)
  store ptr %62, ptr %12, align 8, !tbaa !5
  %63 = load ptr, ptr %12, align 8, !tbaa !5
  %64 = call i32 @p_error(ptr noundef %63)
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %69

66:                                               ; preds = %55
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = call i32 @p_error(ptr noundef %67)
  store i32 %68, ptr %13, align 4, !tbaa !9
  br label %82

69:                                               ; preds = %55
  %70 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %70, ptr %11, align 8, !tbaa !5
  %71 = load ptr, ptr getelementptr inbounds (%struct.pal_global, ptr @__pal_global, i32 0, i32 2), align 8, !tbaa !19
  %72 = icmp ne ptr %71, null
  br i1 %72, label %75, label %73

73:                                               ; preds = %69
  %74 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %74, ptr getelementptr inbounds (%struct.pal_global, ptr @__pal_global, i32 0, i32 3), align 8, !tbaa !22
  store ptr %74, ptr getelementptr inbounds (%struct.pal_global, ptr @__pal_global, i32 0, i32 2), align 8, !tbaa !19
  br label %80

75:                                               ; preds = %69
  %76 = load ptr, ptr %11, align 8, !tbaa !5
  %77 = load ptr, ptr getelementptr inbounds (%struct.pal_global, ptr @__pal_global, i32 0, i32 3), align 8, !tbaa !22
  %78 = getelementptr inbounds %struct.team, ptr %77, i32 0, i32 0
  store ptr %76, ptr %78, align 8, !tbaa !23
  %79 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %79, ptr getelementptr inbounds (%struct.pal_global, ptr @__pal_global, i32 0, i32 3), align 8, !tbaa !22
  br label %80

80:                                               ; preds = %75, %73
  %81 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %81, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %86

82:                                               ; preds = %66, %54
  %83 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %83) #6
  %84 = load i32, ptr %13, align 4, !tbaa !9
  %85 = call ptr @p_ref_err(i32 noundef %84)
  store ptr %85, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %86

86:                                               ; preds = %82, %80, %24, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  %87 = load ptr, ptr %5, align 8
  ret ptr %87
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @p_ref_is_err(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @p_ref_err(i32 noundef) #2

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @p_error(ptr noundef) #2

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { nounwind allocsize(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 nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 8}
!12 = !{!"team", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 28, !7, i64 40, !7, i64 52}
!13 = !{!12, !10, i64 24}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !6, i64 0}
!16 = !{!"dev", !6, i64 0, !10, i64 8, !7, i64 12, !7, i64 24}
!17 = !{!18, !6, i64 24}
!18 = !{!"dev_ops", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144}
!19 = !{!20, !6, i64 120}
!20 = !{!"pal_global", !7, i64 0, !12, i64 56, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !21, i64 152, !10, i64 180}
!21 = !{!"TINYMT32_T", !7, i64 0, !10, i64 16, !10, i64 20, !10, i64 24}
!22 = !{!20, !6, i64 128}
!23 = !{!12, !6, 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"

%struct.pal_global = type { [7 x ptr], %struct.team, ptr, ptr, ptr, ptr, %struct.TINYMT32_T, i32 }
%struct.team = type { ptr, ptr, i32, i32, i32, %union.p_coords, %union.p_coords, %union.p_coords }
%union.p_coords = type { %struct.anon }
%struct.anon = type { i32, i32, i32 }
%struct.TINYMT32_T = type { [4 x i32], i32, i32, i32 }

@__pal_global = external local_unnamed_addr global %struct.pal_global, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @p_open4(ptr noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = tail call zeroext i1 @p_ref_is_err(ptr noundef %0)
  br i1 %5, label %6, label %8

6:                                                ; preds = %4
  %7 = tail call ptr @p_ref_err(i32 noundef 22)
  br label %36

8:                                                ; preds = %4
  %9 = tail call noalias dereferenceable_or_null(64) ptr @malloc(i64 noundef 64) #5
  %.not = icmp eq ptr %9, null
  br i1 %.not, label %10, label %12

10:                                               ; preds = %8
  %11 = tail call ptr @p_ref_err(i32 noundef 12)
  br label %36

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %9, i64 8
  store ptr %0, ptr %13, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %9, i64 24
  store i32 %1, ptr %14, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %9, i64 28
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(12) %15, ptr noundef nonnull align 4 dereferenceable(12) %2, i64 12, i1 false)
  %16 = getelementptr inbounds i8, ptr %9, i64 40
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(12) %16, ptr noundef nonnull align 4 dereferenceable(12) %3, i64 12, i1 false)
  switch i32 %1, label %34 [
    i32 0, label %21
    i32 2, label %17
    i32 1, label %19
  ]

17:                                               ; preds = %12
  %18 = getelementptr inbounds i8, ptr %9, i64 60
  store i32 -1, ptr %18, align 4, !tbaa !12
  br label %19

19:                                               ; preds = %17, %12
  %20 = getelementptr inbounds i8, ptr %9, i64 56
  store i32 -1, ptr %20, align 4, !tbaa !12
  br label %21

21:                                               ; preds = %12, %19
  %.sink = getelementptr inbounds i8, ptr %9, i64 52
  store i32 -1, ptr %.sink, align 4, !tbaa !12
  %22 = load ptr, ptr %0, align 8, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %22, i64 24
  %24 = load ptr, ptr %23, align 8, !tbaa !15
  %25 = tail call ptr %24(ptr noundef nonnull %9) #6
  %26 = tail call i32 @p_error(ptr noundef %25)
  %.not29 = icmp eq i32 %26, 0
  br i1 %.not29, label %29, label %27

27:                                               ; preds = %21
  %28 = tail call i32 @p_error(ptr noundef %25)
  br label %34

29:                                               ; preds = %21
  %30 = load ptr, ptr getelementptr inbounds (i8, ptr @__pal_global, i64 120), align 8, !tbaa !17
  %.not30 = icmp eq ptr %30, null
  br i1 %.not30, label %31, label %32

31:                                               ; preds = %29
  store ptr %25, ptr getelementptr inbounds (i8, ptr @__pal_global, i64 128), align 8, !tbaa !20
  store ptr %25, ptr getelementptr inbounds (i8, ptr @__pal_global, i64 120), align 8, !tbaa !17
  br label %36

32:                                               ; preds = %29
  %33 = load ptr, ptr getelementptr inbounds (i8, ptr @__pal_global, i64 128), align 8, !tbaa !20
  store ptr %25, ptr %33, align 8, !tbaa !21
  store ptr %25, ptr getelementptr inbounds (i8, ptr @__pal_global, i64 128), align 8, !tbaa !20
  br label %36

34:                                               ; preds = %12, %27
  %.0 = phi i32 [ %28, %27 ], [ 22, %12 ]
  tail call void @free(ptr noundef nonnull %9) #6
  %35 = tail call ptr @p_ref_err(i32 noundef %.0)
  br label %36

36:                                               ; preds = %31, %32, %34, %10, %6
  %.027 = phi ptr [ %7, %6 ], [ %35, %34 ], [ %11, %10 ], [ %25, %32 ], [ %25, %31 ]
  ret ptr %.027
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @p_ref_is_err(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @p_ref_err(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @p_error(ptr noundef) local_unnamed_addr #1

; 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 = { inlinehint 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 allocsize(0) }
attributes #6 = { 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, !7, i64 8}
!6 = !{!"team", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !8, i64 28, !8, i64 40, !8, i64 52}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!8, !8, i64 0}
!13 = !{!14, !7, i64 0}
!14 = !{!"dev", !7, i64 0, !10, i64 8, !8, i64 12, !8, i64 24}
!15 = !{!16, !7, i64 24}
!16 = !{!"dev_ops", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144}
!17 = !{!18, !7, i64 120}
!18 = !{!"pal_global", !8, i64 0, !6, i64 56, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !19, i64 152, !10, i64 180}
!19 = !{!"TINYMT32_T", !8, i64 0, !10, i64 16, !10, i64 20, !10, i64 24}
!20 = !{!18, !7, i64 128}
!21 = !{!6, !7, i64 0}


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