#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*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void getShName(char *SerialNumber, char *semaphoreName) {
  strcpy(semaphoreName, "HATSHMEM_");
  if (SerialNumber != NULL && strlen(SerialNumber) > 0) {
    if (strlen(semaphoreName) + strlen(SerialNumber) + 1 > 30) {
      printf("SN too long\n");
    } else {
      strncat(semaphoreName, SerialNumber, 30 - strlen(semaphoreName) - 1);
      semaphoreName[30 - 1] = '\0';
    }
  }
}


x86 assembly (clang, -O0):
<getShName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x8(%rbp)
je     11f3 <getShName+0xb3>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    11f3 <getShName+0xb3>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cmp    $0x1e,%eax
jle    11b6 <getShName+0x76>
lea    0xe5e(%rip),%rdi
call   1050 <printf@plt>
jmp    11ee <getShName+0xae>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1e,%edx
sub    %eax,%edx
sub    $0x1,%edx
call   1060 <strncat@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1d(%rax)
jmp    11f3 <getShName+0xb3>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getShName>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
test   %rbx,%rbx
je     11b2 <getShName+0x72>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11b2 <getShName+0x72>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    %ebp,%eax
cmp    $0x1e,%eax
jl     1193 <getShName+0x53>
lea    0xe80(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <puts@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    $0x1d,%edx
sub    %eax,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <strncat@plt>
movb   $0x0,0x1d(%r14)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"HATSHMEM_\00"
@.str.1 = [13 x i8] c"SN too long\0A\00"
define void @getShName(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i8*, i8** %4
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %34
9: ; preds = %2
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %34
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %15, %17
%19 = add nsw i32 %18, 1
%20 = icmp sgt i32 %19, 30
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %33
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 30, %27
%29 = sub nsw i32 %28, 1
%30 = call i32 @strncat(i8* %24, i8* %25, i32 %29)
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 29
store i8 0, i8* %32
br label %33
33: ; preds = %23, %21
br label %34
34: ; preds = %33, %9, %2
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"HATSHMEM_\00"
@str = [12 x i8] c"SN too long\00"
define void @getShName(i8* %0, i8* %1) {
%3 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %0, null
br i1 %4, label %20, label %5
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %20
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %1)
%10 = tail call i32 @strlen(i8* %0)
%11 = add nsw i32 %10, %9
%12 = icmp sgt i32 %11, 29
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %20
15: ; preds = %8
%16 = tail call i32 @strlen(i8* %1)
%17 = sub i32 29, %16
%18 = tail call i32 @strncat(i8* %1, i8* %0, i32 %17)
%19 = getelementptr inbounds i8, i8* %1, i64 29
store i8 0, i8* %19
br label %20
20: ; preds = %13, %15, %5, %2
ret void
}