; LLVM IR from beagleboard/librobotcontrol :: library/src/io (spi)

; 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.rc_spi_state_t = type { [12 x i8], [12 x i8], [12 x i8], [12 x i8], [12 x i32], [12 x i32] }
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i16, i8, i8, i8, i8, i8, i8 }

@stderr = external global ptr, align 8
@state = external hidden global [6 x %struct.rc_spi_state_t], align 16
@.str.44 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [54 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [48 x i8], align 1
@.str.47 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.48 = external hidden unnamed_addr constant [21 x i8], align 1

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

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

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

declare void @perror(ptr noundef) #2

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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

15:                                               ; preds = %4
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = icmp sgt i32 %16, 5
  br i1 %17, label %18, label %21

18:                                               ; preds = %15, %4
  %19 = load ptr, ptr @stderr, align 8, !tbaa !9
  %20 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %19, ptr noundef @.str.44, i32 noundef 5) #5
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %84

21:                                               ; preds = %15
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = icmp slt i32 %22, 0
  br i1 %23, label %27, label %24

24:                                               ; preds = %21
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = icmp sge i32 %25, 12
  br i1 %26, label %27, label %30

27:                                               ; preds = %24, %21
  %28 = load ptr, ptr @stderr, align 8, !tbaa !9
  %29 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %28, ptr noundef @.str.45, i32 noundef 11) #5
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %84

30:                                               ; preds = %24
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.rc_spi_state_t, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [12 x i8], ptr %34, i64 0, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !13
  %39 = zext i8 %38 to i32
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %44

41:                                               ; preds = %30
  %42 = load ptr, ptr @stderr, align 8, !tbaa !9
  %43 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %42, ptr noundef @.str.46) #5
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %84

44:                                               ; preds = %30
  %45 = load i64, ptr %9, align 8, !tbaa !11
  %46 = icmp ult i64 %45, 1
  br i1 %46, label %47, label %50

47:                                               ; preds = %44
  %48 = load ptr, ptr @stderr, align 8, !tbaa !9
  %49 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %48, ptr noundef @.str.47) #5
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %84

50:                                               ; preds = %44
  %51 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %11, i32 0, i32 0
  store i64 0, ptr %51, align 8, !tbaa !14
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = ptrtoint ptr %52 to i64
  %54 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %11, i32 0, i32 1
  store i64 %53, ptr %54, align 8, !tbaa !18
  %55 = load i64, ptr %9, align 8, !tbaa !11
  %56 = trunc i64 %55 to i32
  %57 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %11, i32 0, i32 2
  store i32 %56, ptr %57, align 8, !tbaa !19
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %59
  %61 = getelementptr inbounds %struct.rc_spi_state_t, ptr %60, i32 0, i32 4
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [12 x i32], ptr %61, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %11, i32 0, i32 3
  store i32 %65, ptr %66, align 4, !tbaa !20
  %67 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %11, i32 0, i32 4
  store i16 0, ptr %67, align 8, !tbaa !21
  %68 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %11, i32 0, i32 5
  store i8 8, ptr %68, align 2, !tbaa !22
  %69 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %11, i32 0, i32 6
  store i8 1, ptr %69, align 1, !tbaa !23
  %70 = load i32, ptr %6, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %71
  %73 = getelementptr inbounds %struct.rc_spi_state_t, ptr %72, i32 0, i32 5
  %74 = load i32, ptr %7, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [12 x i32], ptr %73, i64 0, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !5
  %78 = call i32 (i32, i64, ...) @ioctl(i32 noundef %77, i64 noundef 1075866368, ptr noundef %11) #5
  store i32 %78, ptr %10, align 4, !tbaa !5
  %79 = load i32, ptr %10, align 4, !tbaa !5
  %80 = icmp eq i32 %79, -1
  br i1 %80, label %81, label %82

81:                                               ; preds = %50
  call void @perror(ptr noundef @.str.48)
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %84

82:                                               ; preds = %50
  %83 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %83, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %84

84:                                               ; preds = %82, %81, %47, %41, %27, %18
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %85 = load i32, ptr %5, align 4
  ret i32 %85
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !16, i64 0}
!15 = !{!"spi_ioc_transfer", !16, i64 0, !16, i64 8, !6, i64 16, !6, i64 20, !17, i64 24, !7, i64 26, !7, i64 27, !7, i64 28, !7, i64 29, !7, i64 30, !7, i64 31}
!16 = !{!"long long", !7, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = !{!15, !16, i64 8}
!19 = !{!15, !6, i64 16}
!20 = !{!15, !6, i64 20}
!21 = !{!15, !17, i64 24}
!22 = !{!15, !7, i64 26}
!23 = !{!15, !7, i64 27}


; 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.rc_spi_state_t = type { [12 x i8], [12 x i8], [12 x i8], [12 x i8], [12 x i32], [12 x i32] }
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i16, i8, i8, i8, i8, i8, i8 }

