#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 *createShareMemory(int /*<<< orphan*/, int);
int /*<<< orphan*/ creo_clave(int);
int /*<<< orphan*/ informarViaLlena(int);
int /*<<< orphan*/ inicializarMemoria(int *, int);
int /*<<< orphan*/ mostrarVias(int *, int);
int random_generate(int, int);
int /*<<< orphan*/ random_init();
int sem_mutex_create(int /*<<< orphan*/);
int /*<<< orphan*/ sem_mutex_init(int *, int);
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);
  inicializarMemoria(vias, cantVias);
  sem_mutex_init(&semID, 1);
  while (1) {
    int j = 0;
    int msToWait = random_generate(100, 5000);
    for (j = 0; j < cantVias; j++) {
      sem_mutex_wait(&semID);
      if (vias[j] > 10) {
        vias[j] = 0;
        cantidadLiberaciones++;
        informarViaLlena(cantidadLiberaciones);
      } else if (vias[j] > 0) {
        vias[j]--;
      }
      mostrarVias(vias, cantVias);
      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   1060 <validaParametrosInput@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <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)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10f0 <inicializarMemoria@plt>
lea    -0x1c(%rbp),%rdi
mov    $0x1,%esi
call   1050 <sem_mutex_init@plt>
movl   $0x0,-0x34(%rbp)
mov    $0x64,%edi
mov    $0x1388,%esi
call   1030 <random_generate@plt>
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1338 <main+0x158>
lea    -0x1c(%rbp),%rdi
call   10d0 <sem_mutex_wait@plt>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpl   $0xa,(%rax,%rcx,4)
jle    12e1 <main+0x101>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1070 <informarViaLlena@plt>
jmp    1309 <main+0x129>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jle    1304 <main+0x124>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0xffffffff,%edx
mov    %edx,(%rax,%rcx,4)
jmp    1309 <main+0x129>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10c0 <mostrarVias@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <sem_mutex_post@plt>
imul   $0x3e8,-0x38(%rbp),%edi
call   1100 <usleep@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1295 <main+0xb5>
jmp    1275 <main+0x95>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1060 <validaParametrosInput@plt>
mov    0x8(%rbx),%rdi
call   10e0 <atoi@plt>
mov    %eax,%r13d
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <random_init@plt>
mov    $0x2f3,%edi
call   1040 <creo_clave@plt>
mov    %eax,%ebp
mov    $0x28f,%edi
call   1040 <creo_clave@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   10a0 <sem_mutex_create@plt>
mov    %eax,0x4(%rsp)
lea    0x0(,%r13,4),%esi
mov    %ebx,%edi
call   10b0 <createShareMemory@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13d,%esi
call   10f0 <inicializarMemoria@plt>
lea    0x4(%rsp),%r15
mov    %r15,%rdi
mov    $0x1,%esi
call   1050 <sem_mutex_init@plt>
mov    %r13d,%ebx
xchg   %ax,%ax
mov    $0x64,%edi
mov    $0x1388,%esi
call   1030 <random_generate@plt>
test   %ebx,%ebx
jle    1260 <main+0x80>
imul   $0x3e8,%eax,%r13d
xor    %ebp,%ebp
jmp    12b7 <main+0xd7>
xchg   %ax,%ax
movl   $0x0,(%r12,%rbp,4)
add    $0x1,%r14d
mov    %r14d,%edi
call   1070 <informarViaLlena@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   10c0 <mostrarVias@plt>
mov    %r15,%rdi
call   1080 <sem_mutex_post@plt>
mov    %r13d,%edi
call   1100 <usleep@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
je     1260 <main+0x80>
mov    %r15,%rdi
call   10d0 <sem_mutex_wait@plt>
mov    (%r12,%rbp,4),%eax
cmp    $0xb,%eax
jge    1280 <main+0xa0>
test   %eax,%eax
jle    1294 <main+0xb4>
add    $0xffffffff,%eax
mov    %eax,(%r12,%rbp,4)
jmp    1294 <main+0xb4>

LLVM IR (-O0):
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
%32 = load i32*, i32** %9
%33 = load i32, i32* %10
%34 = call i32 @inicializarMemoria(i32* %32, i32 %33)
%35 = call i32 @sem_mutex_init(i32* %8, i32 1)
br label %36
36: ; preds = %2, %85
store i32 0, i32* %12
%37 = call i32 @random_generate(i32 100, i32 5000)
store i32 %37, i32* %13
store i32 0, i32* %12
br label %38
38: ; preds = %82, %36
%39 = load i32, i32* %12
%40 = load i32, i32* %10
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %85
42: ; preds = %38
%43 = call i32 @sem_mutex_wait(i32* %8)
%44 = load i32*, i32** %9
%45 = load i32, i32* %12
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = icmp sgt i32 %48, 10
br i1 %49, label %50, label %59
50: ; preds = %42
%51 = load i32*, i32** %9
%52 = load i32, i32* %12
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
store i32 0, i32* %54
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
%57 = load i32, i32* %11
%58 = call i32 @informarViaLlena(i32 %57)
br label %74
59: ; preds = %42
%60 = load i32*, i32** %9
%61 = load i32, i32* %12
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %59
%67 = load i32*, i32** %9
%68 = load i32, i32* %12
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i32, i32* %67, i64 %69
%71 = load i32, i32* %70
%72 = add nsw i32 %71, -1
store i32 %72, i32* %70
br label %73
73: ; preds = %66, %59
br label %74
74: ; preds = %73, %50
%75 = load i32*, i32** %9
%76 = load i32, i32* %10
%77 = call i32 @mostrarVias(i32* %75, i32 %76)
%78 = call i32 @sem_mutex_post(i32* %8)
%79 = load i32, i32* %13
%80 = mul nsw i32 1000, %79
%81 = call i32 @usleep(i32 %80)
br label %82
82: ; preds = %74
%83 = load i32, i32* %12
%84 = add nsw i32 %83, 1
store i32 %84, i32* %12
br label %38
85: ; preds = %38
br label %36
}

