#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;
scalar_t__ fork();
int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_unlink(char *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int shm_open(char *, int, int);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char const *argv[]) {
  sem_unlink("/semaph");
  int fd = shm_open("/archivo", O_CREAT | O_RDWR, 0666);
  int *ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  *ptr = 0;
  if (fork() == 0) {
    system("./smless");
    return 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);
  }
  wait(NULL);
  sem_close(sem);
  printf("Final: %d\n", *ptr);
  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)
lea    0xe33(%rip),%rdi
call   10c0 <sem_unlink@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
lea    0xe1d(%rip),%rdi
mov    $0x1b6,%edx
call   10a0 <shm_open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    0x2db8(%rip),%rax
or     (%rax),%edx
mov    0x2dcf(%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    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    125f <main+0xaf>
lea    0xdc3(%rip),%rdi
call   1050 <system@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f3 <main+0x143>
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    0xd91(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x1e8480,-0x2c(%rbp)
jge    12c8 <main+0x118>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1070 <sem_post@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1290 <main+0xe0>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
mov    -0x28(%rbp),%rdi
call   1090 <sem_close@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xd33(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe45(%rip),%rdi
call   10c0 <sem_unlink@plt>
mov    0x2e31(%rip),%rbx
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
lea    0xe2f(%rip),%rdi
mov    $0x1b6,%edx
call   10a0 <shm_open@plt>
mov    0x2e06(%rip),%rcx
mov    0x2dcf(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2de4(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
movl   $0x0,(%rax)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1289 <main+0xd9>
mov    (%rbx),%esi
lea    0xdd2(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,%rbx
mov    $0x1e8480,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
addl   $0x1,(%r14)
mov    %rbx,%rdi
call   1070 <sem_post@plt>
add    $0xffffffff,%ebp
jne    1250 <main+0xa0>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    %rbx,%rdi
call   1090 <sem_close@plt>
mov    (%r14),%esi
lea    0xd98(%rip),%rdi
call   1060 <printf@plt>
jmp    1295 <main+0xe5>
lea    0xd81(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/semaph\00"
@.str.1 = [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.2 = [9 x i8] c"./smless\00"
@.str.3 = [11 x i8] c"Final: %d\0A\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 = call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %11, %12
%14 = call i32 @shm_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 438)
store i32 %14, i32* %6
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* @MAP_SHARED
%19 = load i32, i32* %6
%20 = call i32* @mmap(i32* null, i32 4, i32 %17, i32 %18, i32 %19, i32 0)
store i32* %20, i32** %7
%21 = load i32*, i32** %7
store i32 0, i32* %21
%22 = call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %50
26: ; preds = %2
%27 = load i32, i32* @O_CREAT
%28 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %27, i32 438, i32 1)
store i32* %28, i32** %8
store i32 0, i32* %9
store i32 0, i32* %9
br label %29
29: ; preds = %40, %26
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, 2000000
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = load i32*, i32** %8
%34 = call i32 @sem_wait(i32* %33)
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = add nsw i32 %36, 1
store i32 %37, i32* %35
%38 = load i32*, i32** %8
%39 = call i32 @sem_post(i32* %38)
br label %40
40: ; preds = %32
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %29
43: ; preds = %29
%44 = call i32 @wait(i32* null)
%45 = load i32*, i32** %8
%46 = call i32 @sem_close(i32* %45)
%47 = load i32*, i32** %7
%48 = load i32, i32* %47
%49 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %48)
store i32 0, i32* %3
br label %50
50: ; preds = %43, %24
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [8 x i8] c"/semaph\00"
@.str.1 = [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.2 = [9 x i8] c"./smless\00"
@.str.3 = [11 x i8] c"Final: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%7 = tail call i32 @shm_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 438)
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i32* @mmap(i32* null, i32 4, i32 %10, i32 %11, i32 %7, i32 0)
store i32 0, i32* %12
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %33
17: ; preds = %2
%18 = load i32, i32* @O_CREAT
%19 = tail call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %18, i32 438, i32 1)
br label %20
20: ; preds = %17, %20
%21 = phi i32 [ 0, %17 ], [ %26, %20 ]
%22 = tail call i32 @sem_wait(i32* %19)
%23 = load i32, i32* %12
%24 = add nsw i32 %23, 1
store i32 %24, i32* %12
%25 = tail call i32 @sem_post(i32* %19)
%26 = add nuw nsw i32 %21, 1
%27 = icmp eq i32 %26, 2000000
br i1 %27, label %28, label %20
28: ; preds = %20
%29 = tail call i32 @wait(i32* null)
%30 = tail call i32 @sem_close(i32* %19)
%31 = load i32, i32* %12
%32 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %28, %15
ret i32 0
}