#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 dummy;
};
typedef int /*<<< orphan*/ shmid;
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int write(int, int *, int);
void parent(int fd) {
  int shmid;
  if ((shmid = shmget((key_t)9979, 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  struct shmid_ds shmid_ds;
  if (shmctl(shmid, IPC_STAT, &shmid_ds) == -1) {
    perror("shmctl: shmctl failed");
    exit(1);
  }
  printf("Shmid: %d\n", shmid);
  void *addr = shmat(shmid, NULL, 0);
  if (addr == (void *)-1) {
    perror("main: shmat");
    abort();
  }
  memset(addr, 0, 1024);
  if (write(fd, &shmid, sizeof(shmid)) != sizeof(shmid)) {
    perror("write");
    abort();
  }
  int *ptr = (int *)addr;
  int i;
  for (i = 1; i < 100000; i++) {
    printf("Server: %d\n", i);
    fflush(stdout);
    *ptr = i;
    while (*ptr != -i) {
      sleep(1);
    }
  }
  *ptr = 0;
  exit(0);
}


x86 assembly (clang, -O0):
<parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x26fb,%edi
mov    $0x400,%esi
call   10b0 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11fb <parent+0x4b>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    122f <parent+0x7f>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    1274 <parent+0xc4>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cltq
cmp    $0x4,%rax
je     12b4 <parent+0x104>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
cmpl   $0x186a0,-0x24(%rbp)
jge    132b <parent+0x17b>
mov    -0x24(%rbp),%esi
lea    0xd60(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
sub    -0x24(%rbp),%ecx
cmp    %ecx,%eax
je     1318 <parent+0x168>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    12f6 <parent+0x146>
jmp    131d <parent+0x16d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12c3 <parent+0x113>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<parent>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e0b(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x26fb,%edi
mov    $0x400,%esi
call   10b0 <shmget@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
jns    11ec <parent+0x3c>
lea    0xe16(%rip),%rdi
jmp    120f <parent+0x5f>
mov    %eax,%ebx
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1080 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    121e <parent+0x6e>
lea    0xdf8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdf8(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    1249 <parent+0x99>
lea    0xde4(%rip),%rdi
jmp    12df <parent+0x12f>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0x4,%eax
jne    12d8 <parent+0x128>
mov    $0x1,%r12d
lea    0xdbc(%rip),%r14
mov    0x2d53(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %r12d,%esi
call   1050 <printf@plt>
mov    (%r15),%edi
call   1070 <fflush@plt>
mov    %r12d,(%rbx)
mov    %r12d,%ebp
neg    %ebp
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10d0 <sleep@plt>
cmp    %ebp,(%rbx)
jne    12b0 <parent+0x100>
add    $0x1,%r12d
cmp    $0x186a0,%r12d
jne    1290 <parent+0xe0>
movl   $0x0,(%rbx)
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd55(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@IPC_STAT = global i32 0
@.str.1 = [22 x i8] c"shmctl: shmctl failed\00"
@.str.2 = [11 x i8] c"Shmid: %d\0A\00"
@.str.3 = [12 x i8] c"main: shmat\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [12 x i8] c"Server: %d\0A\00"
@stdout = global i32 0
define void @parent(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.shmid_ds
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 438
%10 = call i32 @shmget(i32 9979, i32 1024, i32 %9)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i32, i32* %3
%17 = load i32, i32* @IPC_STAT
%18 = call i32 @shmctl(i32 %16, i32 %17, %struct.shmid_ds* %4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = load i32, i32* %3
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %3
%27 = call i8* @shmat(i32 %26, i32* null, i32 0)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp eq i8* %28, inttoptr (i64 -1 to i8*)
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 (...) @abort()
unreachable
33: ; preds = %23
%34 = load i8*, i8** %5
%35 = call i32 @memset(i8* %34, i32 0, i32 1024)
%36 = load i32, i32* %2
%37 = call i32 @write(i32 %36, i32* %3, i32 4)
%38 = sext i32 %37 to i64
%39 = icmp ne i64 %38, 4
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 (...) @abort()
unreachable
43: ; preds = %33
%44 = load i8*, i8** %5
%45 = bitcast i8* %44 to i32*
store i32* %45, i32** %6
store i32 1, i32* %7
br label %46
46: ; preds = %65, %43
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 100000
br i1 %48, label %49, label %68
49: ; preds = %46
%50 = load i32, i32* %7
%51 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
%54 = load i32, i32* %7
%55 = load i32*, i32** %6
store i32 %54, i32* %55
br label %56
56: ; preds = %62, %49
%57 = load i32*, i32** %6
%58 = load i32, i32* %57
%59 = load i32, i32* %7
%60 = sub nsw i32 0, %59
%61 = icmp ne i32 %58, %60
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 @sleep(i32 1)
br label %56
64: ; preds = %56
br label %65
65: ; preds = %64
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
br label %46
68: ; preds = %46
%69 = load i32*, i32** %6
store i32 0, i32* %69
%70 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32 }
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@IPC_STAT = global i32 0
@.str.1 = [22 x i8] c"shmctl: shmctl failed\00"
@.str.2 = [11 x i8] c"Shmid: %d\0A\00"
@.str.3 = [12 x i8] c"main: shmat\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [12 x i8] c"Server: %d\0A\00"
@stdout = global i32 0
define void @parent(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.shmid_ds
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, 438
%7 = tail call i32 @shmget(i32 9979, i32 1024, i32 %6)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = bitcast %struct.shmid_ds* %3 to i8*
%14 = load i32, i32* @IPC_STAT
%15 = call i32 @shmctl(i32 %7, i32 %14, %struct.shmid_ds* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%22 = call i8* @shmat(i32 %7, i32* null, i32 0)
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %20
%28 = call i32 @memset(i8* %22, i32 0, i32 1024)
%29 = call i32 @write(i32 %0, i32* %2, i32 4)
%30 = icmp eq i32 %29, 4
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %27
%35 = bitcast i8* %22 to i32*
br label %36
36: ; preds = %46, %34
%37 = phi i32 [ 1, %34 ], [ %47, %46 ]
%38 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
store i32 %37, i32* %35
%41 = sub nsw i32 0, %37
br label %42
42: ; preds = %36, %42
%43 = call i32 @sleep(i32 1)
%44 = load i32, i32* %35
%45 = icmp eq i32 %44, %41
br i1 %45, label %46, label %42
46: ; preds = %42
%47 = add nuw nsw i32 %37, 1
%48 = icmp eq i32 %47, 100000
br i1 %48, label %49, label %36
49: ; preds = %46
store i32 0, i32* %35
%50 = call i32 @exit(i32 0)
unreachable
}