#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*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int shm_open(char *, int, int);
int main(int argc, char const *argv[]) {
  int fd = shm_open("/archivo", O_CREAT | O_RDWR, 0666);
  int *ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  sem_t *sem = sem_open("/semaph", O_CREAT, 0666, 1);
  int i = 0;
  for (i = 0; i < 2000000; i++) {
    sem_wait(sem);
    (*ptr)--;
    sem_post(sem);
  }
  sem_close(sem);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
lea    0xe71(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    0x2e14(%rip),%rax
or     (%rax),%edx
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1060 <sem_open@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x1e8480,-0x2c(%rbp)
jge    1239 <main+0xd9>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <sem_post@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1201 <main+0xa1>
mov    -0x28(%rbp),%rdi
call   1070 <sem_close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e8d(%rip),%rbx
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
lea    0xe83(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
mov    0x2e62(%rip),%rcx
mov    0x2e2b(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e40(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    (%rbx),%esi
lea    0xe49(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1060 <sem_open@plt>
mov    %rax,%rbx
mov    $0x1e8480,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
addl   $0xffffffff,(%r14)
mov    %rbx,%rdi
call   1050 <sem_post@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0x80>
mov    %rbx,%rdi
call   1070 <sem_close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/archivo\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [8 x i8] c"/semaph\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = call i32 @shm_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12, i32 438)
store i32 %13, i32* %6
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_SHARED
%18 = load i32, i32* %6
%19 = call i32* @mmap(i32* null, i32 4, i32 %16, i32 %17, i32 %18, i32 0)
store i32* %19, i32** %7
%20 = load i32, i32* @O_CREAT
%21 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 438, i32 1)
store i32* %21, i32** %8
store i32 0, i32* %9
store i32 0, i32* %9
br label %22
22: ; preds = %33, %2
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 2000000
br i1 %24, label %25, label %36
25: ; preds = %22
%26 = load i32*, i32** %8
%27 = call i32 @sem_wait(i32* %26)
%28 = load i32*, i32** %7
%29 = load i32, i32* %28
%30 = add nsw i32 %29, -1
store i32 %30, i32* %28
%31 = load i32*, i32** %8
%32 = call i32 @sem_post(i32* %31)
br label %33
33: ; preds = %25
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %22
36: ; preds = %22
%37 = load i32*, i32** %8
%38 = call i32 @sem_close(i32* %37)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/archivo\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [8 x i8] c"/semaph\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = tail call i32 @shm_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438)
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %8, %7
%10 = load i32, i32* @MAP_SHARED
%11 = tail call i32* @mmap(i32* null, i32 4, i32 %9, i32 %10, i32 %6, i32 0)
%12 = load i32, i32* @O_CREAT
%13 = tail call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 438, i32 1)
br label %14
14: ; preds = %2, %14
%15 = phi i32 [ 0, %2 ], [ %20, %14 ]
%16 = tail call i32 @sem_wait(i32* %13)
%17 = load i32, i32* %11
%18 = add nsw i32 %17, -1
store i32 %18, i32* %11
%19 = tail call i32 @sem_post(i32* %13)
%20 = add nuw nsw i32 %15, 1
%21 = icmp eq i32 %20, 2000000
br i1 %21, label %22, label %14
22: ; preds = %14
%23 = tail call i32 @sem_close(i32* %13)
ret i32 0
}