#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 mutex;
  mutex = semget(IPC_PRIVATE, 1, 0600);
  semop(mutex, &up, 1);
  pid = fork();
  if (pid < 0) {
    perror("Fork error");
    exit(1);
  }
  if (pid == 0) {
    for (i = 0; i < 10; i++) {
      semop(mutex, &down, 1);
      display("Bonjour monde\n");
      semop(mutex, &up, 1);
    }
  } else {
    for (i = 0; i < 10; i++) {
      semop(mutex, &down, 1);
      display("Hello world\n");
      semop(mutex, &up, 1);
    }
    wait(NULL);
    semctl(mutex, 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    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    120f <main+0x8f>
lea    0xe18(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    126c <main+0xec>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1267 <main+0xe7>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
lea    0xde0(%rip),%rdi
call   1040 <display@plt>
mov    -0x34(%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    1221 <main+0xa1>
jmp    12d5 <main+0x155>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    12b9 <main+0x139>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
lea    0xd9d(%rip),%rdi
call   1040 <display@plt>
mov    -0x34(%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    1273 <main+0xf3>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    -0x34(%rbp),%edi
mov    0x2d0c(%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),%rax
mov    (%rax),%edi
mov    $0x1,%esi
mov    $0x180,%edx
call   1030 <semget@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1532 <main+0x3b2>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
test   %rbx,%rbx
je     13a7 <main+0x227>
lea    0xe1e(%rip),%r14
mov    %r14,%rdi
call   1040 <display@plt>
lea    0x10(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rsp,%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
xor    %edi,%edi
call   1090 <wait@plt>
mov    0x2c41(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <semctl@plt>
jmp    1523 <main+0x3a3>
lea    0xc75(%rip),%r14
mov    %r14,%rdi
call   1040 <display@plt>
lea    0x10(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %rsp,%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %r14,%rdi
call   1040 <display@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xadf(%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 = [15 x i8] c"Bonjour monde\0A\00"
@.str.2 = [13 x i8] c"Hello world\0A\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
store i32 0, i32* %1
%7 = bitcast %struct.sembuf* %4 to i8*
%8 = bitcast %struct.sembuf* %5 to i8*
%9 = load i32, i32* @IPC_PRIVATE
%10 = call i32 @semget(i32 %9, i32 1, i32 384)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call i32 @semop(i32 %11, %struct.sembuf* %4, i32 1)
%13 = call i64 (...) @fork()
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i64, i64* %3
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %19
store i32 0, i32* %2
br label %23
23: ; preds = %32, %22
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 10
br i1 %25, label %26, label %35
26: ; preds = %23
%27 = load i32, i32* %6
%28 = call i32 @semop(i32 %27, %struct.sembuf* %5, i32 1)
%29 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = call i32 @semop(i32 %30, %struct.sembuf* %4, i32 1)
br label %32
32: ; preds = %26
%33 = load i32, i32* %2
%34 = add nsw i32 %33, 1
store i32 %34, i32* %2
br label %23
35: ; preds = %23
br label %54
36: ; preds = %19
store i32 0, i32* %2
br label %37
37: ; preds = %46, %36
%38 = load i32, i32* %2
%39 = icmp slt i32 %38, 10
br i1 %39, label %40, label %49
40: ; preds = %37
%41 = load i32, i32* %6
%42 = call i32 @semop(i32 %41, %struct.sembuf* %5, i32 1)
%43 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %6
%45 = call i32 @semop(i32 %44, %struct.sembuf* %4, i32 1)
br label %46
46: ; preds = %40
%47 = load i32, i32* %2
%48 = add nsw i32 %47, 1
store i32 %48, i32* %2
br label %37
49: ; preds = %37
%50 = call i32 @wait(i32* null)
%51 = load i32, i32* %6
%52 = load i32, i32* @IPC_RMID
%53 = call i32 @semctl(i32 %51, i32 0, i32 %52)
br label %54
54: ; preds = %49, %35
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 = [15 x i8] c"Bonjour monde\0A\00"
@.str.2 = [13 x i8] c"Hello world\0A\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 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = icmp eq i64 %8, 0
%15 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
br i1 %14, label %49, label %16
16: ; preds = %13
%17 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%19 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%20 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%22 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%23 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%25 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%26 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%28 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%29 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%31 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%32 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%34 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%35 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%37 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%38 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%40 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%41 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%43 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%44 = call i32 @display(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%46 = call i32 @wait(i32* null)
%47 = load i32, i32* @IPC_RMID
%48 = call i32 @semctl(i32 %6, i32 0, i32 %47)
br label %79
49: ; preds = %13
%50 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%52 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%53 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%55 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%56 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%57 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%58 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%59 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%60 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%61 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%62 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%63 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%64 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%65 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%66 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%67 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%68 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%69 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%70 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%71 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%72 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%73 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%74 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%75 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
%76 = call i32 @semop(i32 %6, %struct.sembuf* %2, i32 1)
%77 = call i32 @display(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%78 = call i32 @semop(i32 %6, %struct.sembuf* %1, i32 1)
br label %79
79: ; preds = %49, %16
ret i32 0
}