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

/* Variables and functions */
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ exit(int);
scalar_t__ getpid();
int /*<<< orphan*/ ipcend();
scalar_t__ ipcinit(int, int *);
scalar_t__ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ sempost(int);
int /*<<< orphan*/ srand48(long);
int /*<<< orphan*/ startbarber();
int /*<<< orphan*/ startcustomer(int);
scalar_t__ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int i, numCustomers, numChairs;
  pid_t pid[25], bpid;
  int initvalues[4];
  printf("\nEnter no. of Customers : ");
  scanf("%d", &numCustomers);
  printf("\nEnter no. of waiting chairs : ");
  scanf("%d", &numChairs);
  if (numCustomers > 25) {
    printf("The maximum number of Customers allowed is %d.\n", 25);
    exit(-1);
  }
  srand48((long)getpid());
  initvalues[0] = numChairs;
  initvalues[1] = 1;
  initvalues[2] = 0;
  initvalues[3] = 0;
  if (ipcinit(4, initvalues)) {
    exit(1);
  }
  bpid = startbarber();
  for (i = 0; i < numCustomers; i++) {
    printf("starting customers\n");
    pid[i] = startcustomer(i);
  }
  for (i = 0; i < numCustomers; i++) {
    if (waitpid(pid[i], NULL, NULL) < 0) {
      perror("waitpid");
    }
  }
  sempost(2);
  if (kill(bpid, SIGUSR1) < 0) {
    perror("kill");
  }
  if (waitpid(bpid, NULL, NULL) < 0) {
    perror("waitpid");
  }
  ipcend();
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe1d(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   10c0 <scanf@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdff(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   10c0 <scanf@plt>
cmpl   $0x19,-0x18(%rbp)
jle    124c <main+0x7c>
lea    0xe08(%rip),%rdi
mov    $0x19,%esi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rdi
call   10b0 <srand48@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xa0(%rbp)
movl   $0x1,-0x9c(%rbp)
movl   $0x0,-0x98(%rbp)
movl   $0x0,-0x94(%rbp)
lea    -0xa0(%rbp),%rsi
mov    $0x4,%edi
call   1070 <ipcinit@plt>
cmp    $0x0,%rax
je     12a7 <main+0xd7>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1050 <startbarber@plt>
mov    %eax,-0x84(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12f5 <main+0x125>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <startcustomer@plt>
mov    %eax,%ecx
movslq -0x14(%rbp),%rax
mov    %ecx,-0x80(%rbp,%rax,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12bb <main+0xeb>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1345 <main+0x175>
movslq -0x14(%rbp),%rax
mov    -0x80(%rbp,%rax,4),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <waitpid@plt>
cmp    $0x0,%rax
jge    1332 <main+0x162>
lea    0xd55(%rip),%rdi
call   10e0 <perror@plt>
jmp    1337 <main+0x167>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12fc <main+0x12c>
mov    $0x2,%edi
call   1090 <sempost@plt>
mov    -0x84(%rbp),%edi
mov    0x2c8c(%rip),%rax
mov    (%rax),%esi
call   10a0 <kill@plt>
cmp    $0x0,%rax
jge    1379 <main+0x1a9>
lea    0xd16(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x84(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <waitpid@plt>
cmp    $0x0,%rax
jge    13a1 <main+0x1d1>
lea    0xce6(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x0,%al
call   1080 <ipcend@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
lea    0xe0e(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe1b(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10d0 <scanf@plt>
lea    0xe0a(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   10d0 <scanf@plt>
cmpl   $0x1a,0x8(%rsp)
jl     124c <main+0x6c>
lea    0xe08(%rip),%rdi
mov    $0x19,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rdi
call   10c0 <srand48@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x10(%rsp)
movq   $0x1,0x14(%rsp)
movl   $0x0,0x1c(%rsp)
lea    0x10(%rsp),%rsi
mov    $0x4,%edi
call   1080 <ipcinit@plt>
test   %rax,%rax
jne    130a <main+0x12a>
xor    %eax,%eax
call   1060 <startbarber@plt>
mov    %eax,%r14d
cmpl   $0x0,0x8(%rsp)
jle    1314 <main+0x134>
xor    %ebx,%ebx
lea    0xdd5(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <startcustomer@plt>
mov    %eax,0x20(%rsp,%rbx,4)
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
cmp    %rax,%rbx
jl     12b0 <main+0xd0>
test   %eax,%eax
jle    1314 <main+0x134>
xor    %ebp,%ebp
lea    0xd90(%rip),%rbx
jmp    12ee <main+0x10e>
add    $0x1,%rbp
movslq 0x8(%rsp),%rax
cmp    %rax,%rbp
jge    1314 <main+0x134>
mov    0x20(%rsp,%rbp,4),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <waitpid@plt>
test   %rax,%rax
jns    12e0 <main+0x100>
mov    %rbx,%rdi
call   10f0 <perror@plt>
jmp    12e0 <main+0x100>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    $0x2,%edi
call   10a0 <sempost@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   10b0 <kill@plt>
test   %rax,%rax
jns    1340 <main+0x160>
lea    0xd3b(%rip),%rdi
call   10f0 <perror@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <waitpid@plt>
test   %rax,%rax
jns    135d <main+0x17d>
lea    0xd16(%rip),%rdi
call   10f0 <perror@plt>
xor    %eax,%eax
call   1090 <ipcend@plt>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"\0AEnter no. of Customers : \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [32 x i8] c"\0AEnter no. of waiting chairs : \00"
@.str.3 = [48 x i8] c"The maximum number of Customers allowed is %d.\0A\00"
@.str.4 = [20 x i8] c"starting customers\0A\00"
@.str.5 = [8 x i8] c"waitpid\00"
@SIGUSR1 = global i32 0
@.str.6 = [5 x i8] c"kill\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 [25 x i32]
%10 = alloca i32
%11 = alloca [4 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
%16 = load i32, i32* %7
%17 = icmp sgt i32 %16, 25
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 25)
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = call i64 (...) @getpid()
%23 = call i32 @srand48(i64 %22)
%24 = load i32, i32* %8
%25 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 0
store i32 %24, i32* %25
%26 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 1
store i32 1, i32* %26
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 2
store i32 0, i32* %27
%28 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 3
store i32 0, i32* %28
%29 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 0
%30 = call i64 @ipcinit(i32 4, i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = call i32 (...) @startbarber()
store i32 %35, i32* %10
store i32 0, i32* %6
br label %36
36: ; preds = %47, %34
%37 = load i32, i32* %6
%38 = load i32, i32* %7
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %50
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32, i32* %6
%43 = call i32 @startcustomer(i32 %42)
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [25 x i32], [25 x i32]* %9, i64 0, i64 %45
store i32 %43, i32* %46
br label %47
47: ; preds = %40
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %36
50: ; preds = %36
store i32 0, i32* %6
br label %51
51: ; preds = %65, %50
%52 = load i32, i32* %6
%53 = load i32, i32* %7
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %68
55: ; preds = %51
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [25 x i32], [25 x i32]* %9, i64 0, i64 %57
%59 = load i32, i32* %58
%60 = call i64 @waitpid(i32 %59, i32* null, i32* null)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %55
%63 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %64
64: ; preds = %62, %55
br label %65
65: ; preds = %64
%66 = load i32, i32* %6
%67 = add nsw i32 %66, 1
store i32 %67, i32* %6
br label %51
68: ; preds = %51
%69 = call i32 @sempost(i32 2)
%70 = load i32, i32* %10
%71 = load i32, i32* @SIGUSR1
%72 = call i64 @kill(i32 %70, i32 %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %68
%75 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %76
76: ; preds = %74, %68
%77 = load i32, i32* %10
%78 = call i64 @waitpid(i32 %77, i32* null, i32* null)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %76
%81 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %82
82: ; preds = %80, %76
%83 = call i32 (...) @ipcend()
%84 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [27 x i8] c"\0AEnter no. of Customers : \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [32 x i8] c"\0AEnter no. of waiting chairs : \00"
@.str.3 = [48 x i8] c"The maximum number of Customers allowed is %d.\0A\00"
@.str.5 = [8 x i8] c"waitpid\00"
@SIGUSR1 = global i32 0
@.str.6 = [5 x i8] c"kill\00"
@str = [19 x i8] c"starting customers\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [25 x i32]
%6 = alloca [4 x i32]
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast [25 x i32]* %5 to i8*
%10 = bitcast [4 x i32]* %6 to i8*
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 25
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 25)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = call i64 (...) @getpid()
%22 = call i32 @srand48(i64 %21)
%23 = load i32, i32* %4
%24 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
store i32 %23, i32* %24
%25 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 1
store i32 1, i32* %25
%26 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 2
store i32 0, i32* %26
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 3
store i32 0, i32* %27
%28 = call i64 @ipcinit(i32 4, i32* %24)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %20
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20
%33 = call i32 (...) @startbarber()
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 0
br i1 %35, label %38, label %61
36: ; preds = %38
%37 = icmp sgt i32 %45, 0
br i1 %37, label %48, label %61
38: ; preds = %32, %38
%39 = phi i64 [ %44, %38 ], [ 0, %32 ]
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%41 = trunc i64 %39 to i32
%42 = call i32 @startcustomer(i32 %41)
%43 = getelementptr inbounds [25 x i32], [25 x i32]* %5, i64 0, i64 %39
store i32 %42, i32* %43
%44 = add nuw nsw i64 %39, 1
%45 = load i32, i32* %3
%46 = sext i32 %45 to i64
%47 = icmp slt i64 %44, %46
br i1 %47, label %38, label %36
48: ; preds = %36, %56
%49 = phi i64 [ %57, %56 ], [ 0, %36 ]
%50 = getelementptr inbounds [25 x i32], [25 x i32]* %5, i64 0, i64 %49
%51 = load i32, i32* %50
%52 = call i64 @waitpid(i32 %51, i32* null, i32* null)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %48, %54
%57 = add nuw nsw i64 %49, 1
%58 = load i32, i32* %3
%59 = sext i32 %58 to i64
%60 = icmp slt i64 %57, %59
br i1 %60, label %48, label %61
61: ; preds = %56, %32, %36
%62 = call i32 @sempost(i32 2)
%63 = load i32, i32* @SIGUSR1
%64 = call i64 @kill(i32 %33, i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %66, %61
%69 = call i64 @waitpid(i32 %33, i32* null, i32* null)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %73
73: ; preds = %71, %68
%74 = call i32 (...) @ipcend()
%75 = call i32 @exit(i32 0)
unreachable
}