#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 */
char ERR;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ cbreak();
int /*<<< orphan*/ error(int);
scalar_t__ getch();
int /*<<< orphan*/ noecho();
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int sem_unlink(char *);
int sem_wait(int /*<<< orphan*/ *);
void *leeTecla(void *pteclaVoid) {
  sem_t *sem = NULL;
  char *ptecla = (char *)pteclaVoid;
  char tecla;
  if ((sem = sem_open("comecocosleetecla", 0)) == SEM_FAILED) {
    error(2);
  } else {
    noecho();
    cbreak();
    while (*ptecla != 0) {
      tecla = (char)getch();
      if (tecla != ERR) {
        if (sem_wait(sem) == -1) {
          error(3);
        } else {
          *ptecla = tecla;
          sem_post(sem);
        }
      }
    }
    if (sem_unlink("comecocosleetecla") == -1)
      error(6);
  }
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<leeTecla>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xe4d(%rip),%rdi
xor    %esi,%esi
call   1060 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <leeTecla+0x4d>
mov    $0x2,%edi
call   1090 <error@plt>
jmp    127c <leeTecla+0xec>
mov    $0x0,%al
call   10a0 <noecho@plt>
mov    $0x0,%al
call   1040 <cbreak@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1258 <leeTecla+0xc8>
mov    $0x0,%al
call   1080 <getch@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
mov    0x2dd0(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1253 <leeTecla+0xc3>
mov    -0x18(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    123c <leeTecla+0xac>
mov    $0x3,%edi
call   1090 <error@plt>
jmp    124e <leeTecla+0xbe>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rdi
call   1050 <sem_post@plt>
jmp    1253 <leeTecla+0xc3>
jmp    11eb <leeTecla+0x5b>
lea    0xda1(%rip),%rdi
call   10b0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    1277 <leeTecla+0xe7>
mov    $0x6,%edi
call   1090 <error@plt>
jmp    127c <leeTecla+0xec>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<leeTecla>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xe5e(%rip),%rdi
xor    %esi,%esi
call   1060 <sem_open@plt>
mov    %rax,%r14
mov    0x2e35(%rip),%rax
cmp    (%rax),%r14
je     1232 <leeTecla+0xa2>
xor    %eax,%eax
call   10a0 <noecho@plt>
xor    %eax,%eax
call   1040 <cbreak@plt>
cmpb   $0x0,(%r15)
je     121a <leeTecla+0x8a>
mov    0x2e0d(%rip),%r12
jmp    11f1 <leeTecla+0x61>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %bl,(%r15)
mov    %r14,%rdi
call   1050 <sem_post@plt>
cmpb   $0x0,(%r15)
je     121a <leeTecla+0x8a>
xor    %eax,%eax
call   1080 <getch@plt>
mov    %rax,%rbx
cmp    %bl,(%r12)
je     11eb <leeTecla+0x5b>
mov    %r14,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    11e0 <leeTecla+0x50>
mov    $0x3,%edi
call   1090 <error@plt>
jmp    11eb <leeTecla+0x5b>
lea    0xddf(%rip),%rdi
call   10b0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    123c <leeTecla+0xac>
mov    $0x6,%edi
jmp    1237 <leeTecla+0xa7>
mov    $0x2,%edi
call   1090 <error@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <pthread_exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"comecocosleetecla\00"
@SEM_FAILED = global i32* null
@ERR = global i8 0
define i8* @leeTecla(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8
store i8* %0, i8** %3
store i32* null, i32** %4
%7 = load i8*, i8** %3
store i8* %7, i8** %5
%8 = call i32* @sem_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 0)
store i32* %8, i32** %4
%9 = load i32*, i32** @SEM_FAILED
%10 = icmp eq i32* %8, %9
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @error(i32 2)
br label %48
13: ; preds = %1
%14 = call i32 (...) @noecho()
%15 = call i32 (...) @cbreak()
br label %16
16: ; preds = %41, %13
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %42
21: ; preds = %16
%22 = call i64 (...) @getch()
%23 = trunc i64 %22 to i8
store i8 %23, i8* %6
%24 = load i8, i8* %6
%25 = sext i8 %24 to i32
%26 = load i8, i8* @ERR
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %25, %27
br i1 %28, label %29, label %41
29: ; preds = %21
%30 = load i32*, i32** %4
%31 = call i32 @sem_wait(i32* %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @error(i32 3)
br label %40
35: ; preds = %29
%36 = load i8, i8* %6
%37 = load i8*, i8** %5
store i8 %36, i8* %37
%38 = load i32*, i32** %4
%39 = call i32 @sem_post(i32* %38)
br label %40
40: ; preds = %35, %33
br label %41
41: ; preds = %40, %21
br label %16
42: ; preds = %16
%43 = call i32 @sem_unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @error(i32 6)
br label %47
47: ; preds = %45, %42
br label %48
48: ; preds = %47, %11
%49 = call i32 @pthread_exit(i32* null)
%50 = load i8*, i8** %2
ret i8* %50
}

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