#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 scalar_t__ key_t;

/* Variables and functions */
int IPC_CREAT;
scalar_t__ errno;
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rm_shm(int);
scalar_t__ shmat(int, void *, int /*<<< orphan*/);
int shmget(scalar_t__, int, int);
int /*<<< orphan*/ tst_exit();
int /*<<< orphan*/ tst_resm(int, char *, ...);
int main(void) {
  register int shmid;
  char *cp;
  key_t key;
  errno = 0;
  key = (key_t)getpid();
  if ((shmid = shmget(key, 16 * 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    tst_resm(1, "shmget Failed: shmid = %d, errno = %d\n", shmid, errno);
    tst_exit();
  }
  tst_resm(0, "shmget");
  cp = (char *)shmat(shmid, (void *)0x80000, 0);
  if (cp == (char *)-1) {
    perror("shmat");
    tst_resm(1, "shmat Failed: shmid = %d, errno = %d\n", shmid, errno);
    rm_shm(shmid);
    tst_exit();
  }
  *cp = '1';
  *(cp + 1) = '2';
  tst_resm(0, "shmat");
  rm_shm(shmid);
  if (*cp != '1' || *(cp + 1) != '2') {
    tst_resm(1, "Error in shared memory contents: shmid = %d\n", shmid);
  }
  tst_resm(0, "Correct shared memory contents");
  tst_exit();
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x4000,%esi
call   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f4 <main+0x84>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    $0x0,%al
call   1040 <tst_exit@plt>
xor    %edi,%edi
lea    0xe03(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    -0x8(%rbp),%edi
mov    $0x80000,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    1263 <main+0xf3>
lea    0xdff(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edx
mov    0x2da2(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    -0x8(%rbp),%edi
call   1050 <rm_shm@plt>
mov    $0x0,%al
call   1040 <tst_exit@plt>
mov    -0x10(%rbp),%rax
movb   $0x31,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x32,0x1(%rax)
xor    %edi,%edi
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    -0x8(%rbp),%edi
call   1050 <rm_shm@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jne    12ab <main+0x13b>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x32,%eax
je     12c1 <main+0x151>
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
xor    %edi,%edi
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    $0x0,%al
call   1040 <tst_exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e65(%rip),%r14
movq   $0x0,(%r14)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2e48(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %rax,%rdi
mov    $0x4000,%esi
call   1080 <shmget@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1233 <main+0xc3>
lea    0xe4b(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
mov    $0x80000,%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     1263 <main+0xf3>
movw   $0x3231,(%rbx)
lea    0xe49(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
mov    %ebp,%edi
call   1050 <rm_shm@plt>
cmpb   $0x31,(%rbx)
jne    1200 <main+0x90>
cmpb   $0x32,0x1(%rbx)
je     1215 <main+0xa5>
lea    0xe53(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <tst_resm@plt>
lea    0xe6b(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   1060 <perror@plt>
mov    (%r14),%rcx
lea    0xdbe(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <tst_resm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
jmp    11ae <main+0x3e>
lea    0xdc4(%rip),%rdi
call   1060 <perror@plt>
mov    (%r14),%rcx
lea    0xdbb(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <tst_resm@plt>
mov    %ebp,%edi
call   1050 <rm_shm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
jmp    11d9 <main+0x69>

LLVM IR (-O0):
@errno = global i64 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [39 x i8] c"shmget Failed: shmid = %d, errno = %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [38 x i8] c"shmat Failed: shmid = %d, errno = %d\0A\00"
@.str.4 = [45 x i8] c"Error in shared memory contents: shmid = %d\0A\00"
@.str.5 = [31 x i8] c"Correct shared memory contents\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
store i64 0, i64* @errno
%5 = call i64 (...) @getpid()
store i64 %5, i64* %4
%6 = load i64, i64* %4
%7 = load i32, i32* @IPC_CREAT
%8 = or i32 %7, 438
%9 = call i32 @shmget(i64 %6, i32 16384, i32 %8)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %2
%14 = load i64, i64* @errno
%15 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %13, i64 %14)
%16 = call i32 (...) @tst_exit()
br label %17
17: ; preds = %11, %0
%18 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = call i64 @shmat(i32 %19, i8* inttoptr (i64 524288 to i8*), i32 0)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %32
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %2
%27 = load i64, i64* @errno
%28 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %26, i64 %27)
%29 = load i32, i32* %2
%30 = call i32 @rm_shm(i32 %29)
%31 = call i32 (...) @tst_exit()
br label %32
32: ; preds = %24, %17
%33 = load i8*, i8** %3
store i8 49, i8* %33
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 1
store i8 50, i8* %35
%36 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %2
%38 = call i32 @rm_shm(i32 %37)
%39 = load i8*, i8** %3
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 49
br i1 %42, label %49, label %43
43: ; preds = %32
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 50
br i1 %48, label %49, label %52
49: ; preds = %43, %32
%50 = load i32, i32* %2
%51 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %49, %43
%53 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%54 = call i32 (...) @tst_exit()
ret i32 0
}

LLVM IR (-O2):
@errno = global i64 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [39 x i8] c"shmget Failed: shmid = %d, errno = %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [38 x i8] c"shmat Failed: shmid = %d, errno = %d\0A\00"
@.str.4 = [45 x i8] c"Error in shared memory contents: shmid = %d\0A\00"
@.str.5 = [31 x i8] c"Correct shared memory contents\00"
define i32 @main() {
store i64 0, i64* @errno
%1 = tail call i64 (...) @getpid()
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 438
%4 = tail call i32 @shmget(i64 %1, i32 16384, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = load i64, i64* @errno
%9 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %4, i64 %8)
%10 = tail call i32 (...) @tst_exit()
br label %11
11: ; preds = %6, %0
%12 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i64 @shmat(i32 %4, i8* inttoptr (i64 524288 to i8*), i32 0)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = load i64, i64* @errno
%19 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %4, i64 %18)
%20 = tail call i32 @rm_shm(i32 %4)
%21 = tail call i32 (...) @tst_exit()
br label %22
22: ; preds = %16, %11
store i8 49, i8* %14
%23 = getelementptr inbounds i8, i8* %14, i64 1
store i8 50, i8* %23
%24 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @rm_shm(i32 %4)
%26 = load i8, i8* %14
%27 = icmp eq i8 %26, 49
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8, i8* %23
%30 = icmp eq i8 %29, 50
br i1 %30, label %33, label %31
31: ; preds = %28, %22
%32 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %4)
br label %33
33: ; preds = %31, %28
%34 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%35 = tail call i32 (...) @tst_exit()
ret i32 0
}