#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int atoi(char *);
int buscoViaConMenorAuto(int *, int);
int *createShareMemory(int /*<<< orphan*/, int);
int /*<<< orphan*/ creo_clave(int);
int /*<<< orphan*/ printf(char *, int);
int random_generate(int, int);
int /*<<< orphan*/ random_init();
int sem_mutex_create(int /*<<< orphan*/);
int /*<<< orphan*/ sem_mutex_post(int *);
int /*<<< orphan*/ sem_mutex_wait(int *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ validaParametrosInput(int, char **);
int main(int argc, char *argv[]) {
  key_t keySem, keyMem;
  int semID;
  int *vias;
  validaParametrosInput(argc, argv);
  int cantVias = atoi(argv[1]);
  int cantidadLiberaciones = 0;
  random_init();
  keySem = creo_clave(755);
  keyMem = creo_clave(655);
  semID = sem_mutex_create(keySem);
  vias = createShareMemory(keyMem, sizeof(int) * cantVias);
  while (1) {
    sem_mutex_wait(&semID);
    int msToWait = random_generate(400, 600);
    int menorVia = buscoViaConMenorAuto(vias, cantVias);
    printf("\nAuto Ingresando a VIA %d", menorVia + 1);
    vias[menorVia]++;
    sem_mutex_post(&semID);
    usleep(1000 * msToWait);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <validaParametrosInput@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1090 <random_init@plt>
mov    $0x2f3,%edi
call   1040 <creo_clave@plt>
mov    %eax,-0x14(%rbp)
mov    $0x28f,%edi
call   1040 <creo_clave@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   10a0 <sem_mutex_create@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   10b0 <createShareMemory@plt>
mov    %rax,-0x28(%rbp)
lea    -0x1c(%rbp),%rdi
call   10c0 <sem_mutex_wait@plt>
mov    $0x190,%edi
mov    $0x258,%esi
call   1030 <random_generate@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1060 <buscoViaConMenorAuto@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x40(%rbp)
lea    0xd8b(%rip),%rdi
call   1070 <printf@plt>
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
mov    %edx,(%rax,%rcx,4)
lea    -0x1c(%rbp),%rdi
call   1080 <sem_mutex_post@plt>
imul   $0x3e8,-0x34(%rbp),%edi
call   10e0 <usleep@plt>
jmp    123b <main+0x7b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1050 <validaParametrosInput@plt>
mov    0x8(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1090 <random_init@plt>
mov    $0x2f3,%edi
call   1040 <creo_clave@plt>
mov    %eax,%ebx
mov    $0x28f,%edi
call   1040 <creo_clave@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   10a0 <sem_mutex_create@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%esi
shl    $0x2,%esi
mov    %ebp,%edi
call   10b0 <createShareMemory@plt>
mov    %rax,%r13
lea    0x4(%rsp),%r12
lea    0xddb(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   10c0 <sem_mutex_wait@plt>
mov    $0x190,%edi
mov    $0x258,%esi
call   1030 <random_generate@plt>
mov    %eax,%ebp
mov    %r13,%rdi
mov    %r14d,%esi
call   1060 <buscoViaConMenorAuto@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%esi
mov    %r15,%rdi
call   1070 <printf@plt>
movslq %ebx,%rax
addl   $0x1,0x0(%r13,%rax,4)
mov    %r12,%rdi
call   1080 <sem_mutex_post@plt>
imul   $0x3e8,%ebp,%edi
call   10e0 <usleep@plt>
jmp    1230 <main+0x70>

LLVM IR (-O0):
@.str = [26 x i8] c"\0AAuto Ingresando a VIA %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @validaParametrosInput(i32 %14, i8** %15)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %10
store i32 0, i32* %11
%21 = call i32 (...) @random_init()
%22 = call i32 @creo_clave(i32 755)
store i32 %22, i32* %6
%23 = call i32 @creo_clave(i32 655)
store i32 %23, i32* %7
%24 = load i32, i32* %6
%25 = call i32 @sem_mutex_create(i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %7
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = mul i64 4, %28
%30 = trunc i64 %29 to i32
%31 = call i32* @createShareMemory(i32 %26, i32 %30)
store i32* %31, i32** %9
br label %32
32: ; preds = %2, %32
%33 = call i32 @sem_mutex_wait(i32* %8)
%34 = call i32 @random_generate(i32 400, i32 600)
store i32 %34, i32* %12
%35 = load i32*, i32** %9
%36 = load i32, i32* %10
%37 = call i32 @buscoViaConMenorAuto(i32* %35, i32 %36)
store i32 %37, i32* %13
%38 = load i32, i32* %13
%39 = add nsw i32 %38, 1
%40 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = load i32*, i32** %9
%42 = load i32, i32* %13
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
%46 = add nsw i32 %45, 1
store i32 %46, i32* %44
%47 = call i32 @sem_mutex_post(i32* %8)
%48 = load i32, i32* %12
%49 = mul nsw i32 1000, %48
%50 = call i32 @usleep(i32 %49)
br label %32
}

LLVM IR (-O2):
@.str = [26 x i8] c"\0AAuto Ingresando a VIA %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @validaParametrosInput(i32 %0, i8** %1)
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
%9 = tail call i32 (...) @random_init()
%10 = tail call i32 @creo_clave(i32 755)
%11 = tail call i32 @creo_clave(i32 655)
%12 = tail call i32 @sem_mutex_create(i32 %10)
store i32 %12, i32* %3
%13 = shl i32 %8, 2
%14 = tail call i32* @createShareMemory(i32 %11, i32 %13)
br label %15
15: ; preds = %15, %2
%16 = call i32 @sem_mutex_wait(i32* %3)
%17 = call i32 @random_generate(i32 400, i32 600)
%18 = call i32 @buscoViaConMenorAuto(i32* %14, i32 %8)
%19 = add nsw i32 %18, 1
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = sext i32 %18 to i64
%22 = getelementptr inbounds i32, i32* %14, i64 %21
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1
store i32 %24, i32* %22
%25 = call i32 @sem_mutex_post(i32* %3)
%26 = mul nsw i32 %17, 1000
%27 = call i32 @usleep(i32 %26)
br label %15
}