#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*/ ENAMETOOLONG;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int snprintf(char *, int, char *, char const *);
char *get_shm_name(const char *name) {
  char *path;
  int i;
  while (*name == '/')
    ++name;
  path = malloc(255);
  if (path == NULL)
    return NULL;
  i = snprintf(path, 255,
               "/dev/shm/"
               "%s",
               name);
  if (i < 0) {
    free(path);
    return NULL;
  } else if (i >= 255) {
    free(path);
    __set_errno(ENAMETOOLONG);
    return NULL;
  }
  return path;
}


x86 assembly (clang, -O0):
<get_shm_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    116d <get_shm_name+0x2d>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114c <get_shm_name+0xc>
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1193 <get_shm_name+0x53>
movq   $0x0,-0x8(%rbp)
jmp    120d <get_shm_name+0xcd>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xe59(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cf <get_shm_name+0x8f>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    120d <get_shm_name+0xcd>
cmpl   $0xff,-0x1c(%rbp)
jl     1200 <get_shm_name+0xc0>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    120d <get_shm_name+0xcd>
jmp    1205 <get_shm_name+0xc5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_shm_name>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1150 <get_shm_name+0x10>
mov    $0xff,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ae <get_shm_name+0x6e>
mov    %rax,%r14
lea    0xe8d(%rip),%rdx
mov    %rax,%rdi
mov    $0xff,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
test   %eax,%eax
js     11a6 <get_shm_name+0x66>
cmp    $0xff,%eax
jb     11b1 <get_shm_name+0x71>
mov    %r14,%rdi
call   1040 <free@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    11ae <get_shm_name+0x6e>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/shm/%s\00"
@ENAMETOOLONG = global i32 0
define i8* @get_shm_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
br label %6
6: ; preds = %11, %1
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 47
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %3
br label %6
14: ; preds = %6
%15 = call i8* @malloc(i32 255)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i8* null, i8** %2
br label %39
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = load i8*, i8** %3
%22 = call i32 @snprintf(i8* %20, i32 255, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
store i8* null, i8** %2
br label %39
28: ; preds = %19
%29 = load i32, i32* %5
%30 = icmp sge i32 %29, 255
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* @ENAMETOOLONG
%35 = call i32 @__set_errno(i32 %34)
store i8* null, i8** %2
br label %39
36: ; preds = %28
br label %37
37: ; preds = %36
%38 = load i8*, i8** %4
store i8* %38, i8** %2
br label %39
39: ; preds = %37, %31, %25, %18
%40 = load i8*, i8** %2
ret i8* %40
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/shm/%s\00"
@ENAMETOOLONG = global i32 0
define i8* @get_shm_name(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %6, %2 ]
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 47
%6 = getelementptr inbounds i8, i8* %3, i64 1
br i1 %5, label %2, label %7
7: ; preds = %2
%8 = tail call dereferenceable_or_null(255) i8* @malloc(i32 255)
%9 = icmp eq i8* %8, null
br i1 %9, label %21, label %10
10: ; preds = %7
%11 = tail call i32 @snprintf(i8* %8, i32 255, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @free(i8* %8)
br label %21
15: ; preds = %10
%16 = icmp ugt i32 %11, 254
br i1 %16, label %17, label %21
17: ; preds = %15
%18 = tail call i32 @free(i8* %8)
%19 = load i32, i32* @ENAMETOOLONG
%20 = tail call i32 @__set_errno(i32 %19)
br label %21
21: ; preds = %15, %7, %17, %13
%22 = phi i8* [ null, %13 ], [ null, %17 ], [ null, %7 ], [ %8, %15 ]
ret i8* %22
}