// Minimal HTTP server benchmark - C version (single-threaded, poll-based)
// Compile: gcc -O3 -o http_c http_server.c
// Test: wrk -t2 -c50 -d5s http://localhost:8080/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#define PORT 8080
#define RESPONSE "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 15\r\n\r\n{\"status\":\"ok\"}"

int main() {
    int server_fd, client_fd;
    struct sockaddr_in address;
    int opt = 1;
    char buffer[1024];
    socklen_t addrlen = sizeof(address);

    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    setsockopt(server_fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);

    bind(server_fd, (struct sockaddr*)&address, sizeof(address));
    listen(server_fd, 1024);

    printf("C HTTP server listening on port %d\n", PORT);
    fflush(stdout);

    while (1) {
        client_fd = accept(server_fd, (struct sockaddr*)&address, &addrlen);
        if (client_fd < 0) continue;

        read(client_fd, buffer, sizeof(buffer));
        write(client_fd, RESPONSE, strlen(RESPONSE));
        close(client_fd);
    }

    return 0;
}
