#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;
  scalar_t__ sem_flg;
  scalar_t__ sem_num;
};

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int, int, int);
int /*<<< orphan*/ semop(int, struct sembuf *, int);
int main() {
  int id = semget(0x9999, 1, IPC_CREAT | IPC_EXCL | 0600);
  if (id == -1) {
    perror("semget");
    return 1;
  }
  semctl(id, 0, SETVAL, 5);
  int res = 10;
  for (; res;) {
    printf("访问资源%d\n", res);
    struct sembuf op;
    op.sem_num = 0;
    op.sem_op = -1;
    op.sem_flg = 0;
    semop(id, &op, 1);
    printf("请求访问资源%d成功\n", res);
    res--;
  }
  semctl(id, 0, IPC_RMID, NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
mov    0x2e81(%rip),%rax
or     (%rax),%edx
or     $0x180,%edx
mov    $0x9999,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ab <main+0x5b>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    123d <main+0xed>
mov    -0x8(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x5,%ecx
call   1060 <semctl@plt>
movl   $0xa,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1228 <main+0xd8>
mov    -0xc(%rbp),%esi
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    -0xc(%rbp),%esi
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    11ca <main+0x7a>
mov    -0x8(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1060 <semctl@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    0x2e84(%rip),%rax
or     (%rax),%edx
or     $0x180,%edx
mov    $0x1,%r14d
mov    $0x9999,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     141f <main+0x2cf>
mov    %eax,%ebp
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %ebp,%edi
xor    %esi,%esi
mov    $0x5,%ecx
call   1060 <semctl@plt>
lea    0xe62(%rip),%r15
mov    %r15,%rdi
mov    $0xa,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
lea    0x8(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
lea    0xe3a(%rip),%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    0x2bd6(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <semctl@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbea(%rip),%rdi
call   1050 <perror@plt>
jmp    140f <main+0x2bf>

LLVM IR (-O0):
%struct.sembuf = type { i32, i64, i64 }
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str = [7 x i8] c"semget\00"
@SETVAL = global i32 0
@.str.1 = [16 x i8] c"\E8\AE\BF\E9\97\AE\E8\B5\84\E6\BA\90%d\0A\00"
@.str.2 = [28 x i8] c"\E8\AF\B7\E6\B1\82\E8\AE\BF\E9\97\AE\E8\B5\84\E6\BA\90%d\E6\88\90\E5\8A\9F\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sembuf
store i32 0, i32* %1
%5 = load i32, i32* @IPC_CREAT
%6 = load i32, i32* @IPC_EXCL
%7 = or i32 %5, %6
%8 = or i32 %7, 384
%9 = call i32 @semget(i32 39321, i32 1, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %37
14: ; preds = %0
%15 = load i32, i32* %2
%16 = load i32, i32* @SETVAL
%17 = call i32 @semctl(i32 %15, i32 0, i32 %16, i32 5)
store i32 10, i32* %3
br label %18
18: ; preds = %21, %14
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i32, i32* %3
%23 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 2
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 0
store i32 -1, i32* %25
%26 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 1
store i64 0, i64* %26
%27 = load i32, i32* %2
%28 = call i32 @semop(i32 %27, %struct.sembuf* %4, i32 1)
%29 = load i32, i32* %3
%30 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %3
%32 = add nsw i32 %31, -1
store i32 %32, i32* %3
br label %18
33: ; preds = %18
%34 = load i32, i32* %2
%35 = load i32, i32* @IPC_RMID
%36 = call i32 @semctl(i32 %34, i32 0, i32 %35, i32 0)
br label %37
37: ; preds = %33, %12
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str = [7 x i8] c"semget\00"
@SETVAL = global i32 0
@.str.1 = [16 x i8] c"\E8\AE\BF\E9\97\AE\E8\B5\84\E6\BA\90%d\0A\00"
@.str.2 = [28 x i8] c"\E8\AF\B7\E6\B1\82\E8\AE\BF\E9\97\AE\E8\B5\84\E6\BA\90%d\E6\88\90\E5\8A\9F\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca %struct.sembuf
%2 = load i32, i32* @IPC_CREAT
%3 = load i32, i32* @IPC_EXCL
%4 = or i32 %2, %3
%5 = or i32 %4, 384
%6 = tail call i32 @semget(i32 39321, i32 1, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %49
10: ; preds = %0
%11 = load i32, i32* @SETVAL
%12 = tail call i32 @semctl(i32 %6, i32 0, i32 %11, i32 5)
%13 = bitcast %struct.sembuf* %1 to i8*
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 2
%15 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 0
%16 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 1
%17 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 10)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%18 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%19 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 10)
%20 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 9)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%21 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%22 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 9)
%23 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 8)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%24 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%25 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 8)
%26 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 7)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%27 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%28 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 7)
%29 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 6)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%30 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%31 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 6)
%32 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 5)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%33 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%34 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 5)
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 4)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%36 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%37 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 4)
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 3)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%39 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%40 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 3)
%41 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 2)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%42 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%43 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 2)
%44 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i64 0, i64* %14
store i32 -1, i32* %15
store i64 0, i64* %16
%45 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%46 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 1)
%47 = load i32, i32* @IPC_RMID
%48 = call i32 @semctl(i32 %6, i32 0, i32 %47, i32 0)
br label %49
49: ; preds = %10, %8
%50 = phi i32 [ 1, %8 ], [ 0, %10 ]
ret i32 %50
}