#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 */
struct sembuf {
  int sem_op;
  int sem_num;
  scalar_t__ sem_flg;
};
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ ftok(char *, unsigned char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ semctl(int, int, int /*<<< orphan*/, int);
int semget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ semop(int, struct sembuf *, int);
int main(int argc, char *argv[]) {
  int semid, pid, j = 10;
  struct sembuf operacion;
  key_t llave;
  llave = ftok(argv[0], 'U');
  if ((semid = semget(llave, 2, IPC_CREAT | 0600)) == -1) {
    perror("Error al ejecutar semget");
    exit(-1);
  }
  semctl(semid, 0, SETVAL, 0);
  semctl(semid, 1, SETVAL, 1);
  if ((pid = fork()) == -1) {
    perror("Error al ejecutar fork");
    exit(-1);
  } else if (pid == 0) {
    while (j) {
      operacion.sem_flg = 0;
      operacion.sem_op = -1;
      operacion.sem_num = 0;
      semop(semid, &operacion, 1);
      printf("SOY EL PROCESO HIJO. IMPRESION: %d\n", j--);
      operacion.sem_op = 1;
      operacion.sem_num = 1;
      semop(semid, &operacion, 1);
    }
    semctl(semid, 0, IPC_RMID, 0);
  } else {
    while (j) {
      operacion.sem_flg = 0;
      operacion.sem_op = -1;
      operacion.sem_num = 1;
      semop(semid, &operacion, 1);
      printf("SOY EL PROCESO PADRE. IMPRESION: %d\n", j--);
      operacion.sem_op = 1;
      operacion.sem_num = 0;
      semop(semid, &operacion, 1);
    }
    semctl(semid, 0, IPC_RMID, 0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xa,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x55,%esi
call   1090 <ftok@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x2,%esi
call   1030 <semget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11ef <main+0x6f>
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1060 <semctl@plt>
mov    -0x14(%rbp),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ecx
mov    %ecx,%esi
call   1060 <semctl@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1245 <main+0xc5>
lea    0xde3(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    12da <main+0x15a>
jmp    1254 <main+0xd4>
cmpl   $0x0,-0x1c(%rbp)
je     12c0 <main+0x140>
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    -0x1c(%rbp),%esi
mov    %esi,%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
lea    0xd99(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
jmp    1254 <main+0xd4>
mov    -0x14(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1060 <semctl@plt>
jmp    1360 <main+0x1e0>
jmp    12df <main+0x15f>
cmpl   $0x0,-0x1c(%rbp)
je     134b <main+0x1cb>
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    -0x1c(%rbp),%esi
mov    %esi,%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
lea    0xd32(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
jmp    12df <main+0x15f>
mov    -0x14(%rbp),%edi
mov    0x2c8b(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1060 <semctl@plt>
jmp    1365 <main+0x1e5>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    (%rsi),%rdi
mov    $0x55,%esi
call   1090 <ftok@plt>
mov    0x2e36(%rip),%rcx
mov    $0x180,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x2,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     12f5 <main+0x175>
mov    %eax,%ebx
mov    0x2e09(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %eax,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <semctl@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
mov    $0x1,%esi
mov    $0x1,%ecx
call   1060 <semctl@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1266 <main+0xe6>
cmp    $0xffffffff,%eax
je     12fe <main+0x17e>
mov    $0xa,%ebp
movabs $0x1ffffffff,%r12
lea    0x8(%rsp),%r14
lea    0xe3c(%rip),%r15
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x10(%rsp)
mov    %r12,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
movq   $0x1,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
add    $0xffffffff,%ebp
jne    1220 <main+0xa0>
jmp    12d0 <main+0x150>
mov    $0xa,%ebp
mov    $0xffffffff,%r12d
lea    0x8(%rsp),%r14
lea    0xdb3(%rip),%r15
movabs $0x100000001,%r13
nopw   0x0(%rax,%rax,1)
movq   $0x0,0x10(%rsp)
mov    %r12,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %r13,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
add    $0xffffffff,%ebp
jne    1290 <main+0x110>
mov    0x2d09(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <semctl@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd04(%rip),%rdi
jmp    1305 <main+0x185>
lea    0xd14(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@IPC_CREAT = global i32 0
@.str = [25 x i8] c"Error al ejecutar semget\00"
@SETVAL = global i32 0
@.str.1 = [23 x i8] c"Error al ejecutar fork\00"
@.str.2 = [36 x i8] c"SOY EL PROCESO HIJO. IMPRESION: %d\0A\00"
@IPC_RMID = global i32 0
@.str.3 = [37 x i8] c"SOY EL PROCESO PADRE. IMPRESION: %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 %struct.sembuf
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 10, i32* %8
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @ftok(i8* %13, i8 zeroext 85)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = load i32, i32* @IPC_CREAT
%17 = or i32 %16, 384
%18 = call i32 @semget(i32 %15, i32 2, i32 %17)
store i32 %18, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %2
%24 = load i32, i32* %6
%25 = load i32, i32* @SETVAL
%26 = call i32 @semctl(i32 %24, i32 0, i32 %25, i32 0)
%27 = load i32, i32* %6
%28 = load i32, i32* @SETVAL
%29 = call i32 @semctl(i32 %27, i32 1, i32 %28, i32 1)
%30 = call i32 (...) @fork()
store i32 %30, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %23
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %59
38: ; preds = %35
br label %39
39: ; preds = %42, %38
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 2
store i64 0, i64* %43
%44 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 -1, i32* %44
%45 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i32 0, i32* %45
%46 = load i32, i32* %6
%47 = call i32 @semop(i32 %46, %struct.sembuf* %9, i32 1)
%48 = load i32, i32* %8
%49 = add nsw i32 %48, -1
store i32 %49, i32* %8
%50 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %48)
%51 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 1, i32* %51
%52 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i32 1, i32* %52
%53 = load i32, i32* %6
%54 = call i32 @semop(i32 %53, %struct.sembuf* %9, i32 1)
br label %39
55: ; preds = %39
%56 = load i32, i32* %6
%57 = load i32, i32* @IPC_RMID
%58 = call i32 @semctl(i32 %56, i32 0, i32 %57, i32 0)
br label %80
59: ; preds = %35
br label %60
60: ; preds = %63, %59
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %76
63: ; preds = %60
%64 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 2
store i64 0, i64* %64
%65 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 -1, i32* %65
%66 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i32 1, i32* %66
%67 = load i32, i32* %6
%68 = call i32 @semop(i32 %67, %struct.sembuf* %9, i32 1)
%69 = load i32, i32* %8
%70 = add nsw i32 %69, -1
store i32 %70, i32* %8
%71 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %69)
%72 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 1, i32* %72
%73 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i32 0, i32* %73
%74 = load i32, i32* %6
%75 = call i32 @semop(i32 %74, %struct.sembuf* %9, i32 1)
br label %60
76: ; preds = %60
%77 = load i32, i32* %6
%78 = load i32, i32* @IPC_RMID
%79 = call i32 @semctl(i32 %77, i32 0, i32 %78, i32 0)
br label %80
80: ; preds = %76, %55
br label %81
81: ; preds = %80
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@IPC_CREAT = global i32 0
@.str = [25 x i8] c"Error al ejecutar semget\00"
@SETVAL = global i32 0
@.str.1 = [23 x i8] c"Error al ejecutar fork\00"
@.str.2 = [36 x i8] c"SOY EL PROCESO HIJO. IMPRESION: %d\0A\00"
@IPC_RMID = global i32 0
@.str.3 = [37 x i8] c"SOY EL PROCESO PADRE. IMPRESION: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sembuf
%4 = bitcast %struct.sembuf* %3 to i8*
%5 = load i8*, i8** %1
%6 = tail call i32 @ftok(i8* %5, i8 zeroext 85)
%7 = load i32, i32* @IPC_CREAT
%8 = or i32 %7, 384
%9 = tail call i32 @semget(i32 %6, i32 2, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i32, i32* @SETVAL
%16 = tail call i32 @semctl(i32 %9, i32 0, i32 %15, i32 0)
%17 = load i32, i32* @SETVAL
%18 = tail call i32 @semctl(i32 %9, i32 1, i32 %17, i32 1)
%19 = tail call i32 (...) @fork()
switch i32 %19, label %24 [
i32 -1, label %28
i32 0, label %20
]
20: ; preds = %14
%21 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
%22 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
%23 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
br label %31
24: ; preds = %14
%25 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
%26 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
%27 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
br label %38
28: ; preds = %14
%29 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 -1)
unreachable
31: ; preds = %20, %31
%32 = phi i32 [ 10, %20 ], [ %34, %31 ]
store i64 0, i64* %21
store i32 -1, i32* %22
store i32 0, i32* %23
%33 = call i32 @semop(i32 %9, %struct.sembuf* %3, i32 1)
%34 = add nsw i32 %32, -1
%35 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %32)
store i32 1, i32* %22
store i32 1, i32* %23
%36 = call i32 @semop(i32 %9, %struct.sembuf* %3, i32 1)
%37 = icmp eq i32 %34, 0
br i1 %37, label %45, label %31
38: ; preds = %24, %38
%39 = phi i32 [ 10, %24 ], [ %41, %38 ]
store i64 0, i64* %25
store i32 -1, i32* %26
store i32 1, i32* %27
%40 = call i32 @semop(i32 %9, %struct.sembuf* %3, i32 1)
%41 = add nsw i32 %39, -1
%42 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %39)
store i32 1, i32* %26
store i32 0, i32* %27
%43 = call i32 @semop(i32 %9, %struct.sembuf* %3, i32 1)
%44 = icmp eq i32 %41, 0
br i1 %44, label %45, label %38
45: ; preds = %31, %38
%46 = load i32, i32* @IPC_RMID
%47 = call i32 @semctl(i32 %9, i32 0, i32 %46, i32 0)
ret i32 0
}