// bottleneck.c: throughput máximo de un camino y tiempo para enviar un archivo.
//
//   ./bottleneck <megabytes> <Mbps del enlace 1> [Mbps del enlace 2] ...
//
// Aquí mega significa 10^6, como en las velocidades de los enlaces.
// El cálculo ignora los encabezados y supone que nadie más usa el camino.
#include <stdio.h>
#include <stdlib.h>

// Convierte un argumento a un número positivo, o termina con un mensaje.
static double parse_positive(const char *text) {
    char *end;
    double value = strtod(text, &end);
    if (end == text || *end != '\0' || value <= 0) {
        fprintf(stderr, "bottleneck: no es un número positivo: %s\n", text);
        exit(EXIT_FAILURE);
    }
    return value;
}

int main(int argc, char *argv[]) {
    if (argc < 3) {
        fprintf(stderr, "uso: %s <megabytes> <Mbps> [Mbps] ...\n", argv[0]);
        return EXIT_FAILURE;
    }
    double megabytes = parse_positive(argv[1]);
    int slowest = 2;
    double min_rate = parse_positive(argv[2]);
    for (int i = 3; i < argc; i++) {
        double rate = parse_positive(argv[i]);
        if (rate < min_rate) {
            min_rate = rate;
            slowest = i;
        }
    }
    printf("cuello de botella: enlace %d, a %g Mbps\n", slowest - 1, min_rate);
    printf("tiempo para %g MB: %.2f s\n", megabytes, megabytes * 8 / min_rate);
    return EXIT_SUCCESS;
}