@stderr = external local_unnamed_addr global ptr, align 8
@state = external hidden local_unnamed_addr global [6 x %struct.rc_spi_state_t], align 16
@.str.44 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [54 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [48 x i8], align 1
@.str.47 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.48 = external hidden unnamed_addr constant [21 x i8], align 1

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

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

; Function Attrs: nounwind
declare i32 @ioctl(i32 noundef, i64 noundef, ...) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare void @perror(ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: mustprogress 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 i32 @rc_spi_read(i32 noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #3 {
  %5 = alloca %struct.spi_ioc_transfer, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #4
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  store i64 0, ptr %6, align 8
  %or.cond = icmp ugt i32 %0, 5
  br i1 %or.cond, label %7, label %10

7:                                                ; preds = %4
  %8 = load ptr, ptr @stderr, align 8, !tbaa !5
  %9 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %8, ptr noundef nonnull @.str.44, i32 noundef 5) #5
  br label %46

10:                                               ; preds = %4
  %or.cond3 = icmp ugt i32 %1, 11
  br i1 %or.cond3, label %11, label %14

11:                                               ; preds = %10
  %12 = load ptr, ptr @stderr, align 8, !tbaa !5
  %13 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef nonnull @.str.45, i32 noundef 11) #5
  br label %46

14:                                               ; preds = %10
  %15 = zext nneg i32 %0 to i64
  %16 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %15
  %17 = zext nneg i32 %1 to i64
  %18 = getelementptr inbounds [12 x i8], ptr %16, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = icmp eq i8 %19, 0
  br i1 %20, label %21, label %24

21:                                               ; preds = %14
  %22 = load ptr, ptr @stderr, align 8, !tbaa !5
  %23 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str.46) #5
  br label %46

24:                                               ; preds = %14
  %25 = icmp eq i64 %3, 0
  br i1 %25, label %26, label %29

26:                                               ; preds = %24
  %27 = load ptr, ptr @stderr, align 8, !tbaa !5
  %28 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %27, ptr noundef nonnull @.str.47) #5
  br label %46

29:                                               ; preds = %24
  store i64 0, ptr %5, align 8, !tbaa !10
  %30 = ptrtoint ptr %2 to i64
  %31 = getelementptr inbounds i8, ptr %5, i64 8
  store i64 %30, ptr %31, align 8, !tbaa !15
  %32 = trunc i64 %3 to i32
  %33 = getelementptr inbounds i8, ptr %5, i64 16
  store i32 %32, ptr %33, align 8, !tbaa !16
  %34 = getelementptr inbounds i8, ptr %16, i64 48
  %35 = getelementptr inbounds [12 x i32], ptr %34, i64 0, i64 %17
  %36 = load i32, ptr %35, align 4, !tbaa !17
  %37 = getelementptr inbounds i8, ptr %5, i64 20
  store i32 %36, ptr %37, align 4, !tbaa !18
  %38 = getelementptr inbounds i8, ptr %5, i64 26
  store i8 8, ptr %38, align 2, !tbaa !19
  %39 = getelementptr inbounds i8, ptr %5, i64 27
  store i8 1, ptr %39, align 1, !tbaa !20
  %40 = getelementptr inbounds i8, ptr %16, i64 96
  %41 = getelementptr inbounds [12 x i32], ptr %40, i64 0, i64 %17
  %42 = load i32, ptr %41, align 4, !tbaa !17
  %43 = call i32 (i32, i64, ...) @ioctl(i32 noundef %42, i64 noundef 1075866368, ptr noundef nonnull %5) #4
  %44 = icmp eq i32 %43, -1
  br i1 %44, label %45, label %46

45:                                               ; preds = %29
  call void @perror(ptr noundef nonnull @.str.48) #6
  br label %46

46:                                               ; preds = %29, %45, %26, %21, %11, %7
  %.0 = phi i32 [ -1, %7 ], [ -1, %11 ], [ -1, %21 ], [ -1, %26 ], [ -1, %45 ], [ %43, %29 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #4
  ret i32 %.0
}

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

!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 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 0}
!11 = !{!"spi_ioc_transfer", !12, i64 0, !12, i64 8, !13, i64 16, !13, i64 20, !14, i64 24, !7, i64 26, !7, i64 27, !7, i64 28, !7, i64 29, !7, i64 30, !7, i64 31}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!11, !12, i64 8}
!16 = !{!11, !13, i64 16}
!17 = !{!13, !13, i64 0}
!18 = !{!11, !13, i64 20}
!19 = !{!11, !7, i64 26}
!20 = !{!11, !7, i64 27}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass