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

; 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.gpiohandle_request = type { [64 x i32], i32, [64 x i8], [32 x i8], i32, i32 }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [43 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [42 x i8], align 1
@chip_fd = external hidden global [6 x i32], align 16
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [43 x i8], align 1
@handle_fd = external hidden global [6 x [64 x i32]], align 16

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

13:                                               ; preds = %3
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = icmp sge i32 %14, 6
  br i1 %15, label %16, label %19

16:                                               ; preds = %13, %3
  %17 = load ptr, ptr @stderr, align 8, !tbaa !9
  %18 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef @.str) #7
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

19:                                               ; preds = %13
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = icmp slt i32 %20, 0
  br i1 %21, label %25, label %22

22:                                               ; preds = %19
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = icmp sge i32 %23, 64
  br i1 %24, label %25, label %28

25:                                               ; preds = %22, %19
  %26 = load ptr, ptr @stderr, align 8, !tbaa !9
  %27 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %26, ptr noundef @.str.1) #7
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

28:                                               ; preds = %22
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [6 x i32], ptr @chip_fd, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %46

34:                                               ; preds = %28
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = call i32 @__open_gpiochip(i32 noundef %35)
  %37 = icmp ne i32 %36, 0
  %38 = xor i1 %37, true
  %39 = xor i1 %38, true
  %40 = zext i1 %39 to i32
  %41 = sext i32 %40 to i64
  %42 = call i64 @llvm.expect.i64(i64 %41, i64 0)
  %43 = icmp ne i64 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %34
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

45:                                               ; preds = %34
  br label %46

46:                                               ; preds = %45, %28
  call void @llvm.memset.p0.i64(ptr align 4 %9, i8 0, i64 364, i1 false)
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = getelementptr inbounds %struct.gpiohandle_request, ptr %9, i32 0, i32 0
  %49 = getelementptr inbounds [64 x i32], ptr %48, i64 0, i64 0
  store i32 %47, ptr %49, align 4, !tbaa !5
  %50 = getelementptr inbounds %struct.gpiohandle_request, ptr %9, i32 0, i32 4
  store i32 1, ptr %50, align 4, !tbaa !11
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = getelementptr inbounds %struct.gpiohandle_request, ptr %9, i32 0, i32 1
  store i32 %51, ptr %52, align 4, !tbaa !13
  %53 = call ptr @__errno_location() #8
  store i32 0, ptr %53, align 4, !tbaa !5
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [6 x i32], ptr @chip_fd, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = call i32 (i32, i64, ...) @ioctl(i32 noundef %57, i64 noundef 3245126659, ptr noundef %9) #7
  store i32 %58, ptr %8, align 4, !tbaa !5
  %59 = load i32, ptr %8, align 4, !tbaa !5
  %60 = icmp eq i32 %59, -1
  %61 = xor i1 %60, true
  %62 = xor i1 %61, true
  %63 = zext i1 %62 to i32
  %64 = sext i32 %63 to i64
  %65 = call i64 @llvm.expect.i64(i64 %64, i64 0)
  %66 = icmp ne i64 %65, 0
  br i1 %66, label %67, label %68

67:                                               ; preds = %46
  call void @perror(ptr noundef @.str.2)
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

68:                                               ; preds = %46
  %69 = getelementptr inbounds %struct.gpiohandle_request, ptr %9, i32 0, i32 5
  %70 = load i32, ptr %69, align 4, !tbaa !14
  %71 = icmp eq i32 %70, 0
  br i1 %71, label %72, label %75

72:                                               ; preds = %68
  %73 = load ptr, ptr @stderr, align 8, !tbaa !9
  %74 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %73, ptr noundef @.str.3) #7
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

75:                                               ; preds = %68
  %76 = getelementptr inbounds %struct.gpiohandle_request, ptr %9, i32 0, i32 5
  %77 = load i32, ptr %76, align 4, !tbaa !14
  %78 = load i32, ptr %5, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [6 x [64 x i32]], ptr @handle_fd, i64 0, i64 %79
  %81 = load i32, ptr %6, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [64 x i32], ptr %80, i64 0, i64 %82
  store i32 %77, ptr %83, align 4, !tbaa !5
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

84:                                               ; preds = %75, %72, %67, %44, %25, %16
  call void @llvm.lifetime.end.p0(i64 364, ptr %9) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #7
  %85 = load i32, ptr %4, align 4
  ret i32 %85
}

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

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

