#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*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SIGUSR1;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int /*<<< orphan*/ ftruncate(int, int);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int /*<<< orphan*/ strcat(char *, char *);
int main(int argc, char *argv[]) {
  int sharedFD;
  if ((sharedFD =
           shm_open("/sharedPID", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) != -1) {
    ftruncate(sharedFD, 4096);
    char *sharedVariable =
        mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, sharedFD, 0);
    int effectivePID = atoi(sharedVariable);
    printf("pid = %d\n", effectivePID);
    shm_unlink("/sharedPID");
    int i;
    for (i = 0; i < 4096; i++)
      sharedVariable[i] = '\0';
    for (i = 1; i < argc; i++) {
      strcat(sharedVariable, argv[i]);
      strcat(sharedVariable, " ");
    }
    kill(effectivePID, SIGUSR1);
    return 0;
  } else {
    perror(NULL);
  }
}


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)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
or     (%rax),%edx
lea    0xe2f(%rip),%rdi
call   10a0 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12db <main+0x14b>
mov    -0x14(%rbp),%edi
mov    $0x1000,%esi
call   1050 <ftruncate@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
or     (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
lea    0xdbb(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x1000,-0x28(%rbp)
jge    1278 <main+0xe8>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1251 <main+0xc1>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12be <main+0x12e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xd6a(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    127f <main+0xef>
mov    -0x24(%rbp),%edi
mov    0x2d00(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e4 <main+0x154>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r14d
mov    0x2e40(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dde(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe24(%rip),%rdi
call   10b0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12c2 <main+0x122>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1000,%esi
call   1050 <ftruncate@plt>
mov    0x2df1(%rip),%rax
mov    0x2daa(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dcf(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
lea    0xdd1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xdb8(%rip),%rdi
call   10c0 <shm_unlink@plt>
mov    $0x1000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
cmp    $0x2,%r14d
jl     12a0 <main+0x100>
mov    %r14d,%r13d
mov    $0x1,%ebx
lea    0xda4(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbx,8),%rsi
mov    %rbp,%rdi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1280 <main+0xe0>
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1070 <kill@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1080 <perror@plt>
jmp    12b1 <main+0x111>

LLVM IR (-O0):
@.str = [11 x i8] c"/sharedPID\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [10 x i8] c"pid = %d\0A\00"
@.str.2 = [2 x i8] c" \00"
@SIGUSR1 = global i32 0
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
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i32 @shm_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15)
store i32 %16, i32* %6
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %65
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 @ftruncate(i32 %19, i32 4096)
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_SHARED
%25 = load i32, i32* %6
%26 = call i8* @mmap(i32* null, i32 4096, i32 %23, i32 %24, i32 %25, i32 0)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @shm_unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %9
br label %32
32: ; preds = %40, %18
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 4096
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = load i8*, i8** %7
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %35
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %32
43: ; preds = %32
store i32 1, i32* %9
br label %44
44: ; preds = %58, %43
%45 = load i32, i32* %9
%46 = load i32, i32* %4
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %61
48: ; preds = %44
%49 = load i8*, i8** %7
%50 = load i8**, i8*** %5
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %50, i64 %52
%54 = load i8*, i8** %53
%55 = call i32 @strcat(i8* %49, i8* %54)
%56 = load i8*, i8** %7
%57 = call i32 @strcat(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %48
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
br label %44
61: ; preds = %44
%62 = load i32, i32* %8
%63 = load i32, i32* @SIGUSR1
%64 = call i32 @kill(i32 %62, i32 %63)
store i32 0, i32* %3
br label %67
65: ; preds = %2
%66 = call i32 @perror(i32* null)
br label %67
67: ; preds = %61, %65
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [11 x i8] c"/sharedPID\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [10 x i8] c"pid = %d\0A\00"
@.str.2 = [2 x i8] c" \00"
@SIGUSR1 = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @shm_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %35, label %11
11: ; preds = %2
%12 = tail call i32 @ftruncate(i32 %9, i32 4096)
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i8* @mmap(i32* null, i32 4096, i32 %15, i32 %16, i32 %9, i32 0)
%18 = tail call i32 @atoi(i8* %17)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%21 = icmp sgt i32 %0, 1
br i1 %21, label %22, label %32
22: ; preds = %11
%23 = zext i32 %0 to i64
br label %24
24: ; preds = %22, %24
%25 = phi i64 [ 1, %22 ], [ %30, %24 ]
%26 = getelementptr inbounds i8*, i8** %1, i64 %25
%27 = load i8*, i8** %26
%28 = tail call i32 @strcat(i8* %17, i8* %27)
%29 = tail call i32 @strcat(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = add nuw nsw i64 %25, 1
%31 = icmp eq i64 %30, %23
br i1 %31, label %32, label %24
32: ; preds = %24, %11
%33 = load i32, i32* @SIGUSR1
%34 = tail call i32 @kill(i32 %18, i32 %33)
br label %37
35: ; preds = %2
%36 = tail call i32 @perror(i32* null)
br label %37
37: ; preds = %35, %32
ret i32 0
}