#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*/ free(char *);
char *getcwd(char *, int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
char *strcat(char *, char const *);
char *strdup(char const *);
int strlen(char const *);
scalar_t__ strncmp(char *, char *, int);
char *get_serv(const char *ipath) {
  char *path = NULL;
  char *service = NULL;
  if (ipath[0] == '.') {
    path = malloc(sizeof(char) * (strlen(ipath) + 40 + 1));
    path = getcwd(path, 40);
    path = strcat(path, ipath + 1);
  } else {
    path = strdup(ipath);
  }
  if (strncmp("/etc/initng/", path, 12) == 0) {
    service = strdup(&path[12]);
    free(path);
  } else {
    printf("%s doesn't begin with /etc/initng/", path);
    free(path);
    return NULL;
  }
  {
    int i = 0;
    while (service[i] && service[i] != '.')
      i++;
    service[i] = '\0';
  }
  return service;
}


x86 assembly (clang, -O0):
<get_serv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11f8 <get_serv+0x78>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x28,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x28,%esi
call   1070 <getcwd@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   1090 <strcat@plt>
mov    %rax,-0x18(%rbp)
jmp    1205 <get_serv+0x85>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0xc,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1243 <get_serv+0xc3>
mov    -0x18(%rbp),%rdi
add    $0xc,%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    1269 <get_serv+0xe9>
mov    -0x18(%rbp),%rsi
lea    0xdb2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d1 <get_serv+0x151>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
je     129f <get_serv+0x11f>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    12af <get_serv+0x12f>
jmp    12bd <get_serv+0x13d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1270 <get_serv+0xf0>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_serv>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x2e,(%rdi)
jne    11bd <get_serv+0x3d>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x29(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rdi
mov    $0x28,%esi
call   1070 <getcwd@plt>
mov    %rax,%r14
add    $0x1,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
jmp    11c8 <get_serv+0x48>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
mov    %rax,%r14
lea    0xe47(%rip),%rdi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11fc <get_serv+0x7c>
lea    0xe18(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1239 <get_serv+0xb9>
lea    0xc(%r14),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rbx,%rax,1),%ecx
test   %cl,%cl
je     1233 <get_serv+0xb3>
cmp    $0x2e,%cl
je     1233 <get_serv+0xb3>
add    $0x1,%rax
jmp    1220 <get_serv+0xa0>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/etc/initng/\00"
@.str.1 = [35 x i8] c"%s doesn't begin with /etc/initng/\00"
define i8* @get_serv(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 46
br i1 %11, label %12, label %27
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 40
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = mul i64 1, %17
%19 = trunc i64 %18 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = call i8* @getcwd(i8* %21, i32 40)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = call i8* @strcat(i8* %23, i8* %25)
store i8* %26, i8** %4
br label %30
27: ; preds = %1
%28 = load i8*, i8** %3
%29 = call i8* @strdup(i8* %28)
store i8* %29, i8** %4
br label %30
30: ; preds = %27, %12
%31 = load i8*, i8** %4
%32 = call i64 @strncmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %31, i32 12)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i64 12
%37 = call i8* @strdup(i8* %36)
store i8* %37, i8** %5
%38 = load i8*, i8** %4
%39 = call i32 @free(i8* %38)
br label %45
40: ; preds = %30
%41 = load i8*, i8** %4
%42 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = load i8*, i8** %4
%44 = call i32 @free(i8* %43)
store i8* null, i8** %2
br label %73
45: ; preds = %34
store i32 0, i32* %6
br label %46
46: ; preds = %64, %45
%47 = load i8*, i8** %5
%48 = load i32, i32* %6
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %46
%55 = load i8*, i8** %5
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp ne i32 %60, 46
br label %62
62: ; preds = %54, %46
%63 = phi i1 [ false, %46 ], [ %61, %54 ]
br i1 %63, label %64, label %67
64: ; preds = %62
%65 = load i32, i32* %6
%66 = add nsw i32 %65, 1
store i32 %66, i32* %6
br label %46
67: ; preds = %62
%68 = load i8*, i8** %5
%69 = load i32, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = load i8*, i8** %5
store i8* %72, i8** %2
br label %73
73: ; preds = %67, %40
%74 = load i8*, i8** %2
ret i8* %74
}

LLVM IR (-O2):
@.str = [13 x i8] c"/etc/initng/\00"
@.str.1 = [35 x i8] c"%s doesn't begin with /etc/initng/\00"
define noalias i8* @get_serv(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 46
br i1 %3, label %4, label %11
4: ; preds = %1
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 41
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i8* @getcwd(i8* %7, i32 40)
%9 = getelementptr inbounds i8, i8* %0, i64 1
%10 = tail call i8* @strcat(i8* dereferenceable(1) %8, i8* %9)
br label %13
11: ; preds = %1
%12 = tail call i8* @strdup(i8* %0)
br label %13
13: ; preds = %11, %4
%14 = phi i8* [ %8, %4 ], [ %12, %11 ]
%15 = tail call i64 @strncmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %14, i32 12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = getelementptr inbounds i8, i8* %14, i64 12
%19 = tail call i8* @strdup(i8* %18)
%20 = tail call i32 @free(i8* %14)
br label %24
21: ; preds = %13
%22 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%23 = tail call i32 @free(i8* %14)
br label %33
24: ; preds = %28, %17
%25 = phi i64 [ %29, %28 ], [ 0, %17 ]
%26 = getelementptr inbounds i8, i8* %19, i64 %25
%27 = load i8, i8* %26
switch i8 %27, label %28 [
i8 0, label %30
i8 46, label %30
]
28: ; preds = %24
%29 = add nuw i64 %25, 1
br label %24
30: ; preds = %24, %24
%31 = and i64 %25, 4294967295
%32 = getelementptr inbounds i8, i8* %19, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %30, %21
%34 = phi i8* [ %19, %30 ], [ null, %21 ]
ret i8* %34
}