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

/* Variables and functions */
int /*<<< orphan*/ IPC_CREAT;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEM_UNDO;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ftok(char *, int);
int open(char *, int, int);
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int semget(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ semop(int, struct sembuf *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  int fd_product;
  int sd_product;
  key_t key_product;
  struct sembuf sembuf_product;
  fd_product = open("./product.txt", O_RDWR | O_CREAT, 0777);
  key_product = ftok("/arm", 1);
  sd_product = semget(key_product, 1, IPC_CREAT);
  semctl(sd_product, 0, SETVAL, 0);
  sleep(10);
  write(fd_product, "The Product Finished!", 21);
  sembuf_product.sem_num = 0;
  sembuf_product.sem_op = 1;
  sembuf_product.sem_flg = SEM_UNDO;
  semop(sd_product, &sembuf_product, 1);
  close(fd_product);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
lea    0xe5f(%rip),%rdi
mov    $0x1ff,%edx
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
lea    0xe59(%rip),%rdi
mov    $0x1,%esi
call   1090 <ftok@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1070 <semctl@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    0xe0f(%rip),%rsi
mov    $0x15,%edx
call   1040 <write@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e4b(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe61(%rip),%rdi
mov    $0x1ff,%edx
call   1060 <open@plt>
mov    %eax,%ebx
lea    0xe5c(%rip),%rdi
mov    $0x1,%esi
call   1090 <ftok@plt>
mov    0x2e05(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebp
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1070 <semctl@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
lea    0xe1b(%rip),%rsi
mov    %ebx,%edi
mov    $0x15,%edx
call   1040 <write@plt>
movq   $0x0,0x10(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@.str = [14 x i8] c"./product.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"/arm\00"
@IPC_CREAT = global i32 0
@SETVAL = global i32 0
@.str.2 = [22 x i8] c"The Product Finished!\00"
@SEM_UNDO = global i32 0
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sembuf
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7, i32 511)
store i32 %8, i32* %1
%9 = call i32 @ftok(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = load i32, i32* @IPC_CREAT
%12 = call i32 @semget(i32 %10, i32 1, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = load i32, i32* @SETVAL
%15 = call i32 @semctl(i32 %13, i32 0, i32 %14, i32 0)
%16 = call i32 @sleep(i32 10)
%17 = load i32, i32* %1
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 21)
%19 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 2
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 0
store i32 1, i32* %20
%21 = load i32, i32* @SEM_UNDO
%22 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %2
%24 = call i32 @semop(i32 %23, %struct.sembuf* %4, i32 1)
%25 = load i32, i32* %1
%26 = call i32 @close(i32 %25)
ret void
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@.str = [14 x i8] c"./product.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"/arm\00"
@IPC_CREAT = global i32 0
@SETVAL = global i32 0
@.str.2 = [22 x i8] c"The Product Finished!\00"
@SEM_UNDO = global i32 0
define void @main() {
%1 = alloca %struct.sembuf
%2 = bitcast %struct.sembuf* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5, i32 511)
%7 = tail call i32 @ftok(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 1)
%8 = load i32, i32* @IPC_CREAT
%9 = tail call i32 @semget(i32 %7, i32 1, i32 %8)
%10 = load i32, i32* @SETVAL
%11 = tail call i32 @semctl(i32 %9, i32 0, i32 %10, i32 0)
%12 = tail call i32 @sleep(i32 10)
%13 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 21)
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 2
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 0
store i32 1, i32* %15
%16 = load i32, i32* @SEM_UNDO
%17 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @semop(i32 %9, %struct.sembuf* %1, i32 1)
%19 = call i32 @close(i32 %6)
ret void
}