#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 */
typedef int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int main() {
  int q = 3;
  int shmid;
  key_t key;
  int *buf, *x;
  key = 9876;
  shmid = shmget(key, 10 * sizeof(int), IPC_CREAT | 0666);
  if (shmid < 0) {
    perror("shmget");
    exit(1);
  }
  buf = (int *)shmat(shmid, NULL, 0);
  if (buf == NULL) {
    perror("shmat");
    exit(1);
  }
  x = buf;
  for (int i = 0; i < 10; i++) {
    *x = q;
    q = q * 3;
  }
  while (*buf != '*')
    exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3,-0x8(%rbp)
movl   $0x2694,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x28,%esi
call   1050 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119c <main+0x5c>
lea    0xe73(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cf <main+0x8f>
lea    0xe47(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    1206 <main+0xc6>
mov    -0x8(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
imul   $0x3,-0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11de <main+0x9e>
jmp    120b <main+0xcb>
mov    -0x18(%rbp),%rax
cmpl   $0x2a,(%rax)
je     121f <main+0xdf>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e98(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x2694,%edi
mov    $0x28,%esi
call   1050 <shmget@plt>
test   %eax,%eax
jns    116b <main+0x2b>
lea    0xe97(%rip),%rdi
jmp    1182 <main+0x42>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
test   %rax,%rax
jne    1191 <main+0x51>
lea    0xe85(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0xe6a9,(%rax)
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %1
store i32 3, i32* %2
store i32 9876, i32* %4
%8 = load i32, i32* %4
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = call i32 @shmget(i32 %8, i32 40, i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* %3
%19 = call i64 @shmat(i32 %18, i32* null, i32 0)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %5
%21 = load i32*, i32** %5
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32*, i32** %5
store i32* %27, i32** %6
store i32 0, i32* %7
br label %28
28: ; preds = %36, %26
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 10
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = load i32, i32* %2
%33 = load i32*, i32** %6
store i32 %32, i32* %33
%34 = load i32, i32* %2
%35 = mul nsw i32 %34, 3
store i32 %35, i32* %2
br label %36
36: ; preds = %31
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %28
39: ; preds = %28
br label %40
40: ; preds = %39
%41 = load i32*, i32** %5
%42 = load i32, i32* %41
%43 = icmp ne i32 %42, 42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %40
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = or i32 %1, 438
%3 = tail call i32 @shmget(i32 9876, i32 40, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i64 @shmat(i32 %3, i32* null, i32 0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = inttoptr i64 %9 to i32*
store i32 59049, i32* %15
%16 = tail call i32 @exit(i32 0)
unreachable
}