#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 *, char *);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmdt(void *);
int shmget(int, int, int);
int main(int argc, char *argv[]) {
  int shmid;
  void *shared_memory = NULL;
  time_t now;
  int n;
  shmid = shmget(999, 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);
  };
  printf("The time, direct from memory: %s", (char *)shared_memory);
  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    $0x3e7,%edi
mov    $0x400,%esi
mov    $0x1ff,%edx
call   1070 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b5 <main+0x55>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <main+0x88>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0xc0>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x3e7,%edi
mov    $0x400,%esi
mov    $0x1ff,%edx
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     11ad <main+0x4d>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
test   %rax,%rax
je     11c3 <main+0x63>
mov    %rax,%rbx
lea    0xe8b(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffff,%eax
je     11d9 <main+0x79>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe4c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xe60(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"shmget() failed\00"
@.str.1 = [15 x i8] c"shmat() failed\00"
@.str.2 = [33 x i8] c"The time, direct from memory: %s\00"
@.str.3 = [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 i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
%10 = call i32 @shmget(i32 999, i32 1024, i32 511)
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 = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i8* @shmat(i32 %17, i32* null, i32 0)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %16
%25 = load i8*, i8** %7
%26 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %7
%28 = call i32 @shmdt(i8* %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 3)
unreachable
33: ; preds = %24
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 = [33 x i8] c"The time, direct from memory: %s\00"
@.str.3 = [15 x i8] c"shmdt() failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @shmget(i32 999, 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 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%16 = tail call i32 @shmdt(i8* %9)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 3)
unreachable
21: ; preds = %14
ret i32 0
}