; 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.34 = external hidden unnamed_addr constant [56 x i8], align 1
@.str.35 = external hidden unnamed_addr constant [58 x i8], align 1
@.str.36 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.37 = external hidden unnamed_addr constant [49 x i8], align 1
@.str.38 = external hidden unnamed_addr constant [25 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: nounwind uwtable
define dso_local i32 @rc_spi_transfer(i32 noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) #3 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca %struct.spi_ioc_transfer, align 8
  %14 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !9
  store i64 %3, ptr %10, align 8, !tbaa !11
  store ptr %4, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr %13) #5
  call void @llvm.memset.p0.i64(ptr align 8 %13, i8 0, i64 32, i1 false)
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %20, label %17

17:                                               ; preds = %5
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, 5
  br i1 %19, label %20, label %23

20:                                               ; preds = %17, %5
  %21 = load ptr, ptr @stderr, align 8, !tbaa !9
  %22 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef @.str.34, i32 noundef 5) #5
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %88

23:                                               ; preds = %17
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = icmp slt i32 %24, 0
  br i1 %25, label %29, label %26

26:                                               ; preds = %23
  %27 = load i32, ptr %8, align 4, !tbaa !5
  %28 = icmp sge i32 %27, 12
  br i1 %28, label %29, label %32

29:                                               ; preds = %26, %23
  %30 = load ptr, ptr @stderr, align 8, !tbaa !9
  %31 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %30, ptr noundef @.str.35, i32 noundef 11) #5
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %88

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

43:                                               ; preds = %32
  %44 = load ptr, ptr @stderr, align 8, !tbaa !9
  %45 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %44, ptr noundef @.str.36) #5
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %88

46:                                               ; preds = %32
  %47 = load i64, ptr %10, align 8, !tbaa !11
  %48 = icmp ult i64 %47, 1
  br i1 %48, label %49, label %52

49:                                               ; preds = %46
  %50 = load ptr, ptr @stderr, align 8, !tbaa !9
  %51 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %50, ptr noundef @.str.37) #5
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %88

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

85:                                               ; preds = %52
  call void @perror(ptr noundef @.str.38)
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %88

86:                                               ; preds = %52
  %87 = load i32, ptr %12, align 4, !tbaa !5
  store i32 %87, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %88

88:                                               ; preds = %86, %85, %49, %43, %29, %20
  call void @llvm.lifetime.end.p0(i64 32, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  %89 = load i32, ptr %6, align 4
  ret i32 %89
}

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

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 = { 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 nounwind willreturn memory(argmem: write) }
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.34 = external hidden unnamed_addr constant [56 x i8], align 1
@.str.35 = external hidden unnamed_addr constant [58 x i8], align 1
@.str.36 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.37 = external hidden unnamed_addr constant [49 x i8], align 1
@.str.38 = external hidden unnamed_addr constant [25 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_transfer(i32 noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) local_unnamed_addr #3 {
  %6 = alloca %struct.spi_ioc_transfer, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #4
  %7 = getelementptr inbounds i8, ptr %6, i64 24
  store i64 0, ptr %7, align 8
  %or.cond = icmp ugt i32 %0, 5
  br i1 %or.cond, label %8, label %11

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

11:                                               ; preds = %5
  %or.cond3 = icmp ugt i32 %1, 11
  br i1 %or.cond3, label %12, label %15

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

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

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

25:                                               ; preds = %15
  %26 = icmp eq i64 %3, 0
  br i1 %26, label %27, label %30

27:                                               ; preds = %25
  %28 = load ptr, ptr @stderr, align 8, !tbaa !5
  %29 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %28, ptr noundef nonnull @.str.37) #5
  br label %48

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

47:                                               ; preds = %30
  call void @perror(ptr noundef nonnull @.str.38) #6
  br label %48

48:                                               ; preds = %30, %47, %27, %22, %12, %8
  %.0 = phi i32 [ -1, %8 ], [ -1, %12 ], [ -1, %22 ], [ -1, %27 ], [ -1, %47 ], [ %45, %30 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #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