#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 */
scalar_t__ EINTR;
int IPC_CREAT;
scalar_t__ errno;
int fork();
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 *, ...);
scalar_t__ wait(int *);
int main(void) {
  char *cp = NULL;
  int shmid, pid, status;
  key_t key;
  key = (key_t)getpid();
  errno = 0;
  if ((shmid = shmget(key, 16 * 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    tst_resm(1, "Error: shmget: shmid = %d, errno = %d\n", shmid, errno);
    tst_exit();
  }
  cp = (char *)shmat(shmid, (void *)0x80000, 0);
  if (cp == (char *)-1) {
    perror("shmat");
    tst_resm(1, "Error: shmat: shmid = %d, errno = %d\n", shmid, errno);
    rm_shm(shmid);
    tst_exit();
  }
  *cp = '1';
  *(cp + 1) = '2';
  tst_resm(0, "shmget,shmat");
  pid = fork();
  switch (pid) {
  case -1:
    tst_resm(2, "fork failed");
    tst_exit();
  case 0:
    if (*cp != '1') {
      tst_resm(1, "Error: not 1\n");
    }
    if (*(cp + 1) != '2') {
      tst_resm(1, "Error: not 2\n");
    }
    tst_exit();
  }
  while (wait(&status) < 0 && errno == EINTR)
    ;
  tst_resm(0, "cp & cp+1 correct");
  rm_shm(shmid);
  tst_exit();
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e27(%rip),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x4000,%esi
call   1080 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    121c <main+0x8c>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edx
mov    0x2de1(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    $0x0,%al
call   1040 <tst_exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x80000,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    127b <main+0xeb>
lea    0xe14(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edx
mov    0x2d8a(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    -0x14(%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)
lea    0xdc3(%rip),%rsi
xor    %edi,%edi
mov    %dil,%al
mov    %al,-0x2d(%rbp)
call   1090 <tst_resm@plt>
mov    -0x2d(%rbp),%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     12cd <main+0x13d>
jmp    12bd <main+0x12d>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     12e7 <main+0x157>
jmp    1335 <main+0x1a5>
mov    $0x2,%edi
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    $0x0,%al
call   1040 <tst_exit@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
je     130a <main+0x17a>
mov    $0x1,%edi
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x32,%eax
je     132e <main+0x19e>
mov    $0x1,%edi
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    $0x0,%al
call   1040 <tst_exit@plt>
jmp    133a <main+0x1aa>
lea    -0x1c(%rbp),%rdi
call   10a0 <wait@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x2e(%rbp)
jge    136f <main+0x1df>
mov    0x2c84(%rip),%rax
mov    (%rax),%rax
mov    0x2c72(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2e(%rbp)
mov    -0x2e(%rbp),%al
test   $0x1,%al
jne    137f <main+0x1ef>
jmp    1384 <main+0x1f4>
jmp    133a <main+0x1aa>
xor    %edi,%edi
lea    0xcfc(%rip),%rsi
mov    $0x0,%al
call   1090 <tst_resm@plt>
mov    -0x14(%rbp),%edi
call   1050 <rm_shm@plt>
mov    $0x0,%al
call   1040 <tst_exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2e3b(%rip),%r15
movq   $0x0,(%r15)
mov    0x2e1d(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %rax,%rdi
mov    $0x4000,%esi
call   1080 <shmget@plt>
mov    %eax,%r14d
test   %eax,%eax
js     12c2 <main+0x132>
mov    $0x80000,%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     12f3 <main+0x163>
movw   $0x3231,(%rbx)
lea    0xe5a(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     122d <main+0x9d>
cmp    $0xffffffff,%eax
jne    1265 <main+0xd5>
lea    0xe47(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
cmpb   $0x31,(%rbx)
je     1245 <main+0xb5>
lea    0xe34(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
cmpb   $0x32,0x1(%rbx)
je     125e <main+0xce>
lea    0xe29(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
lea    0x4(%rsp),%rbx
mov    0x2d67(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <wait@plt>
test   %rax,%rax
jns    1296 <main+0x106>
mov    0x0(%rbp),%rax
cmp    %rax,(%r15)
je     1280 <main+0xf0>
lea    0xdec(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <tst_resm@plt>
mov    %r14d,%edi
call   1050 <rm_shm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd37(%rip),%rdi
call   1060 <perror@plt>
mov    (%r15),%rcx
lea    0xd2f(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1090 <tst_resm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
jmp    11d2 <main+0x42>
lea    0xd61(%rip),%rdi
call   1060 <perror@plt>
mov    (%r15),%rcx
lea    0xd25(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1090 <tst_resm@plt>
mov    %r14d,%edi
call   1050 <rm_shm@plt>
xor    %eax,%eax
call   1040 <tst_exit@plt>
jmp    11ee <main+0x5e>

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"Error: shmget: shmid = %d, errno = %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [38 x i8] c"Error: shmat: shmid = %d, errno = %d\0A\00"
@.str.4 = [13 x i8] c"shmget,shmat\00"
@.str.5 = [12 x i8] c"fork failed\00"
@.str.6 = [14 x i8] c"Error: not 1\0A\00"
@.str.7 = [14 x i8] c"Error: not 2\0A\00"
@EINTR = global i64 0
@.str.8 = [18 x i8] c"cp & cp+1 correct\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
store i8* null, i8** %2
%7 = call i64 (...) @getpid()
store i64 %7, i64* %6
store i64 0, i64* @errno
%8 = load i64, i64* %6
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = call i32 @shmget(i64 %8, i32 16384, i32 %10)
store i32 %11, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* %3
%16 = load i64, i64* @errno
%17 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %15, i64 %16)
%18 = call i32 (...) @tst_exit()
br label %19
19: ; preds = %13, %0
%20 = load i32, i32* %3
%21 = call i64 @shmat(i32 %20, i8* inttoptr (i64 524288 to i8*), i32 0)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %2
%23 = load i8*, i8** %2
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %33
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %3
%28 = load i64, i64* @errno
%29 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %27, i64 %28)
%30 = load i32, i32* %3
%31 = call i32 @rm_shm(i32 %30)
%32 = call i32 (...) @tst_exit()
br label %33
33: ; preds = %25, %19
%34 = load i8*, i8** %2
store i8 49, i8* %34
%35 = load i8*, i8** %2
%36 = getelementptr inbounds i8, i8* %35, i64 1
store i8 50, i8* %36
%37 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 (...) @fork()
store i32 %38, i32* %4
%39 = load i32, i32* %4
switch i32 %39, label %60 [
i32 -1, label %40
i32 0, label %43
]
40: ; preds = %33
%41 = call i32 (i32, i8*, ...) @tst_resm(i32 2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 (...) @tst_exit()
br label %43
43: ; preds = %33, %40
%44 = load i8*, i8** %2
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 49
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = load i8*, i8** %2
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 50
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %56, %50
%59 = call i32 (...) @tst_exit()
br label %60
60: ; preds = %58, %33
br label %61
61: ; preds = %70, %60
%62 = call i64 @wait(i32* %5)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %61
%65 = load i64, i64* @errno
%66 = load i64, i64* @EINTR
%67 = icmp eq i64 %65, %66
br label %68
68: ; preds = %64, %61
%69 = phi i1 [ false, %61 ], [ %67, %64 ]
br i1 %69, label %70, label %71
70: ; preds = %68
br label %61
71: ; preds = %68
%72 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%73 = load i32, i32* %3
%74 = call i32 @rm_shm(i32 %73)
%75 = 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"Error: shmget: shmid = %d, errno = %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [38 x i8] c"Error: shmat: shmid = %d, errno = %d\0A\00"
@.str.4 = [13 x i8] c"shmget,shmat\00"
@.str.5 = [12 x i8] c"fork failed\00"
@.str.6 = [14 x i8] c"Error: not 1\0A\00"
@.str.7 = [14 x i8] c"Error: not 2\0A\00"
@EINTR = global i64 0
@.str.8 = [18 x i8] c"cp & cp+1 correct\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @getpid()
store i64 0, i64* @errno
%4 = load i32, i32* @IPC_CREAT
%5 = or i32 %4, 438
%6 = tail call i32 @shmget(i64 %3, i32 16384, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i64, i64* @errno
%11 = 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 %6, i64 %10)
%12 = tail call i32 (...) @tst_exit()
br label %13
13: ; preds = %8, %0
%14 = tail call i64 @shmat(i32 %6, i8* inttoptr (i64 524288 to i8*), i32 0)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = load i64, i64* @errno
%20 = 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 %6, i64 %19)
%21 = tail call i32 @rm_shm(i32 %6)
%22 = tail call i32 (...) @tst_exit()
br label %23
23: ; preds = %17, %13
store i8 49, i8* %15
%24 = getelementptr inbounds i8, i8* %15, i64 1
store i8 50, i8* %24
%25 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i32 (...) @fork()
switch i32 %26, label %42 [
i32 -1, label %27
i32 0, label %30
]
27: ; preds = %23
%28 = tail call i32 (i32, i8*, ...) @tst_resm(i32 2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%29 = tail call i32 (...) @tst_exit()
br label %30
30: ; preds = %23, %27
%31 = load i8, i8* %15
%32 = icmp eq i8 %31, 49
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = load i8, i8* %24
%37 = icmp eq i8 %36, 50
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = tail call i32 (...) @tst_exit()
br label %42
42: ; preds = %40, %23
br label %43
43: ; preds = %42, %43
%44 = call i64 @wait(i32* %1)
%45 = icmp slt i64 %44, 0
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINTR
%48 = icmp eq i64 %46, %47
%49 = select i1 %45, i1 %48, i1 false
br i1 %49, label %43, label %50
50: ; preds = %43
%51 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%52 = call i32 @rm_shm(i32 %6)
%53 = call i32 (...) @tst_exit()
ret i32 0
}