#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 */
struct utsname {
  char *nodename;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
scalar_t__ uname(struct utsname *);
void util_get_safe_node_name(char *dst, size_t len) {
  struct utsname uname_info;
  char *p;
  assert(len >= 2);
  dst[len - 1] = '\0';
  if (uname(&uname_info) != 0) {
    perror("uname");
    strncpy(dst, "uname_2_failed", len - 1);
    return;
  }
  strncpy(dst, uname_info.nodename, len - 1);
  p = dst;
  while (*p != '\0') {
    if (!(*p == '-' || *p == '_' || *p == '.' || (*p >= '0' && *p <= '9') ||
          (*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z'))) {
      *p = '_';
    }
    ++p;
  }
}


x86 assembly (clang, -O0):
<util_get_safe_node_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x2,-0x10(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x18(%rbp),%rdi
call   1040 <uname@plt>
cmp    $0x0,%rax
je     11ae <util_get_safe_node_name+0x6e>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
lea    0xe62(%rip),%rsi
call   1030 <strncpy@plt>
jmp    1283 <util_get_safe_node_name+0x143>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1283 <util_get_safe_node_name+0x143>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     1272 <util_get_safe_node_name+0x132>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
je     1272 <util_get_safe_node_name+0x132>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1272 <util_get_safe_node_name+0x132>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     122b <util_get_safe_node_name+0xeb>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    1272 <util_get_safe_node_name+0x132>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x41,%eax
jl     124b <util_get_safe_node_name+0x10b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5a,%eax
jle    1272 <util_get_safe_node_name+0x132>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
jl     126b <util_get_safe_node_name+0x12b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7a,%eax
jle    1272 <util_get_safe_node_name+0x132>
mov    -0x20(%rbp),%rax
movb   $0x5f,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11cb <util_get_safe_node_name+0x8b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_get_safe_node_name>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
cmp    $0x2,%rsi
setae  %dil
call   1050 <assert@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
add    $0xffffffffffffffff,%r14
mov    %rsp,%rdi
call   1040 <uname@plt>
test   %rax,%rax
jne    11d1 <util_get_safe_node_name+0x91>
mov    (%rsp),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movabs $0x4000000000003,%rax
jmp    1194 <util_get_safe_node_name+0x54>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
movzbl (%rbx),%ecx
lea    -0x2d(%rcx),%edx
cmp    $0x32,%dl
ja     11a8 <util_get_safe_node_name+0x68>
movzbl %dl,%edx
bt     %rdx,%rax
jb     1190 <util_get_safe_node_name+0x50>
test   %cl,%cl
je     11c9 <util_get_safe_node_name+0x89>
lea    -0x30(%rcx),%edx
cmp    $0xa,%dl
jb     1190 <util_get_safe_node_name+0x50>
lea    -0x41(%rcx),%edx
cmp    $0x1a,%dl
jb     1190 <util_get_safe_node_name+0x50>
add    $0x9f,%cl
cmp    $0x1a,%cl
jb     1190 <util_get_safe_node_name+0x50>
movb   $0x5f,(%rbx)
jmp    1190 <util_get_safe_node_name+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
lea    0xe22(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strncpy@plt>

LLVM IR (-O0):
%struct.utsname = type { i8* }
@.str = [6 x i8] c"uname\00"
@.str.1 = [15 x i8] c"uname_2_failed\00"
define void @util_get_safe_node_name(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.utsname
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = icmp uge i64 %7, 2
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8*, i8** %3
%12 = load i64, i64* %4
%13 = sub i64 %12, 1
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 0, i8* %14
%15 = call i64 @uname(%struct.utsname* %5)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** %3
%20 = load i64, i64* %4
%21 = sub i64 %20, 1
%22 = call i32 @strncpy(i8* %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %21)
br label %86
23: ; preds = %2
%24 = load i8*, i8** %3
%25 = getelementptr inbounds %struct.utsname, %struct.utsname* %5, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = load i64, i64* %4
%28 = sub i64 %27, 1
%29 = call i32 @strncpy(i8* %24, i8* %26, i64 %28)
%30 = load i8*, i8** %3
store i8* %30, i8** %6
br label %31
31: ; preds = %83, %23
%32 = load i8*, i8** %6
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %86
36: ; preds = %31
%37 = load i8*, i8** %6
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 45
br i1 %40, label %83, label %41
41: ; preds = %36
%42 = load i8*, i8** %6
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 95
br i1 %45, label %83, label %46
46: ; preds = %41
%47 = load i8*, i8** %6
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 46
br i1 %50, label %83, label %51
51: ; preds = %46
%52 = load i8*, i8** %6
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp sge i32 %54, 48
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i8*, i8** %6
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp sle i32 %59, 57
br i1 %60, label %83, label %61
61: ; preds = %56, %51
%62 = load i8*, i8** %6
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp sge i32 %64, 65
br i1 %65, label %66, label %71
66: ; preds = %61
%67 = load i8*, i8** %6
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp sle i32 %69, 90
br i1 %70, label %83, label %71
71: ; preds = %66, %61
%72 = load i8*, i8** %6
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp sge i32 %74, 97
br i1 %75, label %76, label %81
76: ; preds = %71
%77 = load i8*, i8** %6
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp sle i32 %79, 122
br i1 %80, label %83, label %81
81: ; preds = %76, %71
%82 = load i8*, i8** %6
store i8 95, i8* %82
br label %83
83: ; preds = %81, %76, %66, %56, %46, %41, %36
%84 = load i8*, i8** %6
%85 = getelementptr inbounds i8, i8* %84, i32 1
store i8* %85, i8** %6
br label %31
86: ; preds = %17, %31
ret void
}

LLVM IR (-O2):
%struct.utsname = type { i8* }
@.str = [6 x i8] c"uname\00"
@.str.1 = [15 x i8] c"uname_2_failed\00"
define void @util_get_safe_node_name(i8* %0, i64 %1) {
%3 = alloca %struct.utsname
%4 = bitcast %struct.utsname* %3 to i8*
%5 = icmp ugt i64 %1, 1
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = add i64 %1, -1
%9 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 0, i8* %9
%10 = call i64 @uname(%struct.utsname* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @strncpy(i8* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %8)
br label %34
15: ; preds = %2
%16 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = tail call i32 @strncpy(i8* %0, i8* %17, i64 %8)
br label %19
19: ; preds = %32, %15
%20 = phi i8* [ %0, %15 ], [ %33, %32 ]
%21 = load i8, i8* %20
switch i8 %21, label %22 [
i8 0, label %34
i8 45, label %32
i8 95, label %32
i8 46, label %32
]
22: ; preds = %19
%23 = add i8 %21, -48
%24 = icmp ult i8 %23, 10
%25 = add i8 %21, -65
%26 = icmp ult i8 %25, 26
%27 = or i1 %24, %26
%28 = add i8 %21, -97
%29 = icmp ult i8 %28, 26
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %32, label %31
31: ; preds = %22
store i8 95, i8* %20
br label %32
32: ; preds = %22, %19, %19, %19, %31
%33 = getelementptr inbounds i8, i8* %20, i64 1
br label %19
34: ; preds = %19, %12
ret void
}