#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_num;
  int sem_op;
  void *sem_flg;
};
typedef int key_t;

/* Variables and functions */
int /*<<< orphan*/ IPC_CREAT;
void *SEM_UNDO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int ftok(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int semget(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ semop(int, struct sembuf *, int);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int main() {
  int semid;
  int shmid;
  int ret;
  char *p_map;
  key_t semkey, shmkey;
  struct sembuf getsem, setsem;
  semkey = ftok("/etc/fb.modes", 0x0031);
  if (semkey == -1) {
    perror("ftok error");
    exit(1);
  }
  semid = semget(semkey, 0, IPC_CREAT);
  if (semid == -1) {
    perror("semid error");
    exit(1);
  }
  printf("semid=%d/n", semid);
  getsem.sem_num = 0;
  getsem.sem_op = -1;
  getsem.sem_flg = SEM_UNDO;
  setsem.sem_num = 1;
  setsem.sem_op = 1;
  setsem.sem_flg = SEM_UNDO;
  shmkey = ftok("/root/shm3.c", 0x0029);
  if (shmkey == -1) {
    perror("ftok error");
    exit(1);
  }
  printf("shmkey=%d/n", shmkey);
  shmid = shmget(shmkey, 4096, IPC_CREAT);
  printf("shmid=%d/n", shmid);
  if (shmid == -1) {
    perror("shm error");
    exit(1);
  }
  p_map = (char *)shmat(shmid, NULL, 0);
  while (1) {
    semop(semid, &getsem, 1);
    printf("Lucy:%s", p_map);
    printf("Peter:");
    fgets(p_map, 256, stdin);
    semop(semid, &setsem, 1);
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x31,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d3 <main+0x43>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1209 <main+0x79>
lea    0xe1f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x40(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
lea    0xdd7(%rip),%rdi
mov    $0x29,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1286 <main+0xf6>
lea    0xd97(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%esi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1080 <shmget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xffffffff,-0xc(%rbp)
jne    12e1 <main+0x151>
lea    0xd82(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
mov    -0x18(%rbp),%rsi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2cb1(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1050 <fgets@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
jmp    12f3 <main+0x163>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe5b(%rip),%rdi
mov    $0x31,%esi
call   10b0 <ftok@plt>
cmp    $0xffffffff,%eax
je     12e9 <main+0x159>
mov    0x2e11(%rip),%r14
mov    (%r14),%edx
mov    %eax,%edi
xor    %esi,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     12f2 <main+0x162>
mov    %eax,%ebp
lea    0xe48(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0xffffffff00000000,%rax
ff ff ff
mov    %rax,0x18(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
movabs $0x100000001,%rcx
mov    %rcx,0x8(%rsp)
mov    %rax,0x10(%rsp)
lea    0xe11(%rip),%rdi
mov    $0x29,%esi
call   10b0 <ftok@plt>
cmp    $0xffffffff,%eax
je     12e9 <main+0x159>
mov    %eax,%ebx
lea    0xe02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%edx
mov    %ebx,%edi
mov    $0x1000,%esi
call   1080 <shmget@plt>
mov    %eax,%ebx
lea    0xded(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xffffffff,%ebx
je     12fb <main+0x16b>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
lea    0xddb(%rip),%r15
lea    0xddc(%rip),%r12
mov    0x2d4f(%rip),%r14
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
lea    0x18(%rsp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <fgets@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
jmp    12a0 <main+0x110>
lea    0xd1e(%rip),%rdi
jmp    1302 <main+0x172>
lea    0xd20(%rip),%rdi
jmp    1302 <main+0x172>
lea    0xd52(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i8* }
@.str = [14 x i8] c"/etc/fb.modes\00"
@.str.1 = [11 x i8] c"ftok error\00"
@IPC_CREAT = global i32 0
@.str.2 = [12 x i8] c"semid error\00"
@.str.3 = [11 x i8] c"semid=%d/n\00"
@SEM_UNDO = global i8* null
@.str.4 = [13 x i8] c"/root/shm3.c\00"
@.str.5 = [12 x i8] c"shmkey=%d/n\00"
@.str.6 = [11 x i8] c"shmid=%d/n\00"
@.str.7 = [10 x i8] c"shm error\00"
@.str.8 = [8 x i8] c"Lucy:%s\00"
@.str.9 = [7 x i8] c"Peter:\00"
@stdin = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sembuf
%9 = alloca %struct.sembuf
store i32 0, i32* %1
%10 = call i32 @ftok(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 49)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %6
%18 = load i32, i32* @IPC_CREAT
%19 = call i32 @semget(i32 %17, i32 0, i32 %18)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %8, i32 0, i32 0
store i32 0, i32* %28
%29 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %8, i32 0, i32 1
store i32 -1, i32* %29
%30 = load i8*, i8** @SEM_UNDO
%31 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %8, i32 0, i32 2
store i8* %30, i8** %31
%32 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 1, i32* %32
%33 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i32 1, i32* %33
%34 = load i8*, i8** @SEM_UNDO
%35 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 2
store i8* %34, i8** %35
%36 = call i32 @ftok(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 41)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %25
%40 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %25
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %7
%46 = load i32, i32* @IPC_CREAT
%47 = call i32 @shmget(i32 %45, i32 4096, i32 %46)
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %3
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %42
%53 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %42
%56 = load i32, i32* %3
%57 = call i64 @shmat(i32 %56, i32* null, i32 0)
%58 = inttoptr i64 %57 to i8*
store i8* %58, i8** %5
br label %59
59: ; preds = %55, %59
%60 = load i32, i32* %2
%61 = call i32 @semop(i32 %60, %struct.sembuf* %8, i32 1)
%62 = load i8*, i8** %5
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%65 = load i8*, i8** %5
%66 = load i32, i32* @stdin
%67 = call i32 @fgets(i8* %65, i32 256, i32 %66)
%68 = load i32, i32* %2
%69 = call i32 @semop(i32 %68, %struct.sembuf* %9, i32 1)
br label %59
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i8* }
@.str = [14 x i8] c"/etc/fb.modes\00"
@.str.1 = [11 x i8] c"ftok error\00"
@IPC_CREAT = global i32 0
@.str.2 = [12 x i8] c"semid error\00"
@.str.3 = [11 x i8] c"semid=%d/n\00"
@SEM_UNDO = global i8* null
@.str.4 = [13 x i8] c"/root/shm3.c\00"
@.str.5 = [12 x i8] c"shmkey=%d/n\00"
@.str.6 = [11 x i8] c"shmid=%d/n\00"
@.str.7 = [10 x i8] c"shm error\00"
@.str.8 = [8 x i8] c"Lucy:%s\00"
@.str.9 = [7 x i8] c"Peter:\00"
@stdin = 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 = tail call i32 @ftok(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 49)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @IPC_CREAT
%12 = tail call i32 @semget(i32 %5, i32 0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%19 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 0
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 1
store i32 -1, i32* %20
%21 = load i8*, i8** @SEM_UNDO
%22 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %1, i64 0, i32 2
store i8* %21, i8** %22
%23 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 0
store i32 1, i32* %23
%24 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 1
store i32 1, i32* %24
%25 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 2
store i8* %21, i8** %25
%26 = tail call i32 @ftok(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 41)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %17
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%33 = load i32, i32* @IPC_CREAT
%34 = tail call i32 @shmget(i32 %26, i32 4096, i32 %33)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %34)
%36 = icmp eq i32 %34, -1
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %31
%41 = tail call i64 @shmat(i32 %34, i32* null, i32 0)
%42 = inttoptr i64 %41 to i8*
br label %43
43: ; preds = %43, %40
%44 = call i32 @semop(i32 %12, %struct.sembuf* %1, i32 1)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* %42)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%47 = load i32, i32* @stdin
%48 = call i32 @fgets(i8* %42, i32 256, i32 %47)
%49 = call i32 @semop(i32 %12, %struct.sembuf* %2, i32 1)
br label %43
}