#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 */

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *GetShm(int iKey, int iSize, int iFlag) {
  int iShmID;
  char *sShm;
  char sErrMsg[50];
  if ((iShmID = shmget(iKey, iSize, iFlag)) < 0) {
    sprintf(sErrMsg, "shmget %d %d", iKey, iSize);
    fprintf(stderr, "%s: %s", sErrMsg, strerror(errno));
    return NULL;
  }
  if ((sShm = shmat(iShmID, NULL, 0)) == (char *)-1) {
    fprintf(stderr, "shmat: %s", strerror(errno));
    return NULL;
  }
  return sShm;
}


x86 assembly (clang, -O0):
<GetShm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1050 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11d8 <GetShm+0x88>
lea    -0x60(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe74(%rip),%rsi
call   1060 <sprintf@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    123d <GetShm+0xed>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1235 <GetShm+0xe5>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    123d <GetShm+0xed>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetShm>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
call   1050 <shmget@plt>
test   %eax,%eax
js     11a7 <GetShm+0x57>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11ec <GetShm+0x9c>
mov    0x2e60(%rip),%rax
mov    (%rax),%ebp
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe7d(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11ec <GetShm+0x9c>
lea    0xe52(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1060 <sprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe32(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %rbx,%rax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"shmget %d %d\00"
@stderr = global i32 0
@.str.1 = [7 x i8] c"%s: %s\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"shmat: %s\00"
define i8* @GetShm(i32 %0, i32 %1, i32 %2) {
%4 = alloca [50 x i8]
%5 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @shmget(i32 %0, i32 %1, i32 %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %3
%9 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %12)
br label %22
14: ; preds = %3
%15 = tail call i8* @shmat(i32 %6, i32* null, i32 0)
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %14, %17, %8
%23 = phi i8* [ null, %8 ], [ null, %17 ], [ %15, %14 ]
ret i8* %23
}