#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 */
typedef int /*<<< orphan*/ time_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ release_lock(int);
int semget(int, int, int /*<<< orphan*/);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmdt(void *);
int shmget(int, int, int);
int /*<<< orphan*/ wait_and_lock(int);
int main(int argc, char *argv[]) {
  int shmid, sem_id;
  void *shared_memory = NULL;
  time_t now;
  int n;
  shmid = shmget(99, 1024, 0777);
  if (shmid == -1) {
    perror("shmget() failed");
    exit(1);
  };
  shared_memory = shmat(shmid, NULL, 0);
  if (shared_memory == NULL) {
    perror("shmat() failed");
    exit(2);
  };
  sem_id = semget(9900, 2, 0);
  if (sem_id == -1) {
    perror("semget() failed");
    exit(3);
  };
  printf("\tshm_tc2 waiting for lock\n");
  wait_and_lock(sem_id);
  printf("\tshm_tc2 reading memory\n");
  printf("The time, direct from memory: %s", (char *)shared_memory);
  release_lock(sem_id);
  printf("\tshm_tc2 released lock\n");
  if (shmdt(shared_memory) == -1) {
    perror("shmdt() failed");
    exit(3);
  };
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x63,%edi
mov    $0x400,%esi
mov    $0x1ff,%edx
call   10a0 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e5 <main+0x55>
lea    0xe2a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1218 <main+0x88>
lea    0xe07(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    $0x26ac,%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1040 <semget@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    124c <main+0xbc>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
call   1030 <wait_and_lock@plt>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <release_lock@plt>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0x130>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x63,%edi
mov    $0x400,%esi
mov    $0x1ff,%edx
call   10b0 <shmget@plt>
cmp    $0xffffffff,%eax
je     124c <main+0xac>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
test   %rax,%rax
je     1262 <main+0xc2>
mov    %rax,%rbx
mov    $0x26ac,%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1040 <semget@plt>
cmp    $0xffffffff,%eax
je     1278 <main+0xd8>
mov    %eax,%ebp
lea    0xe65(%rip),%rdi
call   1060 <puts@plt>
mov    %ebp,%edi
call   1030 <wait_and_lock@plt>
lea    0xe6c(%rip),%rdi
call   1060 <puts@plt>
lea    0xe16(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
call   1050 <release_lock@plt>
lea    0xe60(%rip),%rdi
call   1060 <puts@plt>
mov    %rbx,%rdi
call   1070 <shmdt@plt>
cmp    $0xffffffff,%eax
je     1281 <main+0xe1>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdad(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xda7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
lea    0xda0(%rip),%rdi
jmp    1288 <main+0xe8>
lea    0xdc8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"shmget() failed\00"
@.str.1 = [15 x i8] c"shmat() failed\00"
@.str.2 = [16 x i8] c"semget() failed\00"
@.str.3 = [27 x i8] c"\09shm_tc2 waiting for lock\0A\00"
@.str.4 = [25 x i8] c"\09shm_tc2 reading memory\0A\00"
@.str.5 = [33 x i8] c"The time, direct from memory: %s\00"
@.str.6 = [24 x i8] c"\09shm_tc2 released lock\0A\00"
@.str.7 = [15 x i8] c"shmdt() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %8
%11 = call i32 @shmget(i32 99, i32 1024, i32 511)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i8* @shmat(i32 %18, i32* null, i32 0)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %17
%26 = call i32 @semget(i32 9900, i32 2, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 3)
unreachable
32: ; preds = %25
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* %7
%35 = call i32 @wait_and_lock(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%37 = load i8*, i8** %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = load i32, i32* %7
%40 = call i32 @release_lock(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%42 = load i8*, i8** %8
%43 = call i32 @shmdt(i8* %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %32
%46 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
%47 = call i32 @exit(i32 3)
unreachable
48: ; preds = %32
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"shmget() failed\00"
@.str.1 = [15 x i8] c"shmat() failed\00"
@.str.2 = [16 x i8] c"semget() failed\00"
@.str.5 = [33 x i8] c"The time, direct from memory: %s\00"
@.str.7 = [15 x i8] c"shmdt() failed\00"
@str = [26 x i8] c"\09shm_tc2 waiting for lock\00"
@str.8 = [24 x i8] c"\09shm_tc2 reading memory\00"
@str.9 = [23 x i8] c"\09shm_tc2 released lock\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @shmget(i32 99, i32 1024, i32 511)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i8* @shmat(i32 %3, i32* null, i32 0)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 2)
unreachable
14: ; preds = %8
%15 = tail call i32 @semget(i32 9900, i32 2, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 3)
unreachable
20: ; preds = %14
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @wait_and_lock(i32 %15)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.8, i64 0, i64 0))
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %9)
%25 = tail call i32 @release_lock(i32 %15)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
%27 = tail call i32 @shmdt(i8* %9)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
%31 = tail call i32 @exit(i32 3)
unreachable
32: ; preds = %20
ret i32 0
}