#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 */
int IPC_CREAT;
int /*<<< orphan*/ perror(char *);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, size_t, int);
void *create_shm(key_t k, size_t s) {
  int id;
  void *p;
  id = shmget(k, s, IPC_CREAT | 0644);
  if (id == -1) {
    perror("shmget");
    return NULL;
  }
  p = shmat(id, NULL, 0);
  if (p == (void *)-1) {
    perror("shmat");
    return NULL;
  }
  return p;
}


x86 assembly (clang, -O0):
<create_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    0x2e93(%rip),%rax
mov    (%rax),%edx
or     $0x1a4,%edx
call   1050 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1180 <create_shm+0x50>
lea    0xe92(%rip),%rdi
call   1030 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <create_shm+0x94>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    11bc <create_shm+0x8c>
lea    0xe5d(%rip),%rdi
call   1030 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <create_shm+0x94>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_shm>:
push   %rax
mov    0x2ea8(%rip),%rax
mov    $0x1a4,%edx
or     (%rax),%edx
call   1050 <shmget@plt>
cmp    $0xffffffff,%eax
je     115c <create_shm+0x2c>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1165 <create_shm+0x35>
pop    %rcx
ret
lea    0xe9d(%rip),%rdi
jmp    116c <create_shm+0x3c>
lea    0xe9b(%rip),%rdi
call   1030 <perror@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i8* @create_shm(i32 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %4
store i64 %1, i64* %5
%8 = load i32, i32* %4
%9 = load i64, i64* %5
%10 = load i32, i32* @IPC_CREAT
%11 = or i32 %10, 420
%12 = call i32 @shmget(i32 %8, i64 %9, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %26
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i8* @shmat(i32 %18, i32* null, i32 0)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, inttoptr (i64 -1 to i8*)
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = 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 %26
24: ; preds = %17
%25 = load i8*, i8** %7
store i8* %25, i8** %3
br label %26
26: ; preds = %24, %22, %15
%27 = load i8*, i8** %3
ret i8* %27
}

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