#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*/ EINVAL;
int /*<<< orphan*/ ENAMETOOLONG;
int NAME_MAX;
char *__strchrnul(char const *, char);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *__shm_mapname(const char *name, char *buf) {
  char *p;
  while (*name == '/')
    name++;
  if (*(p = __strchrnul(name, '/')) || p == name ||
      (p - name <= 2 && name[0] == '.' && p[-1] == '.')) {
    errno = EINVAL;
    return 0;
  }
  if (p - name > NAME_MAX) {
    errno = ENAMETOOLONG;
    return 0;
  }
  memcpy(buf, "/dev/shm/", 9);
  memcpy(buf + 9, name, p - name + 1);
  return buf;
}


x86 assembly (clang, -O0):
<__shm_mapname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1151 <__shm_mapname+0x31>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1130 <__shm_mapname+0x10>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <__strchrnul@plt>
mov    %rax,-0x20(%rbp)
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b3 <__shm_mapname+0x93>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11b3 <__shm_mapname+0x93>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x2,%rax
jg     11d2 <__shm_mapname+0xb2>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11d2 <__shm_mapname+0xb2>
mov    -0x20(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2e,%eax
jne    11d2 <__shm_mapname+0xb2>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1d(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    124e <__shm_mapname+0x12e>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    0x2ddc(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jle    120f <__shm_mapname+0xef>
mov    0x2dd9(%rip),%rax
mov    (%rax),%ecx
mov    0x2de0(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    124e <__shm_mapname+0x12e>
mov    -0x18(%rbp),%rdi
lea    0xde6(%rip),%rsi
mov    $0x9,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rdi
add    $0x9,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__shm_mapname>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r13
lea    -0x1(%rdi),%rbx
add    $0xfffffffe,%r13d
mov    %rbx,%r15
nopw   0x0(%rax,%rax,1)
add    $0x1,%r13
add    $0x1,%rbx
cmpb   $0x2f,0x1(%r15)
lea    0x1(%r15),%r15
je     1140 <__shm_mapname+0x20>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1030 <__strchrnul@plt>
cmp    %rax,%r15
je     1187 <__shm_mapname+0x67>
mov    %rax,%r12
cmpb   $0x0,(%rax)
jne    1187 <__shm_mapname+0x67>
mov    %r12,%rax
sub    %rbx,%rax
cmp    $0x2,%rax
jg     1190 <__shm_mapname+0x70>
cmpb   $0x2e,(%r15)
jne    1190 <__shm_mapname+0x70>
cmpb   $0x2e,-0x1(%r12)
jne    1190 <__shm_mapname+0x70>
mov    0x2e4a(%rip),%rax
jmp    11a6 <__shm_mapname+0x86>
mov    0x2e29(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jle    11b6 <__shm_mapname+0x96>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    0x2e31(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
jmp    11dc <__shm_mapname+0xbc>
lea    0xe43(%rip),%rsi
mov    %r14,%rdi
mov    $0x9,%edx
call   1040 <memcpy@plt>
lea    0x9(%r14),%rdi
sub    %r13d,%r12d
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@NAME_MAX = global i32 0
@ENAMETOOLONG = global i32 0
@.str = [10 x i8] c"/dev/shm/\00"
define i8* @__shm_mapname(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
br label %7
7: ; preds = %12, %2
%8 = load i8*, i8** %4
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 47
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %4
br label %7
15: ; preds = %7
%16 = load i8*, i8** %4
%17 = call i8* @__strchrnul(i8* %16, i8 signext 47)
store i8* %17, i8** %6
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %44, label %21
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = icmp eq i8* %22, %23
br i1 %24, label %44, label %25
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = load i8*, i8** %4
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = icmp sle i64 %30, 2
br i1 %31, label %32, label %46
32: ; preds = %25
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 46
br i1 %37, label %38, label %46
38: ; preds = %32
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 -1
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 46
br i1 %43, label %44, label %46
44: ; preds = %38, %21, %15
%45 = load i32, i32* @EINVAL
store i32 %45, i32* @errno
store i8* null, i8** %3
br label %72
46: ; preds = %38, %32, %25
%47 = load i8*, i8** %6
%48 = load i8*, i8** %4
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = load i32, i32* @NAME_MAX
%53 = sext i32 %52 to i64
%54 = icmp sgt i64 %51, %53
br i1 %54, label %55, label %57
55: ; preds = %46
%56 = load i32, i32* @ENAMETOOLONG
store i32 %56, i32* @errno
store i8* null, i8** %3
br label %72
57: ; preds = %46
%58 = load i8*, i8** %5
%59 = call i32 @memcpy(i8* %58, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%60 = load i8*, i8** %5
%61 = getelementptr inbounds i8, i8* %60, i64 9
%62 = load i8*, i8** %4
%63 = load i8*, i8** %6
%64 = load i8*, i8** %4
%65 = ptrtoint i8* %63 to i64
%66 = ptrtoint i8* %64 to i64
%67 = sub i64 %65, %66
%68 = add nsw i64 %67, 1
%69 = trunc i64 %68 to i32
%70 = call i32 @memcpy(i8* %61, i8* %62, i32 %69)
%71 = load i8*, i8** %5
store i8* %71, i8** %3
br label %72
72: ; preds = %57, %55, %44
%73 = load i8*, i8** %3
ret i8* %73
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@NAME_MAX = global i32 0
@ENAMETOOLONG = global i32 0
@.str = [10 x i8] c"/dev/shm/\00"
define i8* @__shm_mapname(i8* %0, i8* %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i8* [ %0, %2 ], [ %7, %3 ]
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 47
%7 = getelementptr inbounds i8, i8* %4, i64 1
br i1 %6, label %3, label %8
8: ; preds = %3
%9 = tail call i8* @__strchrnul(i8* %4, i8 signext 47)
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
%12 = icmp eq i8* %9, %4
%13 = or i1 %12, %11
br i1 %13, label %26, label %14
14: ; preds = %8
%15 = ptrtoint i8* %9 to i64
%16 = ptrtoint i8* %4 to i64
%17 = sub i64 %15, %16
%18 = icmp slt i64 %17, 3
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i8, i8* %4
%21 = icmp eq i8 %20, 46
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %9, i64 -1
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 46
br i1 %25, label %26, label %28
26: ; preds = %22, %8
%27 = load i32, i32* @EINVAL
store i32 %27, i32* @errno
br label %40
28: ; preds = %22, %19, %14
%29 = load i32, i32* @NAME_MAX
%30 = sext i32 %29 to i64
%31 = icmp sgt i64 %17, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = load i32, i32* @ENAMETOOLONG
store i32 %33, i32* @errno
br label %40
34: ; preds = %28
%35 = tail call i32 @memcpy(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%36 = getelementptr inbounds i8, i8* %1, i64 9
%37 = trunc i64 %17 to i32
%38 = add i32 %37, 1
%39 = tail call i32 @memcpy(i8* %36, i8* %4, i32 %38)
br label %40
40: ; preds = %34, %32, %26
%41 = phi i8* [ null, %26 ], [ null, %32 ], [ %1, %34 ]
ret i8* %41
}