#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 shmid_ds {
  int shm_cpid;
  int shm_lpid;
  scalar_t__ shm_segsz;
};

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int /*<<< orphan*/ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
int shmem_test(void) {
  int mid;
  char *shmaddr;
  int flag = 0;
  struct shmid_ds buf;
  printf("share mem\n");
  mid = shmget(IPC_PRIVATE, 0x100000, IPC_CREAT | IPC_EXCL);
  if (mid < 0) {
    perror("get share mem fail ");
    exit(-1);
  }
  int pid = fork();
  if (pid == 0) {
    shmaddr = (char *)shmat(mid, NULL, 0);
    if (shmaddr == (char *)-1) {
      perror("shmat addr error ");
      exit(-1);
    }
    strcpy(shmaddr, "Hi, I am child process!\n");
    shmdt(shmaddr);
    exit(0);
  } else if (pid > 0) {
    sleep(3);
    flag = shmctl(mid, IPC_STAT, &buf);
    if (flag == -1) {
      perror("shmctl shm error ");
      exit(-1);
    }
    printf("shm_segsz =%d bytes\n", (int)buf.shm_segsz);
    printf("parent pid=%d, shm_cpid = %d \n", getpid(), buf.shm_cpid);
    printf("chlid pid=%d, shm_lpid = %d \n", pid, buf.shm_lpid);
    shmaddr = (char *)shmat(mid, NULL, 0);
    if (shmaddr == (char *)-1) {
      perror("shmat addr error ");
      exit(-2);
    }
    printf("recv :%s", shmaddr);
    shmdt(shmaddr);
    shmctl(mid, IPC_RMID, NULL);
  } else {
    perror("fork error");
    shmctl(mid, IPC_RMID, NULL);
    exit(-3);
  }
  return 0;
}


