#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 uint64_t;

/* Variables and functions */
void *create_sem(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sem_down(void *);
char *shm_open(int);
int stoi(char *);
void main_test_1_ipc(int argc, char **argv) {
  uint64_t key = stoi(argv[0]);
  void *sem = create_sem(key, 0);
  if (sem == ((void *)0)) {
    printf("No se pudo crear el semaforo\n");
    return;
  }
  char *shm = shm_open(key);
  printf("Semaforo %d bloqueado\n", key);
  sem_down(sem);
  printf("Semaforo %d desbloqueado, mensaje leido de shm %d: %s\n", key, key,
         shm);
}


x86 assembly (clang, -O0):
<main_test_1_ipc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <stoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <create_sem@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119a <main_test_1_ipc+0x4a>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11d8 <main_test_1_ipc+0x88>
mov    -0x14(%rbp),%edi
call   1070 <shm_open@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <sem_down@plt>
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main_test_1_ipc>:
push   %r15
push   %r14
push   %rbx
mov    (%rsi),%rdi
call   1070 <stoi@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <create_sem@plt>
test   %rax,%rax
je     11bc <main_test_1_ipc+0x5c>
mov    %rax,%r14
mov    %ebx,%edi
call   1080 <shm_open@plt>
mov    %rax,%r15
lea    0xe6f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <sem_down@plt>
lea    0xe6e(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
mov    %r15,%rcx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <printf@plt>
lea    0xe8b(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <puts@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"No se pudo crear el semaforo\0A\00"
@.str.1 = [23 x i8] c"Semaforo %d bloqueado\0A\00"
@.str.2 = [55 x i8] c"Semaforo %d desbloqueado, mensaje leido de shm %d: %s\0A\00"
define void @main_test_1_ipc(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i8**, i8*** %4
%9 = getelementptr inbounds i8*, i8** %8, i64 0
%10 = load i8*, i8** %9
%11 = call i32 @stoi(i8* %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = call i8* @create_sem(i32 %12, i32 0)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %29
18: ; preds = %2
%19 = load i32, i32* %5
%20 = call i8* @shm_open(i32 %19)
store i8* %20, i8** %7
%21 = load i32, i32* %5
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %6
%24 = call i32 @sem_down(i8* %23)
%25 = load i32, i32* %5
%26 = load i32, i32* %5
%27 = load i8*, i8** %7
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26, i8* %27)
br label %29
29: ; preds = %18, %16
ret void
}

LLVM IR (-O2):
@.str.1 = [23 x i8] c"Semaforo %d bloqueado\0A\00"
@.str.2 = [55 x i8] c"Semaforo %d desbloqueado, mensaje leido de shm %d: %s\0A\00"
@str = [29 x i8] c"No se pudo crear el semaforo\00"
define void @main_test_1_ipc(i32 %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i32 @stoi(i8* %3)
%5 = tail call i8* @create_sem(i32 %4, i32 0)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %14
9: ; preds = %2
%10 = tail call i8* @shm_open(i32 %4)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%12 = tail call i32 @sem_down(i8* %5)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %4, i8* %10)
br label %14
14: ; preds = %9, %7
ret void
}