#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 key_t;

/* Variables and functions */
int /*<<< orphan*/ SEM_UNDO;
int /*<<< orphan*/ exit(int);
int ftok(char *, char);
int /*<<< orphan*/ getchar();
int initsem(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int semop(int, struct sembuf *, int);
int main(void) {
  key_t key;
  int semid;
  struct sembuf sb;
  sb.sem_num = 0;
  sb.sem_op = -1;
  sb.sem_flg = SEM_UNDO;
  if ((key = ftok("semdemo.c", 'J')) == -1) {
    perror("ftok");
    exit(1);
  }
  if ((semid = initsem(key, 1)) == -1) {
    perror("initsem");
    exit(1);
  }
  printf("Press return to lock: ");
  getchar();
  printf("Trying to lock...\n");
  if (semop(semid, &sb, 1) == -1) {
    perror("semop");
    exit(1);
  }
  printf("Locked.\n");
  printf("Press return to unlock: ");
  getchar();
  sb.sem_op = 1;
  if (semop(semid, &sb, 1) == -1) {
    perror("semop");
    exit(1);
  }
  printf("Unlocked\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe5f(%rip),%rdi
mov    $0x4a,%esi
call   1090 <ftok@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11cd <main+0x5d>
lea    0xe4c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1050 <initsem@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11fc <main+0x8c>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
lea    0xe18(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
jne    124b <main+0xdb>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdf5(%rip),%rdi
call   1030 <printf@plt>
lea    0xdf2(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
movl   $0x1,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
jne    12a1 <main+0x131>
lea    0xdaf(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdc1(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
lea    0xe57(%rip),%rdi
mov    $0x4a,%esi
call   10a0 <ftok@plt>
cmp    $0xffffffff,%eax
je     125d <main+0xdd>
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <initsem@plt>
cmp    $0xffffffff,%eax
je     1266 <main+0xe6>
mov    %eax,%ebx
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <semop@plt>
cmp    $0xffffffff,%eax
je     1254 <main+0xd4>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
movl   $0x1,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <semop@plt>
cmp    $0xffffffff,%eax
je     1254 <main+0xd4>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdd3(%rip),%rdi
jmp    126d <main+0xed>
lea    0xda6(%rip),%rdi
jmp    126d <main+0xed>
lea    0xda2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@SEM_UNDO = global i32 0
@.str = [10 x i8] c"semdemo.c\00"
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [8 x i8] c"initsem\00"
@.str.3 = [23 x i8] c"Press return to lock: \00"
@.str.4 = [19 x i8] c"Trying to lock...\0A\00"
@.str.5 = [6 x i8] c"semop\00"
@.str.6 = [9 x i8] c"Locked.\0A\00"
@.str.7 = [25 x i8] c"Press return to unlock: \00"
@.str.8 = [10 x i8] c"Unlocked\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sembuf
store i32 0, i32* %1
%5 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 2
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 0
store i32 -1, i32* %6
%7 = load i32, i32* @SEM_UNDO
%8 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 1
store i32 %7, i32* %8
%9 = call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8 signext 74)
store i32 %9, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 @initsem(i32 %15, i32 1)
store i32 %16, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 (...) @getchar()
%24 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32, i32* %3
%26 = call i32 @semop(i32 %25, %struct.sembuf* %4, i32 1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%34 = call i32 (...) @getchar()
%35 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 0
store i32 1, i32* %35
%36 = load i32, i32* %3
%37 = call i32 @semop(i32 %36, %struct.sembuf* %4, i32 1)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@SEM_UNDO = global i32 0
@.str = [10 x i8] c"semdemo.c\00"
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [8 x i8] c"initsem\00"
@.str.3 = [23 x i8] c"Press return to lock: \00"
@.str.5 = [6 x i8] c"semop\00"
@.str.7 = [25 x i8] c"Press return to unlock: \00"
@str = [18 x i8] c"Trying to lock...\00"
@str.9 = [8 x i8] c"Locked.\00"
@str.10 = [9 x i8] c"Unlocked\00"
define i32 @main() {
%1 = alloca %struct.sembuf
%2 = bitcast %struct.sembuf* %1 to i8*
%3 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 2
store i64 0, i64* %3
%4 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 0
store i32 -1, i32* %4
%5 = load i32, i32* @SEM_UNDO
%6 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 1
store i32 %5, i32* %6
%7 = tail call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8 signext 74)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = tail call i32 @initsem(i32 %7, i32 1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 (...) @getchar()
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%22 = call i32 @semop(i32 %13, %struct.sembuf* %1, i32 1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0))
%29 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%30 = call i32 (...) @getchar()
store i32 1, i32* %4
%31 = call i32 @semop(i32 %13, %struct.sembuf* %1, i32 1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.10, i64 0, i64 0))
ret i32 0
}