#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*/ 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*/ 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("B 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, "ccccc", 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    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
lea    0xe56(%rip),%rdi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
lea    0xe4f(%rip),%rdi
mov    $0x1,%esi
call   10a0 <ftok@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <semctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe21(%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    0xdff(%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   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    0x2e22(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
lea    0xe56(%rip),%rdi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe51(%rip),%rdi
mov    $0x1,%esi
call   10a0 <ftok@plt>
mov    %eax,%ebp
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebp
lea    0xe39(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <semctl@plt>
lea    0xe2f(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    %rsp,%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
movq   $0x0,0x8(%rsp)
movl   $0x1,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
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"B val is %d\0A\00"
@.str.6 = [6 x i8] c"ccccc\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 @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 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 1
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 0
store i32 -1, i32* %28
%29 = load i32, i32* %3
%30 = call i32 @semop(i32 %29, %struct.sembuf* %5, i32 1)
%31 = load i32, i32* %2
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
%33 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 1
store i64 0, i64* %33
%34 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 0
store i32 1, i32* %34
%35 = load i32, i32* %3
%36 = call i32 @semop(i32 %35, %struct.sembuf* %5, i32 1)
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
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"B val is %d\0A\00"
@.str.6 = [6 x i8] c"ccccc\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 @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 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 1
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 0
store i32 -1, i32* %19
%20 = call i32 @semop(i32 %13, %struct.sembuf* %1, i32 1)
%21 = call i32 @write(i32 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
store i64 0, i64* %18
store i32 1, i32* %19
%22 = call i32 @semop(i32 %13, %struct.sembuf* %1, i32 1)
%23 = call i32 @close(i32 %8)
ret void
}