#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ creo_clave(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ remove(char *);
int sem_mutex_create(int /*<<< orphan*/);
int /*<<< orphan*/ sem_mutex_post(int *);
int /*<<< orphan*/ sem_mutex_wait(int *);
int /*<<< orphan*/ usleep(int);
int main() {
  printf("Consumimos \n");
  FILE *consumidor;
  char cadena[100];
  key_t key = creo_clave(55);
  int semaforoid = sem_mutex_create(key);
  while (1) {
    printf("\n voy a esperar...");
    sem_mutex_wait(&semaforoid);
    printf("\n entre en seccion critica");
    consumidor = fopen("p.txt", "r");
    printf("abriendo archivo \n");
    if (consumidor == NULL) {
      perror("Error p.txt");
      return -1;
    }
    while (fgets(cadena, 100, consumidor) != NULL) {
      printf("%s", cadena);
      usleep(500000);
    };
    fclose(consumidor);
    remove("p.txt");
    sem_mutex_post(&semaforoid);
    usleep(500000);
  };
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x37,%edi
call   1030 <creo_clave@plt>
mov    %eax,-0x84(%rbp)
mov    -0x84(%rbp),%edi
call   1090 <sem_mutex_create@plt>
mov    %eax,-0x88(%rbp)
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x88(%rbp),%rdi
call   10c0 <sem_mutex_wait@plt>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe36(%rip),%rdi
lea    0xe14(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1263 <main+0xb3>
lea    0xe00(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbp
ret
jmp    1268 <main+0xb8>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12a5 <main+0xf5>
lea    -0x80(%rbp),%rsi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x7a120,%edi
call   10d0 <usleep@plt>
jmp    1268 <main+0xb8>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xda1(%rip),%rdi
call   1040 <remove@plt>
lea    -0x88(%rbp),%rdi
call   1080 <sem_mutex_post@plt>
mov    $0x7a120,%edi
call   10d0 <usleep@plt>
jmp    11f1 <main+0x41>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
lea    0xe6a(%rip),%rdi
call   1050 <puts@plt>
mov    $0x37,%edi
call   1030 <creo_clave@plt>
mov    %eax,%edi
call   10a0 <sem_mutex_create@plt>
mov    %eax,0xc(%rsp)
lea    0xe0a(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xc(%rsp),%rdi
call   10d0 <sem_mutex_wait@plt>
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe1a(%rip),%rdi
lea    0xe0b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbp
lea    0xe19(%rip),%rdi
call   1050 <puts@plt>
test   %rbp,%rbp
je     1301 <main+0x141>
lea    0x10(%rsp),%rbx
lea    0xdea(%rip),%r14
lea    0xc(%rsp),%r15
lea    0xdf3(%rip),%r12
lea    0xddd(%rip),%r13
jmp    1287 <main+0xc7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x7a120,%edi
call   10e0 <usleep@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %rbp,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1270 <main+0xb0>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1040 <remove@plt>
mov    %r15,%rdi
call   1090 <sem_mutex_post@plt>
mov    $0x7a120,%edi
call   10e0 <usleep@plt>
lea    0xd3b(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   10d0 <sem_mutex_wait@plt>
lea    0xd38(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
lea    0xd42(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1050 <puts@plt>
test   %rbp,%rbp
jne    1287 <main+0xc7>
lea    0xd28(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Consumimos \0A\00"
@.str.1 = [19 x i8] c"\0A voy a esperar...\00"
@.str.2 = [27 x i8] c"\0A entre en seccion critica\00"
@.str.3 = [6 x i8] c"p.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [19 x i8] c"abriendo archivo \0A\00"
@.str.6 = [12 x i8] c"Error p.txt\00"
@.str.7 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @creo_clave(i32 55)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = call i32 @sem_mutex_create(i32 %8)
store i32 %9, i32* %5
br label %10
10: ; preds = %0, %30
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @sem_mutex_wait(i32* %5)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %14, i32** %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%16 = load i32*, i32** %2
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
ret i32 -1
20: ; preds = %10
br label %21
21: ; preds = %26, %20
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%23 = load i32*, i32** %2
%24 = call i32* @fgets(i8* %22, i32 100, i32* %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %27)
%29 = call i32 @usleep(i32 500000)
br label %21
30: ; preds = %21
%31 = load i32*, i32** %2
%32 = call i32 @fclose(i32* %31)
%33 = call i32 @remove(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @sem_mutex_post(i32* %5)
%35 = call i32 @usleep(i32 500000)
br label %10
}

LLVM IR (-O2):
@.str.1 = [19 x i8] c"\0A voy a esperar...\00"
@.str.2 = [27 x i8] c"\0A entre en seccion critica\00"
@.str.3 = [6 x i8] c"p.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.6 = [12 x i8] c"Error p.txt\00"
@.str.7 = [3 x i8] c"%s\00"
@str = [12 x i8] c"Consumimos \00"
@str.8 = [18 x i8] c"abriendo archivo \00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%5 = tail call i32 @creo_clave(i32 55)
%6 = bitcast i32* %2 to i8*
%7 = tail call i32 @sem_mutex_create(i32 %5)
store i32 %7, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @sem_mutex_wait(i32* %2)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%13 = icmp eq i32* %11, null
br i1 %13, label %18, label %14
14: ; preds = %0, %25
%15 = phi i32* [ %33, %25 ], [ %11, %0 ]
%16 = call i32* @fgets(i8* %4, i32 100, i32* %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %25, label %20
18: ; preds = %25, %0
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
ret i32 -1
20: ; preds = %14, %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%22 = call i32 @usleep(i32 500000)
%23 = call i32* @fgets(i8* %4, i32 100, i32* %15)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %20
25: ; preds = %20, %14
%26 = call i32 @fclose(i32* %15)
%27 = call i32 @remove(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @sem_mutex_post(i32* %2)
%29 = call i32 @usleep(i32 500000)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @sem_mutex_wait(i32* %2)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%35 = icmp eq i32* %33, null
br i1 %35, label %18, label %14
}