; LLVM IR from cyberbotics/webots_ros2 :: webots_ros2_driver/webots/src/controller/c (keyboard)

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

@keyboard = external hidden global %struct.WbKeyboard, align 4
@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [77 x i8], align 1
@__FUNCTION__.wb_keyboard_get_key = external hidden unnamed_addr constant [20 x i8], align 1

declare void @robot_mutex_lock(...) #0

declare void @robot_mutex_unlock(...) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @wb_keyboard_get_key() #2 {
  %1 = alloca i32, align 4
  %2 = load i32, ptr getelementptr inbounds (%struct.WbKeyboard, ptr @keyboard, i32 0, i32 1), align 4, !tbaa !5
  %3 = icmp sle i32 %2, 0
  br i1 %3, label %4, label %7

4:                                                ; preds = %0
  %5 = load ptr, ptr @stderr, align 8, !tbaa !10
  %6 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef @.str, ptr noundef @__FUNCTION__.wb_keyboard_get_key) #4
  br label %7

7:                                                ; preds = %4, %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 -1, ptr %1, align 4, !tbaa !12
  call void (...) @robot_mutex_lock()
  %8 = load i8, ptr getelementptr inbounds (%struct.WbKeyboard, ptr @keyboard, i32 0, i32 2), align 4, !tbaa !13
  %9 = sext i8 %8 to i32
  %10 = icmp ne i32 %9, -1
  br i1 %10, label %11, label %23

11:                                               ; preds = %7
  %12 = load i8, ptr getelementptr inbounds (%struct.WbKeyboard, ptr @keyboard, i32 0, i32 2), align 4, !tbaa !13
  %13 = sext i8 %12 to i32
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [8 x i32], ptr @keyboard, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !12
  store i32 %16, ptr %1, align 4, !tbaa !12
  %17 = load i32, ptr %1, align 4, !tbaa !12
  %18 = icmp sge i32 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %11
  %20 = load i8, ptr getelementptr inbounds (%struct.WbKeyboard, ptr @keyboard, i32 0, i32 2), align 4, !tbaa !13
  %21 = add i8 %20, 1
  store i8 %21, ptr getelementptr inbounds (%struct.WbKeyboard, ptr @keyboard, i32 0, i32 2), align 4, !tbaa !13
  br label %22

22:                                               ; preds = %19, %11
  br label %23

23:                                               ; preds = %22, %7
  call void (...) @robot_mutex_unlock()
  %24 = load i32, ptr %1, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret i32 %24
}

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

attributes #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 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 "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 }

!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, !9, i64 32}
!6 = !{!"", !7, i64 0, !9, i64 32, !7, i64 36}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!9, !9, i64 0}
!13 = !{!6, !7, i64 36}


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

@keyboard = external hidden local_unnamed_addr global %struct.WbKeyboard, align 4
@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [77 x i8], align 1
@__FUNCTION__.wb_keyboard_get_key = external hidden unnamed_addr constant [20 x i8], align 1

declare void @robot_mutex_lock(...) local_unnamed_addr #0

declare void @robot_mutex_unlock(...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @wb_keyboard_get_key() local_unnamed_addr #1 {
  %1 = load i32, ptr getelementptr inbounds (i8, ptr @keyboard, i64 32), align 4, !tbaa !5
  %2 = icmp slt i32 %1, 1
  br i1 %2, label %3, label %6

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

6:                                                ; preds = %3, %0
  tail call void (...) @robot_mutex_lock() #4
  %7 = load i8, ptr getelementptr inbounds (i8, ptr @keyboard, i64 36), align 4, !tbaa !12
  %.not = icmp eq i8 %7, -1
  br i1 %.not, label %15, label %8

8:                                                ; preds = %6
  %9 = sext i8 %7 to i64
  %10 = getelementptr inbounds [8 x i32], ptr @keyboard, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !13
  %12 = icmp sgt i32 %11, -1
  br i1 %12, label %13, label %15

13:                                               ; preds = %8
  %14 = add nuw i8 %7, 1
  store i8 %14, ptr getelementptr inbounds (i8, ptr @keyboard, i64 36), align 4, !tbaa !12
  br label %15

15:                                               ; preds = %8, %13, %6
  %.0 = phi i32 [ %11, %13 ], [ %11, %8 ], [ -1, %6 ]
  tail call void (...) @robot_mutex_unlock() #4
  ret i32 %.0
}

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

attributes #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 = { 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 = { 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 = { cold nounwind }
attributes #4 = { 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, !9, i64 32}
!6 = !{!"", !7, i64 0, !9, i64 32, !7, i64 36}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!6, !7, i64 36}
!13 = !{!9, !9, i64 0}


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