#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 timeval {
  int /*<<< orphan*/ tv_usec;
};
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ SHM_RDONLY;
int /*<<< orphan*/ SHM_REMAP;
int /*<<< orphan*/ SHM_RND;
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void *shmat(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ shmdt(void *);
int shmget(int /*<<< orphan*/, int, int);
main() {
  char *curdir;
  struct timeval tv;
  key_t ipc_key;
  int shmid;
  void *addr;
  curdir = getcwd(NULL, 0);
  gettimeofday(&tv, NULL);
  ipc_key = ftok(curdir, tv.tv_usec);
  shmid = shmget(ipc_key, 65535, (IPC_CREAT | IPC_EXCL | 0600));
  addr = shmat(shmid, NULL, SHM_RDONLY);
  addr = shmat(shmid, addr, SHM_REMAP);
  shmdt(addr);
  shmat(-1, NULL, SHM_RND);
  shmat(shmid, (void *)-1, SHM_RND);
  shmdt((void *)-1);
  shmctl(shmid, IPC_RMID, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1090 <ftok@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
mov    0x2e33(%rip),%rax
or     (%rax),%edx
or     $0x180,%edx
mov    $0xffff,%esi
call   1080 <shmget@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
call   1070 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <shmdt@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <shmat@plt>
mov    -0x20(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffffffffffff,%rsi
call   1070 <shmat@plt>
mov    $0xffffffffffffffff,%rdi
call   1030 <shmdt@plt>
mov    -0x20(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <shmctl@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    (%rsp),%esi
mov    %rbx,%rdi
call   1090 <ftok@plt>
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e4b(%rip),%rcx
or     (%rcx),%edx
or     $0x180,%edx
mov    %eax,%edi
mov    $0xffff,%esi
call   1080 <shmget@plt>
mov    %eax,%ebx
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <shmat@plt>
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edx
mov    %ebx,%edi
mov    %rax,%rsi
call   1070 <shmat@plt>
mov    %rax,%rdi
call   1030 <shmdt@plt>
mov    0x2df1(%rip),%r14
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1070 <shmat@plt>
mov    (%r14),%edx
mov    %ebx,%edi
mov    $0xffffffffffffffff,%rsi
call   1070 <shmat@plt>
mov    $0xffffffffffffffff,%rdi
call   1030 <shmdt@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <shmctl@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@SHM_RDONLY = global i32 0
@SHM_REMAP = global i32 0
@SHM_RND = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 0, i32* %1
%7 = call i8* @getcwd(i32* null, i32 0)
store i8* %7, i8** %2
%8 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%9 = load i8*, i8** %2
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @ftok(i8* %9, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* @IPC_CREAT
%15 = load i32, i32* @IPC_EXCL
%16 = or i32 %14, %15
%17 = or i32 %16, 384
%18 = call i32 @shmget(i32 %13, i32 65535, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = load i32, i32* @SHM_RDONLY
%21 = call i8* @shmat(i32 %19, i8* null, i32 %20)
store i8* %21, i8** %6
%22 = load i32, i32* %5
%23 = load i8*, i8** %6
%24 = load i32, i32* @SHM_REMAP
%25 = call i8* @shmat(i32 %22, i8* %23, i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = call i32 @shmdt(i8* %26)
%28 = load i32, i32* @SHM_RND
%29 = call i8* @shmat(i32 -1, i8* null, i32 %28)
%30 = load i32, i32* %5
%31 = load i32, i32* @SHM_RND
%32 = call i8* @shmat(i32 %30, i8* inttoptr (i64 -1 to i8*), i32 %31)
%33 = call i32 @shmdt(i8* inttoptr (i64 -1 to i8*))
%34 = load i32, i32* %5
%35 = load i32, i32* @IPC_RMID
%36 = call i32 @shmctl(i32 %34, i32 %35, i32* null)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@SHM_RDONLY = global i32 0
@SHM_REMAP = global i32 0
@SHM_RND = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = tail call i8* @getcwd(i32* null, i32 0)
%4 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i32 @ftok(i8* %3, i32 %6)
%8 = load i32, i32* @IPC_CREAT
%9 = load i32, i32* @IPC_EXCL
%10 = or i32 %8, %9
%11 = or i32 %10, 384
%12 = tail call i32 @shmget(i32 %7, i32 65535, i32 %11)
%13 = load i32, i32* @SHM_RDONLY
%14 = tail call i8* @shmat(i32 %12, i8* null, i32 %13)
%15 = load i32, i32* @SHM_REMAP
%16 = tail call i8* @shmat(i32 %12, i8* %14, i32 %15)
%17 = tail call i32 @shmdt(i8* %16)
%18 = load i32, i32* @SHM_RND
%19 = tail call i8* @shmat(i32 -1, i8* null, i32 %18)
%20 = load i32, i32* @SHM_RND
%21 = tail call i8* @shmat(i32 %12, i8* inttoptr (i64 -1 to i8*), i32 %20)
%22 = tail call i32 @shmdt(i8* inttoptr (i64 -1 to i8*))
%23 = load i32, i32* @IPC_RMID
%24 = tail call i32 @shmctl(i32 %12, i32 %23, i32* null)
ret i32 0
}