#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*/ EX_CONFIG;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ LOG_CRIT;
int /*<<< orphan*/ LOG_EMERG;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ isalnum(char);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ syslog_syserror(int /*<<< orphan*/, char *);
char *regex_escape_ifname(char *ifname) {
  size_t periods = 0;
  size_t i = 0;
  char c = '\0';
  while ((c = ifname[i++]) != '\0') {
    if (c == '.') {
      periods++;
    } else if (!isalnum(c)) {
      syslog(LOG_CRIT, "Non-alphanumeric characters other than period aren't "
                       "supposed to be part of a network interface name");
      exit(EX_CONFIG);
    }
  }
  if (periods > 0) {
    size_t j = 0;
    char *result = (char *)malloc(i + periods + 1);
    if (result == NULL) {
      syslog_syserror(LOG_EMERG, "Unable to allocate memory");
      exit(EX_OSERR);
    }
    i = 0;
    while ((c = ifname[i++]) != '\0') {
      if (c == '.') {
        result[j++] = '\\';
      }
      result[j++] = c;
    }
    result[j] = '\0';
    return result;
  } else {
    return ifname;
  }
}


x86 assembly (clang, -O0):
<regex_escape_ifname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     11f2 <regex_escape_ifname+0xa2>
movsbl -0x21(%rbp),%eax
cmp    $0x2e,%eax
jne    11b3 <regex_escape_ifname+0x63>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11ed <regex_escape_ifname+0x9d>
movsbl -0x21(%rbp),%edi
call   1030 <isalnum@plt>
cmp    $0x0,%eax
jne    11e8 <regex_escape_ifname+0x98>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1050 <syslog@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11ed <regex_escape_ifname+0x9d>
jmp    1170 <regex_escape_ifname+0x20>
cmpq   $0x0,-0x18(%rbp)
jbe    12d0 <regex_escape_ifname+0x180>
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1248 <regex_escape_ifname+0xf8>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1060 <syslog_syserror@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     12b7 <regex_escape_ifname+0x167>
movsbl -0x21(%rbp),%eax
cmp    $0x2e,%eax
jne    1299 <regex_escape_ifname+0x149>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5c,(%rax,%rcx,1)
mov    -0x21(%rbp),%dl
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1250 <regex_escape_ifname+0x100>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d8 <regex_escape_ifname+0x188>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<regex_escape_ifname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %r14d,%r14d
xor    %ebx,%ebx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl -0x1(%r15,%rbx,1),%eax
test   %al,%al
je     1196 <regex_escape_ifname+0x46>
cmp    $0x2e,%al
je     1190 <regex_escape_ifname+0x40>
movsbl %al,%edi
call   1030 <isalnum@plt>
add    $0x1,%rbx
test   %eax,%eax
jne    1170 <regex_escape_ifname+0x20>
jmp    11f0 <regex_escape_ifname+0xa0>
add    $0x1,%r14
jmp    115d <regex_escape_ifname+0xd>
test   %r14,%r14
je     11e7 <regex_escape_ifname+0x97>
lea    (%r14,%rbx,1),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1213 <regex_escape_ifname+0xc3>
xor    %ecx,%ecx
jmp    11d3 <regex_escape_ifname+0x83>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movb   $0x5c,(%rax,%rcx,1)
add    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
add    $0x1,%rcx
add    $0x1,%r15
movzbl (%r15),%edx
cmp    $0x2e,%dl
je     11c0 <regex_escape_ifname+0x70>
test   %dl,%dl
jne    11c8 <regex_escape_ifname+0x78>
movb   $0x0,(%rax,%rcx,1)
mov    %rax,%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1050 <syslog@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1060 <syslog_syserror@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@LOG_CRIT = global i32 0
@.str = [101 x i8] c"Non-alphanumeric characters other than period aren't supposed to be part of a network interface name\00"
@EX_CONFIG = global i32 0
@LOG_EMERG = global i32 0
@.str.1 = [26 x i8] c"Unable to allocate memory\00"
@EX_OSERR = global i32 0
define i8* @regex_escape_ifname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %3
store i64 0, i64* %4
store i64 0, i64* %5
store i8 0, i8* %6
br label %9
9: ; preds = %34, %1
%10 = load i8*, i8** %3
%11 = load i64, i64* %5
%12 = add i64 %11, 1
store i64 %12, i64* %5
%13 = getelementptr inbounds i8, i8* %10, i64 %11
%14 = load i8, i8* %13
store i8 %14, i8* %6
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %9
%18 = load i8, i8* %6
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 46
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i64, i64* %4
%23 = add i64 %22, 1
store i64 %23, i64* %4
br label %34
24: ; preds = %17
%25 = load i8, i8* %6
%26 = call i32 @isalnum(i8 signext %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32, i32* @LOG_CRIT
%30 = call i32 @syslog(i32 %29, i8* getelementptr inbounds ([101 x i8], [101 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EX_CONFIG
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
br label %34
34: ; preds = %33, %21
br label %9
35: ; preds = %9
%36 = load i64, i64* %4
%37 = icmp ugt i64 %36, 0
br i1 %37, label %38, label %81
38: ; preds = %35
store i64 0, i64* %7
%39 = load i64, i64* %5
%40 = load i64, i64* %4
%41 = add i64 %39, %40
%42 = add i64 %41, 1
%43 = call i64 @malloc(i64 %42)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %52
47: ; preds = %38
%48 = load i32, i32* @LOG_EMERG
%49 = call i32 @syslog_syserror(i32 %48, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%50 = load i32, i32* @EX_OSERR
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %38
store i64 0, i64* %5
br label %53
53: ; preds = %70, %52
%54 = load i8*, i8** %3
%55 = load i64, i64* %5
%56 = add i64 %55, 1
store i64 %56, i64* %5
%57 = getelementptr inbounds i8, i8* %54, i64 %55
%58 = load i8, i8* %57
store i8 %58, i8* %6
%59 = sext i8 %58 to i32
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %76
61: ; preds = %53
%62 = load i8, i8* %6
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 46
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = load i8*, i8** %8
%67 = load i64, i64* %7
%68 = add i64 %67, 1
store i64 %68, i64* %7
%69 = getelementptr inbounds i8, i8* %66, i64 %67
store i8 92, i8* %69
br label %70
70: ; preds = %65, %61
%71 = load i8, i8* %6
%72 = load i8*, i8** %8
%73 = load i64, i64* %7
%74 = add i64 %73, 1
store i64 %74, i64* %7
%75 = getelementptr inbounds i8, i8* %72, i64 %73
store i8 %71, i8* %75
br label %53
76: ; preds = %53
%77 = load i8*, i8** %8
%78 = load i64, i64* %7
%79 = getelementptr inbounds i8, i8* %77, i64 %78
store i8 0, i8* %79
%80 = load i8*, i8** %8
store i8* %80, i8** %2
br label %83
81: ; preds = %35
%82 = load i8*, i8** %3
store i8* %82, i8** %2
br label %83
83: ; preds = %81, %76
%84 = load i8*, i8** %2
ret i8* %84
}

LLVM IR (-O2):
@LOG_CRIT = global i32 0
@.str = [101 x i8] c"Non-alphanumeric characters other than period aren't supposed to be part of a network interface name\00"
@EX_CONFIG = global i32 0
@LOG_EMERG = global i32 0
@.str.1 = [26 x i8] c"Unable to allocate memory\00"
@EX_OSERR = global i32 0
define i8* @regex_escape_ifname(i8* %0) {
br label %2
2: ; preds = %10, %1
%3 = phi i64 [ %11, %10 ], [ 0, %1 ]
%4 = phi i64 [ %7, %10 ], [ 0, %1 ]
br label %5
5: ; preds = %2, %12
%6 = phi i64 [ %7, %12 ], [ %4, %2 ]
%7 = add i64 %6, 1
%8 = getelementptr inbounds i8, i8* %0, i64 %6
%9 = load i8, i8* %8
switch i8 %9, label %12 [
i8 0, label %20
i8 46, label %10
]
10: ; preds = %5
%11 = add i64 %3, 1
br label %2
12: ; preds = %5
%13 = tail call i32 @isalnum(i8 signext %9)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %5
15: ; preds = %12
%16 = load i32, i32* @LOG_CRIT
%17 = tail call i32 @syslog(i32 %16, i8* getelementptr inbounds ([101 x i8], [101 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EX_CONFIG
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %5
%21 = icmp eq i64 %3, 0
br i1 %21, label %48, label %22
22: ; preds = %20
%23 = add i64 %3, 1
%24 = add i64 %23, %7
%25 = tail call i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
%27 = icmp eq i64 %25, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i32, i32* @LOG_EMERG
%30 = tail call i32 @syslog_syserror(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EX_OSERR
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %22, %42
%34 = phi i64 [ %36, %42 ], [ 0, %22 ]
%35 = phi i64 [ %44, %42 ], [ 0, %22 ]
%36 = add i64 %34, 1
%37 = getelementptr inbounds i8, i8* %0, i64 %34
%38 = load i8, i8* %37
switch i8 %38, label %42 [
i8 0, label %46
i8 46, label %39
]
39: ; preds = %33
%40 = add i64 %35, 1
%41 = getelementptr inbounds i8, i8* %26, i64 %35
store i8 92, i8* %41
br label %42
42: ; preds = %33, %39
%43 = phi i64 [ %40, %39 ], [ %35, %33 ]
%44 = add i64 %43, 1
%45 = getelementptr inbounds i8, i8* %26, i64 %43
store i8 %38, i8* %45
br label %33
46: ; preds = %33
%47 = getelementptr inbounds i8, i8* %26, i64 %35
store i8 0, i8* %47
br label %48
48: ; preds = %20, %46
%49 = phi i8* [ %26, %46 ], [ %0, %20 ]
ret i8* %49
}