#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 */

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ SETVAL;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sem_signal(int, int);
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int semget(int, int, int);
int *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int);
int main(int argc, char **argv) {
  int applicantsCount = atoi(argv[1]);
  int semaphoreHr;
  semaphoreHr = semget(67623, 1, 0700 | IPC_CREAT);
  semctl(semaphoreHr, 0, SETVAL, 0);
  int statusId =
      shmget(123321, (sizeof(int) * applicantsCount), 0700 | IPC_CREAT);
  int *status;
  status = shmat(statusId, 0, 0);
  int f;
  int index;
  for (index = 0; index < applicantsCount; index++) {
    f = fork();
    if (f == -1) {
      printf("FORK ERROR\n");
      exit(1);
    }
    if (f == 0)
      break;
  }
  sem_signal(semaphoreHr, 1);
  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    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
or     $0x1c0,%edx
mov    $0x10827,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1060 <semctl@plt>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
or     $0x1c0,%edx
mov    $0x1e1b9,%edi
call   1080 <shmget@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmat@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1281 <main+0xf1>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    125f <main+0xcf>
lea    0xdb0(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    126e <main+0xde>
jmp    1281 <main+0xf1>
jmp    1273 <main+0xe3>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1229 <main+0x99>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <sem_signal@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    0x2e37(%rip),%r15
mov    $0x1c0,%ebp
mov    (%r15),%edx
or     %ebp,%edx
mov    $0x10827,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%r14d
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <semctl@plt>
lea    0x0(,%rbx,4),%esi
or     (%r15),%ebp
mov    $0x1e1b9,%edi
mov    %ebp,%edx
call   1080 <shmget@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
test   %ebx,%ebx
jle    1215 <main+0x85>
nop
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1215 <main+0x85>
cmp    $0xffffffff,%eax
je     122f <main+0x9f>
add    $0xffffffff,%ebx
jne    1200 <main+0x70>
mov    %r14d,%edi
mov    $0x1,%esi
call   1050 <sem_signal@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdca(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@SETVAL = global i32 0
@.str = [12 x i8] c"FORK ERROR\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*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %6
%16 = load i32, i32* @IPC_CREAT
%17 = or i32 448, %16
%18 = call i32 @semget(i32 67623, i32 1, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* @SETVAL
%21 = call i32 @semctl(i32 %19, i32 0, i32 %20, i32 0)
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = mul i64 4, %23
%25 = trunc i64 %24 to i32
%26 = load i32, i32* @IPC_CREAT
%27 = or i32 448, %26
%28 = call i32 @shmget(i32 123321, i32 %25, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = call i32* @shmat(i32 %29, i32 0, i32 0)
store i32* %30, i32** %9
store i32 0, i32* %11
br label %31
31: ; preds = %47, %2
%32 = load i32, i32* %11
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %50
35: ; preds = %31
%36 = call i32 (...) @fork()
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = load i32, i32* %10
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
br label %50
46: ; preds = %42
br label %47
47: ; preds = %46
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %31
50: ; preds = %45, %31
%51 = load i32, i32* %7
%52 = call i32 @sem_signal(i32 %51, i32 1)
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@SETVAL = global i32 0
@str = [11 x i8] c"FORK ERROR\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 @atoi(i8* %4)
%6 = load i32, i32* @IPC_CREAT
%7 = or i32 %6, 448
%8 = tail call i32 @semget(i32 67623, i32 1, i32 %7)
%9 = load i32, i32* @SETVAL
%10 = tail call i32 @semctl(i32 %8, i32 0, i32 %9, i32 0)
%11 = shl i32 %5, 2
%12 = load i32, i32* @IPC_CREAT
%13 = or i32 %12, 448
%14 = tail call i32 @shmget(i32 123321, i32 %11, i32 %13)
%15 = tail call i32* @shmat(i32 %14, i32 0, i32 0)
%16 = icmp sgt i32 %5, 0
br i1 %16, label %17, label %26
17: ; preds = %2, %23
%18 = phi i32 [ %24, %23 ], [ 0, %2 ]
%19 = tail call i32 (...) @fork()
switch i32 %19, label %23 [
i32 -1, label %20
i32 0, label %26
]
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = add nuw nsw i32 %18, 1
%25 = icmp eq i32 %24, %5
br i1 %25, label %26, label %17
26: ; preds = %23, %17, %2
%27 = tail call i32 @sem_signal(i32 %8, i32 1)
ret i32 0
}