LLVM IR (-O2):
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)
%15 = tail call i32 @inicializarMemoria(i32* %14, i32 %8)
%16 = call i32 @sem_mutex_init(i32* %3, i32 1)
%17 = icmp sgt i32 %8, 0
%18 = zext i32 %8 to i64
br label %19
19: ; preds = %39, %2
%20 = phi i32 [ 0, %2 ], [ %40, %39 ]
br label %21
21: ; preds = %19, %21
%22 = call i32 @random_generate(i32 100, i32 5000)
br i1 %17, label %23, label %21
23: ; preds = %21
%24 = mul nsw i32 %22, 1000
br label %25
25: ; preds = %23, %39
%26 = phi i64 [ 0, %23 ], [ %44, %39 ]
%27 = phi i32 [ %20, %23 ], [ %40, %39 ]
%28 = call i32 @sem_mutex_wait(i32* %3)
%29 = getelementptr inbounds i32, i32* %14, i64 %26
%30 = load i32, i32* %29
%31 = icmp sgt i32 %30, 10
br i1 %31, label %32, label %35
32: ; preds = %25
store i32 0, i32* %29
%33 = add nsw i32 %27, 1
%34 = call i32 @informarViaLlena(i32 %33)
br label %39
35: ; preds = %25
%36 = icmp sgt i32 %30, 0
br i1 %36, label %37, label %39
37: ; preds = %35
%38 = add nsw i32 %30, -1
store i32 %38, i32* %29
br label %39
39: ; preds = %35, %37, %32
%40 = phi i32 [ %33, %32 ], [ %27, %37 ], [ %27, %35 ]
%41 = call i32 @mostrarVias(i32* %14, i32 %8)
%42 = call i32 @sem_mutex_post(i32* %3)
%43 = call i32 @usleep(i32 %24)
%44 = add nuw nsw i64 %26, 1
%45 = icmp eq i64 %44, %18
br i1 %45, label %19, label %25
}