#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
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*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int main() {
  char cadena[100];
  FILE *productor;
  int i;
  int partida = 0;
  key_t key = creo_clave(55);
  int semaforoid = sem_mutex_create(key);
  sem_mutex_init(&semaforoid, 1);
  while (1) {
    printf("\n voy a esperar...");
    sem_mutex_wait(&semaforoid);
    productor = fopen("p.txt", "w");
    if (productor == NULL) {
      perror("Error p.txt");
      return -1;
    }
    for (i = 0; i <= 20; i++) {
      sprintf(cadena, "producto -%02d-%02d\n", partida, i);
      printf("%s", cadena);
      fputs(cadena, productor);
      sleep(1);
      partida++;
    }
    fclose(productor);
    sem_mutex_post(&semaforoid);
    sleep(1);
  };
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x80(%rbp)
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    -0x88(%rbp),%rdi
mov    $0x1,%esi
call   1040 <sem_mutex_init@plt>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x88(%rbp),%rdi
call   10c0 <sem_mutex_wait@plt>
lea    0xdef(%rip),%rdi
lea    0xde0(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    1261 <main+0xa1>
lea    0xdc7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbp
ret
movl   $0x0,-0x7c(%rbp)
cmpl   $0x14,-0x7c(%rbp)
jg     12c8 <main+0x108>
lea    -0x70(%rbp),%rdi
mov    -0x80(%rbp),%edx
mov    -0x7c(%rbp),%ecx
lea    0xd9e(%rip),%rsi
call   10d0 <sprintf@plt>
lea    -0x70(%rbp),%rsi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rsi
call   1070 <fputs@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    1268 <main+0xa8>
mov    -0x78(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x88(%rbp),%rdi
call   1080 <sem_mutex_post@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
jmp    120b <main+0x4b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    $0x37,%edi
call   1030 <creo_clave@plt>
mov    %eax,%edi
call   1090 <sem_mutex_create@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <sem_mutex_init@plt>
lea    0xe04(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10c0 <sem_mutex_wait@plt>
lea    0xe09(%rip),%rdi
lea    0xdfa(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12e2 <main+0x122>
mov    %rax,%r13
lea    0xdf0(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xdf9(%rip),%r15
xor    %r12d,%r12d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r12,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%ecx
call   10d0 <sprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <fputs@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
add    $0x1,%ebp
cmp    $0x15,%ebp
jne    1250 <main+0x90>
mov    %r13,%rdi
call   1050 <fclose@plt>
lea    0xc(%rsp),%r13
mov    %r13,%rdi
call   1080 <sem_mutex_post@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
lea    0xd4f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
call   10c0 <sem_mutex_wait@plt>
lea    0xd54(%rip),%rdi
lea    0xd45(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r13
add    %ebp,%r12d
test   %rax,%rax
jne    1240 <main+0x80>
lea    0xd2c(%rip),%rdi
call   10b0 <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 = [19 x i8] c"\0A voy a esperar...\00"
@.str.1 = [6 x i8] c"p.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"Error p.txt\00"
@.str.4 = [21 x i8] c"producto -%02d-%02d\0A\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%8 = call i32 @creo_clave(i32 55)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = call i32 @sem_mutex_create(i32 %9)
store i32 %10, i32* %7
%11 = call i32 @sem_mutex_init(i32* %7, i32 1)
br label %12
12: ; preds = %0, %40
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @sem_mutex_wait(i32* %7)
%15 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %3
%16 = load i32*, i32** %3
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
ret i32 -1
20: ; preds = %12
store i32 0, i32* %4
br label %21
21: ; preds = %37, %20
%22 = load i32, i32* %4
%23 = icmp sle i32 %22, 20
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%26 = load i32, i32* %5
%27 = load i32, i32* %4
%28 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %27)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%32 = load i32*, i32** %3
%33 = call i32 @fputs(i8* %31, i32* %32)
%34 = call i32 @sleep(i32 1)
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %37
37: ; preds = %24
%38 = load i32, i32* %4
%39 = add nsw i32 %38, 1
store i32 %39, i32* %4
br label %21
40: ; preds = %21
%41 = load i32*, i32** %3
%42 = call i32 @fclose(i32* %41)
%43 = call i32 @sem_mutex_post(i32* %7)
%44 = call i32 @sleep(i32 1)
br label %12
}

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