x86 assembly (clang, -O0):
<shmem_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x14(%rbp)
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
mov    0x2e0a(%rip),%rax
or     (%rax),%edx
mov    $0x100000,%esi
call   10a0 <shmget@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1215 <shmem_test+0x65>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1282 <shmem_test+0xd2>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    1262 <shmem_test+0xb2>
lea    0xdcc(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xdc4(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <shmdt@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    137e <shmem_test+0x1ce>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    -0x4(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <shmctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12ce <shmem_test+0x11e>
lea    0xd8b(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%edx
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    1349 <shmem_test+0x199>
lea    0xce5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <shmdt@plt>
mov    -0x4(%rbp),%edi
mov    0x2c5a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <shmctl@plt>
jmp    13a9 <shmem_test+0x1f9>
lea    0xd32(%rip),%rdi
call   1080 <perror@plt>
mov    -0x4(%rbp),%edi
mov    0x2c34(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <shmctl@plt>
mov    $0xfffffffd,%edi
call   10b0 <exit@plt>
jmp    13ae <shmem_test+0x1fe>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shmem_test>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xeea(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x100000,%esi
call   10b0 <shmget@plt>
test   %eax,%eax
js     12cd <shmem_test+0x10d>
mov    %eax,%ebx
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12d6 <shmem_test+0x116>
jle    12f0 <shmem_test+0x130>
mov    %eax,%ebp
mov    $0x3,%edi
call   10d0 <sleep@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1080 <shmctl@plt>
cmp    $0xffffffff,%eax
je     1318 <shmem_test+0x158>
mov    0x10(%rsp),%esi
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    0x8(%rsp),%edx
lea    0xe02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0xc(%rsp),%edx
lea    0xe0d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     132e <shmem_test+0x16e>
mov    %rax,%rbp
lea    0xe03(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1050 <shmdt@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <shmctl@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd2c(%rip),%rdi
jmp    131f <shmem_test+0x15f>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    1344 <shmem_test+0x184>
lea    0xd26(%rip),%rdi
jmp    131f <shmem_test+0x15f>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <shmctl@plt>
mov    $0xfffffffd,%edi
call   10c0 <exit@plt>
lea    0xd20(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xcdf(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffffe,%edi
call   10c0 <exit@plt>
mov    %rax,%rbx
lea    0xcd8(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { i32, i32, i64 }
@.str = [11 x i8] c"share mem\0A\00"
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.1 = [20 x i8] c"get share mem fail \00"
@.str.2 = [18 x i8] c"shmat addr error \00"
@.str.3 = [25 x i8] c"Hi, I am child process!\0A\00"
@IPC_STAT = global i32 0
@.str.4 = [18 x i8] c"shmctl shm error \00"
@.str.5 = [21 x i8] c"shm_segsz =%d bytes\0A\00"
@.str.6 = [31 x i8] c"parent pid=%d, shm_cpid = %d \0A\00"
@.str.7 = [30 x i8] c"chlid pid=%d, shm_lpid = %d \0A\00"
@.str.8 = [9 x i8] c"recv :%s\00"
@IPC_RMID = global i32 0
@.str.9 = [11 x i8] c"fork error\00"
define i32 @shmem_test() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.shmid_ds
%5 = alloca i32
store i32 0, i32* %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @IPC_PRIVATE
%8 = load i32, i32* @IPC_CREAT
%9 = load i32, i32* @IPC_EXCL
%10 = or i32 %8, %9
%11 = call i32 @shmget(i32 %7, i32 1048576, i32 %10)
store i32 %11, i32* %1
%12 = load i32, i32* %1
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = call i32 (...) @fork()
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %36
21: ; preds = %17
%22 = load i32, i32* %1
%23 = call i64 @shmat(i32 %22, i32* null, i32 0)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %2
%25 = load i8*, i8** %2
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %21
%31 = load i8*, i8** %2
%32 = call i32 @strcpy(i8* %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%33 = load i8*, i8** %2
%34 = call i32 @shmdt(i8* %33)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %17
%37 = load i32, i32* %5
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %78
39: ; preds = %36
%40 = call i32 @sleep(i32 3)
%41 = load i32, i32* %1
%42 = load i32, i32* @IPC_STAT
%43 = call i32 @shmctl(i32 %41, i32 %42, %struct.shmid_ds* %4)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %39
%50 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i32 0, i32 2
%51 = load i64, i64* %50
%52 = trunc i64 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = call i32 (...) @getpid()
%55 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %56)
%58 = load i32, i32* %5
%59 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i32 0, i32 1
%60 = load i32, i32* %59
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i32 %58, i32 %60)
%62 = load i32, i32* %1
%63 = call i64 @shmat(i32 %62, i32* null, i32 0)
%64 = inttoptr i64 %63 to i8*
store i8* %64, i8** %2
%65 = load i8*, i8** %2
%66 = icmp eq i8* %65, inttoptr (i64 -1 to i8*)
br i1 %66, label %67, label %70
67: ; preds = %49
%68 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%69 = call i32 @exit(i32 -2)
unreachable
70: ; preds = %49
%71 = load i8*, i8** %2
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %71)
%73 = load i8*, i8** %2
%74 = call i32 @shmdt(i8* %73)
%75 = load i32, i32* %1
%76 = load i32, i32* @IPC_RMID
%77 = call i32 @shmctl(i32 %75, i32 %76, %struct.shmid_ds* null)
br label %84
78: ; preds = %36
%79 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%80 = load i32, i32* %1
%81 = load i32, i32* @IPC_RMID
%82 = call i32 @shmctl(i32 %80, i32 %81, %struct.shmid_ds* null)
%83 = call i32 @exit(i32 -3)
unreachable
84: ; preds = %70
br label %85
85: ; preds = %84
ret i32 0
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32, i32, i64 }
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.1 = [20 x i8] c"get share mem fail \00"
@.str.2 = [18 x i8] c"shmat addr error \00"
@.str.3 = [25 x i8] c"Hi, I am child process!\0A\00"
@IPC_STAT = global i32 0
@.str.4 = [18 x i8] c"shmctl shm error \00"
@.str.5 = [21 x i8] c"shm_segsz =%d bytes\0A\00"
@.str.6 = [31 x i8] c"parent pid=%d, shm_cpid = %d \0A\00"
@.str.7 = [30 x i8] c"chlid pid=%d, shm_lpid = %d \0A\00"
@.str.8 = [9 x i8] c"recv :%s\00"
@IPC_RMID = global i32 0
@.str.9 = [11 x i8] c"fork error\00"
@str = [10 x i8] c"share mem\00"
define i32 @shmem_test() {
%1 = alloca %struct.shmid_ds
%2 = bitcast %struct.shmid_ds* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @IPC_PRIVATE
%5 = load i32, i32* @IPC_CREAT
%6 = load i32, i32* @IPC_EXCL
%7 = or i32 %6, %5
%8 = tail call i32 @shmget(i32 %4, i32 1048576, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = tail call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = tail call i64 @shmat(i32 %8, i32* null, i32 0)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %16
%24 = tail call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @shmdt(i8* %18)
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %13
%28 = icmp sgt i32 %14, 0
br i1 %28, label %29, label %60
29: ; preds = %27
%30 = tail call i32 @sleep(i32 3)
%31 = load i32, i32* @IPC_STAT
%32 = call i32 @shmctl(i32 %8, i32 %31, %struct.shmid_ds* %1)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %29
%38 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 2
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = call i32 (...) @getpid()
%43 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %42, i32 %44)
%46 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %1, i64 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i32 %14, i32 %47)
%49 = call i64 @shmat(i32 %8, i32* null, i32 0)
%50 = inttoptr i64 %49 to i8*
%51 = icmp eq i8* %50, inttoptr (i64 -1 to i8*)
br i1 %51, label %52, label %55
52: ; preds = %37
%53 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @exit(i32 -2)
unreachable
55: ; preds = %37
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %50)
%57 = call i32 @shmdt(i8* %50)
%58 = load i32, i32* @IPC_RMID
%59 = call i32 @shmctl(i32 %8, i32 %58, %struct.shmid_ds* null)
ret i32 0
60: ; preds = %27
%61 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%62 = load i32, i32* @IPC_RMID
%63 = tail call i32 @shmctl(i32 %8, i32 %62, %struct.shmid_ds* null)
%64 = tail call i32 @exit(i32 -3)
unreachable
}