#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 member_1;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_0;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ display(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int semget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ semop(int, struct sembuf *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int i;
  pid_t pid;
  struct sembuf up = {0, 1, 0};
  struct sembuf down = {0, -1, 0};
  int event_ab;
  int event_cd;
  event_ab = semget(IPC_PRIVATE, 1, 0600);
  event_cd = semget(IPC_PRIVATE, 1, 0600);
  pid = fork();
  if (pid < 0) {
    perror("Fork error");
    exit(1);
  }
  if (pid == 0) {
    for (i = 0; i < 10; i++) {
      semop(event_ab, &up, 1);
      semop(event_cd, &down, 1);
      display("cd\n");
    }
  } else {
    for (i = 0; i < 10; i++) {
      semop(event_ab, &down, 1);
      display("ab");
      semop(event_cd, &up, 1);
    }
    wait(NULL);
    semctl(event_ab, 0, IPC_RMID);
    semctl(event_cd, 0, IPC_RMID);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe68(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xe62(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe60(%rip),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
mov    $0x180,%edx
call   1030 <semget@plt>
mov    %eax,-0x34(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
mov    $0x180,%edx
call   1030 <semget@plt>
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1219 <main+0x99>
lea    0xe0e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1276 <main+0xf6>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1271 <main+0xf1>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
lea    0xdc5(%rip),%rdi
call   1040 <display@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    122b <main+0xab>
jmp    12f2 <main+0x172>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    12c3 <main+0x143>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
lea    0xd88(%rip),%rdi
call   1040 <display@plt>
mov    -0x38(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    127d <main+0xfd>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    -0x34(%rbp),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <semctl@plt>
mov    -0x38(%rbp),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <semctl@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0xe76(%rip),%eax
mov    %eax,0x18(%rsp)
mov    0xe63(%rip),%rax
mov    %rax,0x10(%rsp)
mov    0xe6c(%rip),%eax
mov    %eax,0x8(%rsp)
mov    0xe59(%rip),%rax
mov    %rax,(%rsp)
mov    0x2e2a(%rip),%rbx
mov    (%rbx),%edi
mov    $0x1,%esi
mov    $0x180,%edx
call   1030 <semget@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
mov    $0x1,%esi
mov    $0x180,%edx
call   1030 <semget@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     154a <main+0x3ca>
je     13b0 <main+0x230>
mov    %rsp,%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
lea    0xe14(%rip),%r15
mov    %r15,%rdi
call   1040 <display@plt>
lea    0x10(%rsp),%r12
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r15,%rdi
call   1040 <display@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
xor    %edi,%edi
call   1090 <wait@plt>
mov    0x2c45(%rip),%r14
mov    (%r14),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <semctl@plt>
mov    (%r14),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <semctl@plt>
jmp    153b <main+0x3bb>
lea    0x10(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rsp,%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
lea    0xc46(%rip),%r12
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r12,%rdi
call   1040 <display@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xac7(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@__const.main.up = %struct.sembuf { i32 0, i32 1, i32 0 }
@__const.main.down = %struct.sembuf { i32 0, i32 -1, i32 0 }
@IPC_PRIVATE = global i32 0
@.str = [11 x i8] c"Fork error\00"
@.str.1 = [4 x i8] c"cd\0A\00"
@.str.2 = [3 x i8] c"ab\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.sembuf
%5 = alloca %struct.sembuf
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast %struct.sembuf* %4 to i8*
%9 = bitcast %struct.sembuf* %5 to i8*
%10 = load i32, i32* @IPC_PRIVATE
%11 = call i32 @semget(i32 %10, i32 1, i32 384)
store i32 %11, i32* %6
%12 = load i32, i32* @IPC_PRIVATE
%13 = call i32 @semget(i32 %12, i32 1, i32 384)
store i32 %13, i32* %7
%14 = call i64 (...) @fork()
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = load i64, i64* %3
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %20
store i32 0, i32* %2
br label %24
24: ; preds = %33, %23
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 10
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = load i32, i32* %6
%29 = call i32 @semop(i32 %28, %struct.sembuf* %4, i32 1)
%30 = load i32, i32* %7
%31 = call i32 @semop(i32 %30, %struct.sembuf* %5, i32 1)
%32 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %27
%34 = load i32, i32* %2
%35 = add nsw i32 %34, 1
store i32 %35, i32* %2
br label %24
36: ; preds = %24
br label %58
37: ; preds = %20
store i32 0, i32* %2
br label %38
38: ; preds = %47, %37
%39 = load i32, i32* %2
%40 = icmp slt i32 %39, 10
br i1 %40, label %41, label %50
41: ; preds = %38
%42 = load i32, i32* %6
%43 = call i32 @semop(i32 %42, %struct.sembuf* %5, i32 1)
%44 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %7
%46 = call i32 @semop(i32 %45, %struct.sembuf* %4, i32 1)
br label %47
47: ; preds = %41
%48 = load i32, i32* %2
%49 = add nsw i32 %48, 1
store i32 %49, i32* %2
br label %38
50: ; preds = %38
%51 = call i32 @wait(i32* null)
%52 = load i32, i32* %6
%53 = load i32, i32* @IPC_RMID
%54 = call i32 @semctl(i32 %52, i32 0, i32 %53)
%55 = load i32, i32* %7
%56 = load i32, i32* @IPC_RMID
%57 = call i32 @semctl(i32 %55, i32 0, i32 %56)
br label %58
58: ; preds = %50, %36
ret i32 0
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@__const.main.up = %struct.sembuf { i32 0, i32 1, i32 0 }
@__const.main.down = %struct.sembuf { i32 0, i32 -1, i32 0 }
@IPC_PRIVATE = global i32 0
@.str = [11 x i8] c"Fork error\00"
@.str.1 = [4 x i8] c"cd\0A\00"
@.str.2 = [3 x i8] c"ab\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca %struct.sembuf
%2 = alloca %struct.sembuf
%3 = bitcast %struct.sembuf* %1 to i8*
%4 = bitcast %struct.sembuf* %2 to i8*
%5 = load i32, i32* @IPC_PRIVATE
%6 = tail call i32 @semget(i32 %5, i32 1, i32 384)
%7 = load i32, i32* @IPC_PRIVATE
%8 = tail call i32 @semget(i32 %7, i32 1, i32 384)
%9 = tail call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = icmp eq i64 %9, 0
br i1 %15, label %52, label %16
16: ; preds = %14
%17 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%18 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%20 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%21 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%23 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%24 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%26 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%27 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%29 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%30 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%32 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%33 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%35 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%36 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%38 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%39 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%41 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%42 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%44 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%45 = call i32 @display(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @semop(i32 %8, %struct.sembuf* %1, i32 1)
%47 = call i32 @wait(i32* null)
%48 = load i32, i32* @IPC_RMID
%49 = call i32 @semctl(i32 %6, i32 0, i32 %48)
%50 = load i32, i32* @IPC_RMID
%51 = call i32 @semctl(i32 %8, i32 0, i32 %50)
br label %83
52: ; preds = %14
%53 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%54 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%55 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%56 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%57 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%58 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%59 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%60 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%61 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%62 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%63 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%64 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%65 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%66 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%67 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%68 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%69 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%70 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%71 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%72 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%73 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%74 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%75 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%76 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%77 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%78 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%79 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%80 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%81 = call i32 @semop(i32 %8, %struct.sembuf* %2, i32 1)
%82 = call i32 @display(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %83
83: ; preds = %52, %16
ret i32 0
}