#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*/ sem_t;

/* Variables and functions */
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ error(int);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int sem_unlink(char *);
int sem_wait(int /*<<< orphan*/ *);
char rwTecla(char *tecla, int escribir, char nuevoValor) {
  char aux = 0;
  sem_t *sem;
  if ((sem = sem_open("comecocosleetecla", O_CREAT, 0600, 1)) == SEM_FAILED) {
    error(4);
  } else {
    if (sem_wait(sem) == -1) {
      error(5);
    } else {
      if (escribir == true) {
        *tecla = nuevoValor;
      }
      aux = *tecla;
      sem_post(sem);
    }
    if (sem_unlink("comecocosleetecla") == -1)
      error(7);
  }
  return aux;
}


x86 assembly (clang, -O0):
<rwTecla>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
movb   $0x0,-0xe(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
mov    $0x180,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <rwTecla+0x5a>
mov    $0x4,%edi
call   1060 <error@plt>
jmp    1214 <rwTecla+0xc4>
mov    -0x18(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    11cb <rwTecla+0x7b>
mov    $0x5,%edi
call   1060 <error@plt>
jmp    11f0 <rwTecla+0xa0>
cmpl   $0x1,-0xc(%rbp)
jne    11de <rwTecla+0x8e>
mov    -0xd(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    %al,-0xe(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <sem_post@plt>
lea    0xe09(%rip),%rdi
call   1070 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    120f <rwTecla+0xbf>
mov    $0x7,%edi
call   1060 <error@plt>
jmp    1214 <rwTecla+0xc4>
movsbl -0xe(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rwTecla>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
mov    $0x180,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e58(%rip),%rax
cmp    (%rax),%rbx
je     11a5 <rwTecla+0x55>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     11ae <rwTecla+0x5e>
cmp    $0x1,%r15d
jne    11bc <rwTecla+0x6c>
mov    %bpl,(%r14)
jmp    11bf <rwTecla+0x6f>
mov    $0x4,%edi
xor    %ebp,%ebp
jmp    11dd <rwTecla+0x8d>
mov    $0x5,%edi
call   1060 <error@plt>
xor    %ebp,%ebp
jmp    11c7 <rwTecla+0x77>
mov    (%r14),%bpl
mov    %rbx,%rdi
call   1040 <sem_post@plt>
lea    0xe32(%rip),%rdi
call   1070 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    11e2 <rwTecla+0x92>
mov    $0x7,%edi
call   1060 <error@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"comecocosleetecla\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
define signext i8 @rwTecla(i8* %0, i32 %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8 %2, i8* %6
store i8 0, i8* %7
%9 = load i32, i32* @O_CREAT
%10 = call i32* @sem_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %9, i32 384, i32 1)
store i32* %10, i32** %8
%11 = load i32*, i32** @SEM_FAILED
%12 = icmp eq i32* %10, %11
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @error(i32 4)
br label %38
15: ; preds = %3
%16 = load i32*, i32** %8
%17 = call i32 @sem_wait(i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @error(i32 5)
br label %32
21: ; preds = %15
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i8, i8* %6
%26 = load i8*, i8** %4
store i8 %25, i8* %26
br label %27
27: ; preds = %24, %21
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
store i8 %29, i8* %7
%30 = load i32*, i32** %8
%31 = call i32 @sem_post(i32* %30)
br label %32
32: ; preds = %27, %19
%33 = call i32 @sem_unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @error(i32 7)
br label %37
37: ; preds = %35, %32
br label %38
38: ; preds = %37, %13
%39 = load i8, i8* %7
ret i8 %39
}

LLVM IR (-O2):
@.str = [18 x i8] c"comecocosleetecla\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
define signext i8 @rwTecla(i8* nocapture %0, i32 %1, i8 signext %2) {
%4 = load i32, i32* @O_CREAT
%5 = tail call i32* @sem_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4, i32 384, i32 1)
%6 = load i32*, i32** @SEM_FAILED
%7 = icmp eq i32* %5, %6
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = tail call i32 @sem_wait(i32* %5)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @error(i32 5)
br label %21
13: ; preds = %8
%14 = icmp eq i32 %1, 1
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = load i8, i8* %0
br label %18
17: ; preds = %13
store i8 %2, i8* %0
br label %18
18: ; preds = %15, %17
%19 = phi i8 [ %16, %15 ], [ %2, %17 ]
%20 = tail call i32 @sem_post(i32* %5)
br label %21
21: ; preds = %18, %11
%22 = phi i8 [ 0, %11 ], [ %19, %18 ]
%23 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %21, %3
%26 = phi i32 [ 4, %3 ], [ 7, %21 ]
%27 = phi i8 [ 0, %3 ], [ %22, %21 ]
%28 = tail call i32 @error(i32 %26)
br label %29
29: ; preds = %25, %21
%30 = phi i8 [ %22, %21 ], [ %27, %25 ]
ret i8 %30
}