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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, size_t, int);
int /*<<< orphan*/ usleep(int);
void *wait_remote_shm(key_t k, size_t s) {
  void *p;
  int id;
  while (1) {
    id = shmget(k, s, 0644);
    if (id == -1) {
      if (errno == ENOENT) {
        usleep(10);
      } else {
        perror("shmget");
        return NULL;
      }
    } else {
      break;
    }
  }
  p = shmat(id, NULL, 0);
  if (p == (void *)-1) {
    perror("shmat");
    return NULL;
  }
  return p;
}


x86 assembly (clang, -O0):
<wait_remote_shm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1a4,%edx
call   1050 <shmget@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b4 <wait_remote_shm+0x74>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <wait_remote_shm+0x56>
mov    $0xa,%edi
call   1060 <usleep@plt>
jmp    11af <wait_remote_shm+0x6f>
lea    0xe63(%rip),%rdi
call   1030 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1202 <wait_remote_shm+0xc2>
jmp    11b9 <wait_remote_shm+0x79>
jmp    11be <wait_remote_shm+0x7e>
jmp    114f <wait_remote_shm+0xf>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11fa <wait_remote_shm+0xba>
lea    0xe1f(%rip),%rdi
call   1030 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1202 <wait_remote_shm+0xc2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_remote_shm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1a4,%edx
call   1050 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11a7 <wait_remote_shm+0x67>
mov    0x2e7d(%rip),%r15
mov    0x2e66(%rip),%r12
lea    0xe8f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11c8 <wait_remote_shm+0x88>
mov    $0xa,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1a4,%edx
call   1050 <shmget@plt>
cmp    $0xffffffff,%eax
je     1180 <wait_remote_shm+0x40>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11c1 <wait_remote_shm+0x81>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3f(%rip),%r14
mov    %r14,%rdi
call   1030 <perror@plt>
xor    %eax,%eax
jmp    11b8 <wait_remote_shm+0x78>

LLVM IR (-O0):
@errno = global i64 0
@ENOENT = global i64 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i8* @wait_remote_shm(i32 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
br label %8
8: ; preds = %2, %24
%9 = load i32, i32* %4
%10 = load i64, i64* %5
%11 = call i32 @shmget(i32 %9, i64 %10, i32 420)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %23
14: ; preds = %8
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @usleep(i32 10)
br label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %34
22: ; preds = %18
br label %24
23: ; preds = %8
br label %25
24: ; preds = %22
br label %8
25: ; preds = %23
%26 = load i32, i32* %7
%27 = call i8* @shmat(i32 %26, i32* null, i32 0)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp eq i8* %28, inttoptr (i64 -1 to i8*)
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %34
32: ; preds = %25
%33 = load i8*, i8** %6
store i8* %33, i8** %3
br label %34
34: ; preds = %32, %30, %20
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@errno = global i64 0
@ENOENT = global i64 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i8* @wait_remote_shm(i32 %0, i64 %1) {
%3 = tail call i32 @shmget(i32 %0, i64 %1, i32 420)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %13
5: ; preds = %2, %9
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %17
9: ; preds = %5
%10 = tail call i32 @usleep(i32 10)
%11 = tail call i32 @shmget(i32 %0, i64 %1, i32 420)
%12 = icmp eq i32 %11, -1
br i1 %12, label %5, label %13
13: ; preds = %9, %2
%14 = phi i32 [ %3, %2 ], [ %11, %9 ]
%15 = tail call i8* @shmat(i32 %14, i32* null, i32 0)
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %20
17: ; preds = %5, %13
%18 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %5 ]
%19 = tail call i32 @perror(i8* %18)
br label %20
20: ; preds = %17, %13
%21 = phi i8* [ %15, %13 ], [ null, %17 ]
ret i8* %21
}