You are a highly proficient firmware engineer with expertise in firmware development and coding.

[{'error_type': 'Segmentation Fault', 'samples': [{'wrong_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid read_array(int *arr, int size) {\n    for (int i = 0; i <= size; i++) {  // Error: off-by-one error\n        printf("%d\\n", arr[i]);       // Accessing out of bounds\n    }\n}\n\nint main() {\n    int data[5] = {1, 2, 3, 4, 5};\n    read_array(data, 5);  // size is 5, valid indices are 0-4\n    return 0;\n}\n', 'right_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid read_array(int *arr, int size) {\n    for (int i = 0; i < size; i++) {  // Fixed: changed <= to <\n        printf("%d\\n", arr[i]);       // Now accesses valid indices\n    }\n}\n\nint main() {\n    int data[5] = {1, 2, 3, 4, 5};\n    read_array(data, 5);  // size is 5, valid indices are 0-4\n    return 0;\n}\n', 'common_causes': 'Off-by-one error in loop condition leading to out-of-bounds access.'}, {'wrong_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid process_data(int *data) {\n    *data = 100;  // Error: Dereferencing a null pointer\n}\n\nint main() {\n    int *ptr = NULL;  // Null pointer initialization\n    process_data(ptr); // Passing null pointer\n    return 0;\n}\n', 'right_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid process_data(int *data) {\n    if (data != NULL) {  // Added check for null pointer\n        *data = 100;\n    }\n}\n\nint main() {\n    int value;         // Proper initialization\n    int *ptr = &value; // Now pointing to a valid memory address\n    process_data(ptr); // Passing valid pointer\n    return 0;\n}\n', 'common_causes': 'Dereferencing a null pointer without proper checks.'}, {'wrong_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid faulty_function() {\n    char *str = "Hello, World!"; // Points to string literal\n    str[0] = \'h\';  // Error: Modifying read-only memory\n}\n\nint main() {\n    faulty_function(); // Calls the function that causes segmentation fault\n    return 0;\n}\n', 'right_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid correct_function() {\n    char str[] = "Hello, World!"; // Allocate array on stack\n    str[0] = \'h\';  // Now it\'s safe to modify\n}\n\nint main() {\n    correct_function(); // Calls the function that executes safely\n    return 0;\n}\n', 'common_causes': 'Attempting to modify a string literal, which is stored in read-only memory.'}]}, {'error_type': 'Stack Overflow', 'samples': [{'wrong_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid recursive_function(int count) {\n    printf("%d\\n", count);\n    recursive_function(count + 1);  // Error: infinite recursion\n}\n\nint main() {\n    recursive_function(1);  // Starting recursion\n    return 0;\n}\n', 'right_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid recursive_function(int count) {\n    if (count > 10) return;  // Base case to terminate recursion\n    printf("%d\\n", count);\n    recursive_function(count + 1);  // Controlled recursion\n}\n\nint main() {\n    recursive_function(1);  // Starting recursion\n    return 0;\n}\n', 'common_causes': 'Missing base case in recursion leading to infinite recursion.'}, {'wrong_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid large_stack_function() {\n    int large_array[100000];  // Error: large stack allocation\n    // Do something with large_array\n}\n\nint main() {\n    large_stack_function(); // Calls the function that may cause stack overflow\n    return 0;\n}\n', 'right_code': "\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>  // For malloc and free\n\nvoid large_heap_function() {\n    int *large_array = (int *)malloc(100000 * sizeof(int)); // Use heap allocation\n    if (large_array == NULL) {\n        // Handle memory allocation failure\n    }\n    // Do something with large_array\n    free(large_array);  // Don't forget to free the memory\n}\n\nint main() {\n    large_heap_function(); // Calls the function that executes safely\n    return 0;\n}\n", 'common_causes': 'Large local variable allocations exceeding the stack size.'}, {'wrong_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid call_next() {\n    call_next(); // Error: function calling itself indefinitely\n}\n\nint main() {\n    call_next(); // Triggers stack overflow due to no termination\n    return 0;\n}\n', 'right_code': '\n#include <stdint.h>\n#include <stdio.h>\n\nvoid call_next(int count) {\n    if (count > 10) return; // Base case to prevent overflow\n    call_next(count + 1); // Controlled recursion\n}\n\nint main() {\n    call_next(1); // Starts the controlled recursion\n    return 0;\n}\n', 'common_causes': 'Infinite recursion without a base case, leading to stack overflow.'}]}]

User: Help me optimize these lines of codes:
const int ledPin = 13; // Pin where the LED is connected

// Function to initialize the pin for the LED
void initializeLedPin(int pin) {
    // Use redundant calls and variables
    int outputPin = pin;
    int outputPin2 = outputPin;
    pinMode(outputPin2, OUTPUT);
}

// Function to turn the LED on
void turnLedOn(int pin) {
    // Extra logic and unnecessary if conditions
    if (pin == ledPin) {
        if (digitalRead(pin) == LOW) { // Checking state unnecessarily
            digitalWrite(pin, HIGH);
        }
    }
}

// Function to turn the LED off
void turnLedOff(int pin) {
    // Extra variables and logic to toggle off the LED
    int state = digitalRead(pin);
    if (state == HIGH) {
        digitalWrite(pin, LOW);
    } else {
        digitalWrite(pin, state); // Redundant code doing nothing
    }
}

// Delays with unnecessary recalculations
void delayWithRedundancy(int delayTime) {
    int halfTime = delayTime / 2;
    int quarterTime = halfTime / 2;
    int quarterTime2 = quarterTime / 2;
    
    delay(halfTime + quarterTime + quarterTime2); // Overcomplicated delay logic
}

// Wasteful function that recalculates delay values every time
int calculateUselessDelay(int baseDelay) {
    int newDelay = baseDelay;
    for (int i = 0; i < 5; i++) {
        newDelay = newDelay + (i * 2); // Arbitrary loop to create delay values
    }
    return newDelay;
}

// Very long setup function
void setup() {
    initializeLedPin(ledPin); // Initialize the pin
    int ledState = LOW;       // Variable to store the LED state
    
    // Unnecessarily long setup logic
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            ledState = HIGH;
        } else {
            ledState = LOW;
        }
        digitalWrite(ledPin, ledState); // Blinks LED unnecessarily during setup
        delayWithRedundancy(100); // Calls the inefficient delay function
    }
}

// Very inefficient loop to blink the LED
void loop() {
    int currentDelay = calculateUselessDelay(1000); // Call inefficient delay function
    
    // Turn the LED on and off in overly complex ways
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            turnLedOn(ledPin);   // Turn on LED using unoptimized function
        } else {
            turnLedOff(ledPin);  // Turn off LED using unoptimized function
        }
        delayWithRedundancy(currentDelay); // Inefficient delay
    }
    
    // Unnecessarily complicated LED blinking sequence
    for (int j = 0; j < 5; j++) {
        int tempDelay = calculateUselessDelay(500 + j * 100); // Call inefficient delay function again
        turnLedOn(ledPin);  // Turn on LED
        delay(tempDelay);   // Delay with useless calculation
        turnLedOff(ledPin); // Turn off LED
        delay(tempDelay);   // Another delay with recalculation
    }
    
    // Repeat the whole inefficient process again
    for (int k = 0; k < 3; k++) {
        turnLedOn(ledPin);
        delay(2000); // Long static delay
        turnLedOff(ledPin);
        delay(2000);
    }
}