#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 IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int shmid;
  void *shared_memory = NULL;
  char *mem_ptr;
  time_t now;
  int n, i;
  shmid = shmget(999, 1024, IPC_CREAT | 0777);
  if (shmid == -1) {
    perror("shmget() failed");
    exit(1);
  };
  shared_memory = shmat(shmid, NULL, 0);
  if (shared_memory == NULL) {
    perror("shmat() failed");
    exit(2);
  };
  mem_ptr = (char *)shared_memory;
  for (i = 0; i < 60; ++i) {
    time(&now);
    strcpy(mem_ptr, ctime(&now));
    sleep(1);
  }
  if (shmctl(shmid, IPC_RMID, 0) == -1) {
    perror("shmctl() failed");
    exit(3);
  };
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
or     $0x1ff,%edx
mov    $0x3e7,%edi
mov    $0x400,%esi
call   1090 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ef <main+0x5f>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1222 <main+0x92>
lea    0xdfd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x3c,-0x34(%rbp)
jge    1278 <main+0xe8>
lea    -0x2c(%rbp),%rdi
call   1050 <time@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1231 <main+0xa1>
mov    -0x14(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    12aa <main+0x11a>
lea    0xd84(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e3a(%rip),%rax
mov    $0x1ff,%edx
or     (%rax),%edx
mov    $0x3e7,%edi
mov    $0x400,%esi
call   1090 <shmget@plt>
cmp    $0xffffffff,%eax
je     122e <main+0x9e>
mov    %eax,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
test   %rax,%rax
je     1244 <main+0xb4>
mov    %rax,%r15
mov    $0x3c,%ebx
lea    0x4(%rsp),%rbp
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1050 <time@plt>
mov    %rbp,%rdi
call   1040 <ctime@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0xffffffff,%ebx
jne    11e0 <main+0x50>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xca>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    0xdbe(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [16 x i8] c"shmget() failed\00"
@.str.1 = [15 x i8] c"shmat() failed\00"
@IPC_RMID = global i32 0
@.str.2 = [16 x i8] c"shmctl() 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 i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
%12 = load i32, i32* @IPC_CREAT
%13 = or i32 %12, 511
%14 = call i32 @shmget(i32 999, i32 1024, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i8* @shmat(i32 %21, i32* null, i32 0)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %20
%29 = load i8*, i8** %7
store i8* %29, i8** %8
store i32 0, i32* %11
br label %30
30: ; preds = %39, %28
%31 = load i32, i32* %11
%32 = icmp slt i32 %31, 60
br i1 %32, label %33, label %42
33: ; preds = %30
%34 = call i32 @time(i32* %9)
%35 = load i8*, i8** %8
%36 = call i32 @ctime(i32* %9)
%37 = call i32 @strcpy(i8* %35, i32 %36)
%38 = call i32 @sleep(i32 1)
br label %39
39: ; preds = %33
%40 = load i32, i32* %11
%41 = add nsw i32 %40, 1
store i32 %41, i32* %11
br label %30
42: ; preds = %30
%43 = load i32, i32* %6
%44 = load i32, i32* @IPC_RMID
%45 = call i32 @shmctl(i32 %43, i32 %44, i32 0)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 3)
unreachable
50: ; preds = %42
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [16 x i8] c"shmget() failed\00"
@.str.1 = [15 x i8] c"shmat() failed\00"
@IPC_RMID = global i32 0
@.str.2 = [16 x i8] c"shmctl() failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, 511
%7 = tail call i32 @shmget(i32 999, i32 1024, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i8* @shmat(i32 %7, i32* null, i32 0)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 2)
unreachable
18: ; preds = %12, %18
%19 = phi i32 [ %24, %18 ], [ 0, %12 ]
%20 = call i32 @time(i32* %3)
%21 = call i32 @ctime(i32* %3)
%22 = call i32 @strcpy(i8* %13, i32 %21)
%23 = call i32 @sleep(i32 1)
%24 = add nuw nsw i32 %19, 1
%25 = icmp eq i32 %24, 60
br i1 %25, label %26, label %18
26: ; preds = %18
%27 = load i32, i32* @IPC_RMID
%28 = call i32 @shmctl(i32 %7, i32 %27, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 3)
unreachable
33: ; preds = %26
ret i32 0
}