#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 /*<<< orphan*/ sem_flg;
  int /*<<< orphan*/ sem_num;
};

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ SEM_UNDO;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int rand();
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int, int, int);
scalar_t__ semop(int, struct sembuf *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int);
scalar_t__ wait(int /*<<< orphan*/);
int main() {
  int id = semget(0x9999, 1, 0600 | IPC_CREAT);
  int i;
  for (i = 0; i < 6; i++) {
    if (fork() == 0) {
      struct sembuf ops[1] = {
          {.sem_num = 0, .sem_op = -1, .sem_flg = SEM_UNDO}};
      if (semop(id, ops, 1) == 0) {
        printf("%d进入房间\n", getpid());
        srand(getpid());
        int n = rand() % 6 + 5;
        sleep(n);
        printf("%d退出房间\n", getpid());
        ops[0].sem_op = 1;
        semop(id, ops, 1);
      }
      exit(0);
    }
  }
  puts("房间开放,准入4人:");
  semctl(id, 0, SETVAL, 4);
  while (wait(0) > 0)
    ;
  semctl(id, 0, IPC_RMID, NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x9999,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x6,-0xc(%rbp)
jge    12ce <main+0x10e>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    12bb <main+0xfb>
movl   $0xffffffff,-0x18(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
cmp    $0x0,%rax
jne    12b4 <main+0xf4>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xda9(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    $0x6,%ecx
cltd
idiv   %ecx
add    $0x5,%edx
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10b0 <sleep@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xd79(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12c0 <main+0x100>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11f7 <main+0x37>
lea    0xd4b(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
mov    0x2ce4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x4,%ecx
call   1080 <semctl@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
cmp    $0x0,%rax
jle    1308 <main+0x148>
jmp    12f2 <main+0x132>
mov    -0x8(%rbp),%edi
mov    0x2cc6(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1080 <semctl@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e04(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0x9999,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1288 <main+0xc8>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1288 <main+0xc8>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1288 <main+0xc8>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1288 <main+0xc8>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1288 <main+0xc8>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1288 <main+0xc8>
lea    0xde5(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x4,%ecx
call   1080 <semctl@plt>
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
call   10c0 <wait@plt>
test   %rax,%rax
jg     1260 <main+0xa0>
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <semctl@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
movl   $0xffffffff,(%rsp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1090 <semop@plt>
test   %rax,%rax
jne    1335 <main+0x175>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd3a(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
call   1070 <srand@plt>
xor    %eax,%eax
call   10e0 <rand@plt>
cltq
imul   $0x2aaaaaab,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,2),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x5,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xcf8(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
movl   $0x1,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1090 <semop@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@IPC_CREAT = global i32 0
@SEM_UNDO = global i32 0
@.str = [16 x i8] c"%d\E8\BF\9B\E5\85\A5\E6\88\BF\E9\97\B4\0A\00"
@.str.1 = [16 x i8] c"%d\E9\80\80\E5\87\BA\E6\88\BF\E9\97\B4\0A\00"
@.str.2 = [25 x i8] c"\E6\88\BF\E9\97\B4\E5\BC\80\E6\94\BE,\E5\87\86\E5\85\A54\E4\BA\BA:\00"
@SETVAL = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1 x %struct.sembuf]
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @IPC_CREAT
%7 = or i32 384, %6
%8 = call i32 @semget(i32 39321, i32 1, i32 %7)
store i32 %8, i32* %2
store i32 0, i32* %3
br label %9
9: ; preds = %45, %0
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 6
br i1 %11, label %12, label %48
12: ; preds = %9
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %12
%16 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%17 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %16, i32 0, i32 0
store i32 -1, i32* %17
%18 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %16, i32 0, i32 1
%19 = load i32, i32* @SEM_UNDO
store i32 %19, i32* %18
%20 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %16, i32 0, i32 2
store i32 0, i32* %20
%21 = load i32, i32* %2
%22 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%23 = call i64 @semop(i32 %21, %struct.sembuf* %22, i32 1)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %42
25: ; preds = %15
%26 = call i32 (...) @getpid()
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = call i32 (...) @getpid()
%29 = call i32 @srand(i32 %28)
%30 = call i32 (...) @rand()
%31 = srem i32 %30, 6
%32 = add nsw i32 %31, 5
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = call i32 @sleep(i32 %33)
%35 = call i32 (...) @getpid()
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%38 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %37, i32 0, i32 0
store i32 1, i32* %38
%39 = load i32, i32* %2
%40 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%41 = call i64 @semop(i32 %39, %struct.sembuf* %40, i32 1)
br label %42
42: ; preds = %25, %15
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %12
br label %45
45: ; preds = %44
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 1
store i32 %47, i32* %3
br label %9
48: ; preds = %9
%49 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %2
%51 = load i32, i32* @SETVAL
%52 = call i32 @semctl(i32 %50, i32 0, i32 %51, i32 4)
br label %53
53: ; preds = %56, %48
%54 = call i64 @wait(i32 0)
%55 = icmp sgt i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
br label %53
57: ; preds = %53
%58 = load i32, i32* %2
%59 = load i32, i32* @IPC_RMID
%60 = call i32 @semctl(i32 %58, i32 0, i32 %59, i32 0)
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@IPC_CREAT = global i32 0
@SEM_UNDO = global i32 0
@.str = [16 x i8] c"%d\E8\BF\9B\E5\85\A5\E6\88\BF\E9\97\B4\0A\00"
@.str.1 = [16 x i8] c"%d\E9\80\80\E5\87\BA\E6\88\BF\E9\97\B4\0A\00"
@.str.2 = [25 x i8] c"\E6\88\BF\E9\97\B4\E5\BC\80\E6\94\BE,\E5\87\86\E5\85\A54\E4\BA\BA:\00"
@SETVAL = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca [1 x %struct.sembuf]
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 384
%4 = tail call i32 @semget(i32 39321, i32 1, i32 %3)
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %26, label %7
7: ; preds = %0
%8 = tail call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %7
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %10
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %13
%17 = tail call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %16
%20 = tail call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @SETVAL
%25 = tail call i32 @semctl(i32 %4, i32 0, i32 %24, i32 4)
br label %49
26: ; preds = %19, %16, %13, %10, %7, %0
%27 = bitcast [1 x %struct.sembuf]* %1 to i8*
%28 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0
%29 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0, i32 0
store i32 -1, i32* %29
%30 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0, i32 1
%31 = load i32, i32* @SEM_UNDO
store i32 %31, i32* %30
%32 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %1, i64 0, i64 0, i32 2
store i32 0, i32* %32
%33 = call i64 @semop(i32 %4, %struct.sembuf* %28, i32 1)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %26
%36 = call i32 (...) @getpid()
%37 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = call i32 (...) @getpid()
%39 = call i32 @srand(i32 %38)
%40 = call i32 (...) @rand()
%41 = srem i32 %40, 6
%42 = add nsw i32 %41, 5
%43 = call i32 @sleep(i32 %42)
%44 = call i32 (...) @getpid()
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %44)
store i32 1, i32* %29
%46 = call i64 @semop(i32 %4, %struct.sembuf* %28, i32 1)
br label %47
47: ; preds = %35, %26
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %49, %22
%50 = tail call i64 @wait(i32 0)
%51 = icmp sgt i64 %50, 0
br i1 %51, label %49, label %52
52: ; preds = %49
%53 = load i32, i32* @IPC_RMID
%54 = tail call i32 @semctl(i32 %4, i32 0, i32 %53, i32 0)
ret i32 0
}