#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 */

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ my_sem_post(int, int /*<<< orphan*/);
int /*<<< orphan*/ my_sem_wait(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ shmdt(void *);
int /*<<< orphan*/ stdin;
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int read_share_memory(int semid, char *addr) {
  int n;
  while (1) {
    my_sem_wait(semid, 1);
    printf(">");
    fgets(addr, 1024, stdin);
    addr[strlen(addr) - 1] = '\0';
    my_sem_post(semid, 0);
    if (strncmp(addr, "quit", 4) == 0) {
      if (shmdt((void *)addr) < 0) {
        perror("Fail to semdt");
        return -1;
      }
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_share_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1070 <my_sem_wait@plt>
lea    0xe5d(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1090 <fgets@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1080 <my_sem_post@plt>
mov    -0x10(%rbp),%rdi
lea    0xe0d(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1239 <read_share_memory+0xb9>
mov    -0x10(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0x0,%rax
jge    1234 <read_share_memory+0xb4>
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <read_share_memory+0xc5>
jmp    123e <read_share_memory+0xbe>
jmp    118f <read_share_memory+0xf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_share_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e55(%rip),%r15
lea    0xe66(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <my_sem_wait@plt>
mov    $0x3e,%edi
call   1030 <putchar@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <my_sem_post@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11a0 <read_share_memory+0x20>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1210 <read_share_memory+0x90>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1205 <read_share_memory+0x85>

LLVM IR (-O0):
@.str = [2 x i8] c">\00"
@stdin = global i32 0
@.str.1 = [5 x i8] c"quit\00"
@.str.2 = [14 x i8] c"Fail to semdt\00"
define i32 @read_share_memory(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
br label %7
7: ; preds = %2, %32
%8 = load i32, i32* %4
%9 = call i32 @my_sem_wait(i32 %8, i32 1)
%10 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = load i8*, i8** %5
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %11, i32 1024, i32 %12)
%14 = load i8*, i8** %5
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %14, i64 %18
store i8 0, i8* %19
%20 = load i32, i32* %4
%21 = call i32 @my_sem_post(i32 %20, i32 0)
%22 = load i8*, i8** %5
%23 = call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %7
%26 = load i8*, i8** %5
%27 = call i64 @shmdt(i8* %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %34
31: ; preds = %25
br label %33
32: ; preds = %7
br label %7
33: ; preds = %31
store i32 0, i32* %3
br label %34
34: ; preds = %33, %29
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [5 x i8] c"quit\00"
@.str.2 = [14 x i8] c"Fail to semdt\00"
define i32 @read_share_memory(i32 %0, i8* %1) {
br label %3
3: ; preds = %3, %2
%4 = tail call i32 @my_sem_wait(i32 %0, i32 1)
%5 = tail call i32 @putchar(i32 62)
%6 = load i32, i32* @stdin
%7 = tail call i32 @fgets(i8* %1, i32 1024, i32 %6)
%8 = tail call i32 @strlen(i8* %1)
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %11
%12 = tail call i32 @my_sem_post(i32 %0, i32 0)
%13 = tail call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %3
15: ; preds = %3
%16 = tail call i64 @shmdt(i8* %1)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %15, %18
%21 = phi i32 [ -1, %18 ], [ 0, %15 ]
ret i32 %21
}