#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_segsz;
  int shm_cpid;
};

/* Variables and functions */
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ LOG_DEBUG;
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ send_shm(int, int);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int /*<<< orphan*/ shmdt(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
void read_shm(int spid, int shmid) {
  int pid2 = -1;
  char *shmaddr;
  struct shmid_ds buf;
  int flag = 0;
  syslog(LOG_DEBUG, "daemon: read shm %d %d", spid, shmid);
  flag = shmctl(shmid, IPC_STAT, &buf);
  if (flag == -1) {
    perror("shmctl shm error");
    return;
  }
  syslog(LOG_DEBUG, "shm_segsz =%d bytes shmid %d", buf.shm_segsz,
         buf.shm_cpid);
  shmaddr = (char *)shmat(shmid, NULL, 0);
  if (shmaddr == NULL) {
    perror("shmat addr error");
    return;
  }
  syslog(LOG_DEBUG, "daemon:%s", shmaddr);
  shmdt(shmaddr);
  if ((pid2 = fork()) == 0) {
    send_shm(spid, shmid);
  } else {
    syslog(LOG_DEBUG, " send pid %d shmid%d\n", spid, shmid);
    return;
  }
}


x86 assembly (clang, -O0):
<read_shm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x8(%rbp),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <shmctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11dc <read_shm+0x6c>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
jmp    128b <read_shm+0x11b>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1227 <read_shm+0xb7>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
jmp    128b <read_shm+0x11b>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    126e <read_shm+0xfe>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <send_shm@plt>
jmp    128b <read_shm+0x11b>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_shm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%r14d
mov    0x2e55(%rip),%r15
mov    (%r15),%edi
lea    0xe73(%rip),%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <syslog@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
je     1230 <read_shm+0xc0>
mov    (%r15),%edi
mov    (%rsp),%edx
mov    0x4(%rsp),%ecx
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1050 <syslog@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
test   %rax,%rax
je     123e <read_shm+0xce>
mov    %rax,%rbp
mov    (%r15),%edi
lea    0xe70(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
mov    %rbp,%rdi
call   1030 <shmdt@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     121b <read_shm+0xab>
mov    (%r15),%edi
lea    0xe53(%rip),%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <syslog@plt>
jmp    1225 <read_shm+0xb5>
mov    %r14d,%edi
mov    %ebx,%esi
call   1040 <send_shm@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde0(%rip),%rdi
call   1070 <perror@plt>
jmp    1225 <read_shm+0xb5>
lea    0xe00(%rip),%rdi
call   1070 <perror@plt>
jmp    1225 <read_shm+0xb5>

LLVM IR (-O0):
%struct.shmid_ds = type { i32, i32 }
@LOG_DEBUG = global i32 0
@.str = [23 x i8] c"daemon: read shm %d %d\00"
@IPC_STAT = global i32 0
@.str.1 = [17 x i8] c"shmctl shm error\00"
@.str.2 = [29 x i8] c"shm_segsz =%d bytes shmid %d\00"
@.str.3 = [17 x i8] c"shmat addr error\00"
@.str.4 = [10 x i8] c"daemon:%s\00"
@.str.5 = [22 x i8] c" send pid %d shmid%d\0A\00"
define void @read_shm(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.shmid_ds
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 -1, i32* %5
store i32 0, i32* %8
%9 = load i32, i32* @LOG_DEBUG
%10 = load i32, i32* %3
%11 = load i32, i32* %4
%12 = call i32 (i32, i8*, ...) @syslog(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
%13 = load i32, i32* %4
%14 = load i32, i32* @IPC_STAT
%15 = call i32 @shmctl(i32 %13, i32 %14, %struct.shmid_ds* %7)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %51
20: ; preds = %2
%21 = load i32, i32* @LOG_DEBUG
%22 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %7, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 (i32, i8*, ...) @syslog(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %25)
%27 = load i32, i32* %4
%28 = call i64 @shmat(i32 %27, i32* null, i32 0)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %51
34: ; preds = %20
%35 = load i32, i32* @LOG_DEBUG
%36 = load i8*, i8** %6
%37 = call i32 (i32, i8*, ...) @syslog(i32 %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = load i8*, i8** %6
%39 = call i32 @shmdt(i8* %38)
%40 = call i32 (...) @fork()
store i32 %40, i32* %5
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %34
%43 = load i32, i32* %3
%44 = load i32, i32* %4
%45 = call i32 @send_shm(i32 %43, i32 %44)
br label %51
46: ; preds = %34
%47 = load i32, i32* @LOG_DEBUG
%48 = load i32, i32* %3
%49 = load i32, i32* %4
%50 = call i32 (i32, i8*, ...) @syslog(i32 %47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %48, i32 %49)
br label %51
51: ; preds = %18, %32, %46, %42
ret void
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32, i32 }
@LOG_DEBUG = global i32 0
@.str = [23 x i8] c"daemon: read shm %d %d\00"
@IPC_STAT = global i32 0
@.str.1 = [17 x i8] c"shmctl shm error\00"
@.str.2 = [29 x i8] c"shm_segsz =%d bytes shmid %d\00"
@.str.3 = [17 x i8] c"shmat addr error\00"
@.str.4 = [10 x i8] c"daemon:%s\00"
@.str.5 = [22 x i8] c" send pid %d shmid%d\0A\00"
define void @read_shm(i32 %0, i32 %1) {
%3 = alloca %struct.shmid_ds
%4 = bitcast %struct.shmid_ds* %3 to i8*
%5 = load i32, i32* @LOG_DEBUG
%6 = tail call i32 (i32, i8*, ...) @syslog(i32 %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%7 = load i32, i32* @IPC_STAT
%8 = call i32 @shmctl(i32 %1, i32 %7, %struct.shmid_ds* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %35
12: ; preds = %2
%13 = load i32, i32* @LOG_DEBUG
%14 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 (i32, i8*, ...) @syslog(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %17)
%19 = call i64 @shmat(i32 %1, i32* null, i32 0)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %35
24: ; preds = %12
%25 = load i32, i32* @LOG_DEBUG
%26 = call i32 (i32, i8*, ...) @syslog(i32 %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%27 = call i32 @shmdt(i8* %20)
%28 = call i32 (...) @fork()
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @send_shm(i32 %0, i32 %1)
br label %35
32: ; preds = %24
%33 = load i32, i32* @LOG_DEBUG
%34 = call i32 (i32, i8*, ...) @syslog(i32 %33, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %0, i32 %1)
br label %35
35: ; preds = %30, %32, %22, %10
ret void
}