#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_num;
};
typedef int key_t;

/* Variables and functions */
int /*<<< orphan*/ GETVAL;
int /*<<< orphan*/ IPC_CREAT;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ close(int);
int ftok(char *, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int semget(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ semop(int, struct sembuf *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  int val_sem;
  int fd_sem;
  int semnum_sem;
  key_t key_sem;
  struct sembuf buf_sem;
  fd_sem = open("./sem.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
  printf("fd_sem val is %d\n", fd_sem);
  key_sem = ftok("/arm/key_sem", 1);
  printf("key_sem val is %d\n", key_sem);
  semnum_sem = semget(key_sem, 1, IPC_CREAT);
  printf("semnum_sem val is %d\n", semnum_sem);
  val_sem = semctl(semnum_sem, 0, GETVAL);
  printf("a val is %d\n", val_sem);
  semctl(semnum_sem, 0, SETVAL, 1);
  val_sem = semctl(semnum_sem, 0, GETVAL);
  printf("a val is %d\n", val_sem);
  buf_sem.sem_num = 0;
  buf_sem.sem_op = -1;
  semop(semnum_sem, &buf_sem, 1);
  write(fd_sem, "aaaaa", 5);
  sleep(10);
  write(fd_sem, "bbbbb", 5);
  buf_sem.sem_num = 0;
  buf_sem.sem_op = 1;
  semop(semnum_sem, &buf_sem, 1);
  close(fd_sem);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
lea    0xe46(%rip),%rdi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
lea    0xe3f(%rip),%rdi
mov    $0x1,%esi
call   10a0 <ftok@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe20(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x0,%al
call   1080 <semctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x1,%ecx
mov    $0x0,%al
call   1080 <semctl@plt>
mov    -0xc(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x0,%al
call   1080 <semctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xdce(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
mov    -0x8(%rbp),%edi
lea    0xdac(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    0xd94(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e3f(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
lea    0xe44(%rip),%rdi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe3b(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe3f(%rip),%rdi
mov    $0x1,%esi
call   10a0 <ftok@plt>
mov    %eax,%ebp
lea    0xe39(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebp
lea    0xe27(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x2dad(%rip),%r15
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <semctl@plt>
lea    0xe1a(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
mov    $0x1,%ecx
xor    %eax,%eax
call   1080 <semctl@plt>
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <semctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movq   $0x0,0x10(%rsp)
movl   $0xffffffff,0x8(%rsp)
ff
lea    0x8(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
lea    0xdc0(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
lea    0xda9(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
movq   $0x0,0x10(%rsp)
movl   $0x1,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i64 }
@.str = [10 x i8] c"./sem.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@.str.1 = [18 x i8] c"fd_sem val is %d\0A\00"
@.str.2 = [13 x i8] c"/arm/key_sem\00"
@.str.3 = [19 x i8] c"key_sem val is %d\0A\00"
@IPC_CREAT = global i32 0
@.str.4 = [22 x i8] c"semnum_sem val is %d\0A\00"
@GETVAL = global i32 0
@.str.5 = [13 x i8] c"a val is %d\0A\00"
@SETVAL = global i32 0
@.str.6 = [6 x i8] c"aaaaa\00"
@.str.7 = [6 x i8] c"bbbbb\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sembuf
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10, i32 511)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @ftok(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 1)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %4
%18 = load i32, i32* @IPC_CREAT
%19 = call i32 @semget(i32 %17, i32 1, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %3
%23 = load i32, i32* @GETVAL
%24 = call i32 (i32, i32, i32, ...) @semctl(i32 %22, i32 0, i32 %23)
store i32 %24, i32* %1
%25 = load i32, i32* %1
%26 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %3
%28 = load i32, i32* @SETVAL
%29 = call i32 (i32, i32, i32, ...) @semctl(i32 %27, i32 0, i32 %28, i32 1)
%30 = load i32, i32* %3
%31 = load i32, i32* @GETVAL
%32 = call i32 (i32, i32, i32, ...) @semctl(i32 %30, i32 0, i32 %31)
store i32 %32, i32* %1
%33 = load i32, i32* %1
%34 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 1
store i64 0, i64* %35
%36 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 0
store i32 -1, i32* %36
%37 = load i32, i32* %3
%38 = call i32 @semop(i32 %37, %struct.sembuf* %5, i32 1)
%39 = load i32, i32* %2
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
%41 = call i32 @sleep(i32 10)
%42 = load i32, i32* %2
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
%44 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 1
store i64 0, i64* %44
%45 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 0
store i32 1, i32* %45
%46 = load i32, i32* %3
%47 = call i32 @semop(i32 %46, %struct.sembuf* %5, i32 1)
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
ret void
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i64 }
@.str = [10 x i8] c"./sem.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@.str.1 = [18 x i8] c"fd_sem val is %d\0A\00"
@.str.2 = [13 x i8] c"/arm/key_sem\00"
@.str.3 = [19 x i8] c"key_sem val is %d\0A\00"
@IPC_CREAT = global i32 0
@.str.4 = [22 x i8] c"semnum_sem val is %d\0A\00"
@GETVAL = global i32 0
@.str.5 = [13 x i8] c"a val is %d\0A\00"
@SETVAL = global i32 0
@.str.6 = [6 x i8] c"aaaaa\00"
@.str.7 = [6 x i8] c"bbbbb\00"
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_APPEND
%5 = or i32 %4, %3
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 511)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = tail call i32 @ftok(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 1)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @IPC_CREAT
%13 = tail call i32 @semget(i32 %10, i32 1, i32 %12)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @GETVAL
%16 = tail call i32 (i32, i32, i32, ...) @semctl(i32 %13, i32 0, i32 %15)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @SETVAL
%19 = tail call i32 (i32, i32, i32, ...) @semctl(i32 %13, i32 0, i32 %18, i32 1)
%20 = load i32, i32* @GETVAL
%21 = tail call i32 (i32, i32, i32, ...) @semctl(i32 %13, i32 0, i32 %20)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 0
store i32 -1, i32* %24
%25 = call i32 @semop(i32 %13, %struct.sembuf* %1, i32 1)
%26 = call i32 @write(i32 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
%27 = call i32 @sleep(i32 10)
%28 = call i32 @write(i32 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
store i64 0, i64* %23
store i32 1, i32* %24
%29 = call i32 @semop(i32 %13, %struct.sembuf* %1, i32 1)
%30 = call i32 @close(i32 %8)
ret void
}