; Function Attrs: nounwind uwtable
declare hidden i32 @__open_gpiochip(i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #3

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #5

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

declare void @perror(ptr noundef) #6

; 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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind willreturn memory(none) "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 = { "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 #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(none) }

!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, !6, i64 356}
!12 = !{!"gpiohandle_request", !7, i64 0, !6, i64 256, !7, i64 260, !7, i64 324, !6, i64 356, !6, i64 360}
!13 = !{!12, !6, i64 256}
!14 = !{!12, !6, i64 360}


; 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.gpiohandle_request = type { [64 x i32], i32, [64 x i8], [32 x i8], i32, i32 }

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [43 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [42 x i8], align 1
@chip_fd = external hidden local_unnamed_addr global [6 x i32], align 16
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [43 x i8], align 1
@handle_fd = external hidden local_unnamed_addr global [6 x [64 x i32]], align 16

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @rc_gpio_init(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.gpiohandle_request, align 4
  call void @llvm.lifetime.start.p0(i64 364, ptr nonnull %4) #6
  %or.cond = icmp ugt i32 %0, 5
  br i1 %or.cond, label %5, label %8

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

8:                                                ; preds = %3
  %or.cond3 = icmp ugt i32 %1, 63
  br i1 %or.cond3, label %9, label %12

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

12:                                               ; preds = %8
  %13 = zext nneg i32 %0 to i64
  %14 = getelementptr inbounds [6 x i32], ptr @chip_fd, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %19

17:                                               ; preds = %12
  %18 = tail call i32 @__open_gpiochip(i32 noundef %0)
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %19, label %38, !prof !11

19:                                               ; preds = %17, %12
  %20 = getelementptr inbounds i8, ptr %4, i64 4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(364) %20, i8 0, i64 360, i1 false)
  store i32 %1, ptr %4, align 4, !tbaa !9
  %21 = getelementptr inbounds i8, ptr %4, i64 356
  store i32 1, ptr %21, align 4, !tbaa !12
  %22 = getelementptr inbounds i8, ptr %4, i64 256
  store i32 %2, ptr %22, align 4, !tbaa !14
  %23 = tail call ptr @__errno_location() #8
  store i32 0, ptr %23, align 4, !tbaa !9
  %24 = load i32, ptr %14, align 4, !tbaa !9
  %25 = call i32 (i32, i64, ...) @ioctl(i32 noundef %24, i64 noundef 3245126659, ptr noundef nonnull %4) #6
  %26 = icmp eq i32 %25, -1
  br i1 %26, label %27, label %28, !prof !15

27:                                               ; preds = %19
  call void @perror(ptr noundef nonnull @.str.2) #9
  br label %38

28:                                               ; preds = %19
  %29 = getelementptr inbounds i8, ptr %4, i64 360
  %30 = load i32, ptr %29, align 4, !tbaa !16
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %35

32:                                               ; preds = %28
  %33 = load ptr, ptr @stderr, align 8, !tbaa !5
  %34 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef nonnull @.str.3) #7
  br label %38

35:                                               ; preds = %28
  %36 = zext nneg i32 %1 to i64
  %37 = getelementptr inbounds [6 x [64 x i32]], ptr @handle_fd, i64 0, i64 %13, i64 %36
  store i32 %30, ptr %37, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %17, %35, %32, %27, %9, %5
  %.0 = phi i32 [ -1, %5 ], [ -1, %9 ], [ -1, %27 ], [ -1, %32 ], [ 0, %35 ], [ -1, %17 ]
  call void @llvm.lifetime.end.p0(i64 364, ptr nonnull %4) #6
  ret i32 %.0
}

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

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

; Function Attrs: nounwind uwtable
declare hidden i32 @__open_gpiochip(i32 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #4

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

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

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 "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 = { cold nounwind }
attributes #8 = { nounwind willreturn memory(none) }
attributes #9 = { 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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!12 = !{!13, !10, i64 356}
!13 = !{!"gpiohandle_request", !7, i64 0, !10, i64 256, !7, i64 260, !7, i64 324, !10, i64 356, !10, i64 360}
!14 = !{!13, !10, i64 256}
!15 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!16 = !{!13, !10, i64 360}


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