#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 */
char *my_malloc(int);
char *my_nstrchr(char, char *, int);
int /*<<< orphan*/ snprintf(char *, scalar_t__, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strchr(char *, char);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
char *get_socket_base_path(char *cmd) {
  char *temp = NULL;
  char *temp1 = NULL;
  char path[1024] = {0};
  char *root_path = NULL;
  if (!strncmp(cmd, "rmroot", 6)) {
    temp = strchr(cmd, '/');
    root_path = my_malloc(128);
    sprintf(root_path, "%s", temp);
  } else {
    temp = strchr(cmd, '/');
    temp1 = strchr(temp, '\n');
    strncpy(path, temp, strlen(temp) - strlen(temp1));
    root_path = my_malloc(128);
    temp = my_nstrchr('/', path, 5);
    if (temp == NULL) {
      sprintf(root_path, "%s", path);
    } else {
      snprintf(root_path, strlen(path) - strlen(temp) + 1, "%s", path);
    }
  }
  return root_path;
}


x86 assembly (clang, -O0):
<get_socket_base_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
movq   $0x0,-0x428(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe28(%rip),%rsi
mov    $0x6,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%eax
jne    122a <get_socket_base_path+0x9a>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x10(%rbp)
mov    $0x80,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xde7(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    1349 <get_socket_base_path+0x1b9>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x18(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x440(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x440(%rbp),%rdx
mov    -0x438(%rbp),%rdi
mov    -0x430(%rbp),%rsi
sub    %rax,%rdx
call   1040 <strncpy@plt>
mov    $0x80,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rsi
mov    $0x2f,%edi
mov    $0x5,%edx
call   10a0 <my_nstrchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12f2 <get_socket_base_path+0x162>
mov    -0x428(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xd1f(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    1344 <get_socket_base_path+0x1b4>
mov    -0x428(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x420(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x450(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x450(%rbp),%rsi
mov    -0x448(%rbp),%rdi
sub    %rax,%rsi
add    $0x1,%rsi
lea    -0x420(%rbp),%rcx
lea    0xcc8(%rip),%rdx
call   1080 <snprintf@plt>
jmp    1349 <get_socket_base_path+0x1b9>
mov    -0x428(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_socket_base_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   10a0 <memset@plt>
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x6,%edx
call   1050 <strncmp@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,%rbx
test   %ebp,%ebp
je     126f <get_socket_base_path+0xdf>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1080 <strchr@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1070 <strlen@plt>
sub    %rax,%rbp
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
mov    $0x80,%edi
call   1030 <my_malloc@plt>
mov    %rax,%r14
mov    $0x2f,%edi
mov    %r15,%rsi
mov    $0x5,%edx
call   10b0 <my_nstrchr@plt>
test   %rax,%rax
je     1284 <get_socket_base_path+0xf4>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1070 <strlen@plt>
sub    %rax,%rbp
add    $0x1,%rbp
lea    0xda8(%rip),%rdx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15,%rcx
call   1090 <snprintf@plt>
jmp    128f <get_socket_base_path+0xff>
mov    $0x80,%edi
call   1030 <my_malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
jmp    128a <get_socket_base_path+0xfa>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1060 <strcpy@plt>
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"rmroot\00"
@.str.1 = [3 x i8] c"%s\00"
define i8* @get_socket_base_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
store i8* null, i8** %4
%7 = bitcast [1024 x i8]* %5 to i8*
store i8* null, i8** %6
%8 = load i8*, i8** %2
%9 = call i32 @strncmp(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%10 = icmp ne i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i8* @strchr(i8* %12, i8 signext 47)
store i8* %13, i8** %3
%14 = call i8* @my_malloc(i32 128)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = load i8*, i8** %3
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %51
18: ; preds = %1
%19 = load i8*, i8** %2
%20 = call i8* @strchr(i8* %19, i8 signext 47)
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = call i8* @strchr(i8* %21, i8 signext 10)
store i8* %22, i8** %4
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = load i8*, i8** %3
%25 = load i8*, i8** %3
%26 = call i64 @strlen(i8* %25)
%27 = load i8*, i8** %4
%28 = call i64 @strlen(i8* %27)
%29 = sub nsw i64 %26, %28
%30 = call i32 @strncpy(i8* %23, i8* %24, i64 %29)
%31 = call i8* @my_malloc(i32 128)
store i8* %31, i8** %6
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = call i8* @my_nstrchr(i8 signext 47, i8* %32, i32 5)
store i8* %33, i8** %3
%34 = load i8*, i8** %3
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %18
%37 = load i8*, i8** %6
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %50
40: ; preds = %18
%41 = load i8*, i8** %6
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%43 = call i64 @strlen(i8* %42)
%44 = load i8*, i8** %3
%45 = call i64 @strlen(i8* %44)
%46 = sub nsw i64 %43, %45
%47 = add nsw i64 %46, 1
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%49 = call i32 @snprintf(i8* %41, i64 %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %40, %36
br label %51
51: ; preds = %50, %11
%52 = load i8*, i8** %6
ret i8* %52
}

LLVM IR (-O2):
@.str = [7 x i8] c"rmroot\00"
@.str.1 = [3 x i8] c"%s\00"
define i8* @get_socket_base_path(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%5 = icmp eq i32 %4, 0
%6 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
br i1 %5, label %7, label %10
7: ; preds = %1
%8 = tail call i8* @my_malloc(i32 128)
%9 = tail call i8* @strcpy(i8* dereferenceable(1) %8, i8* dereferenceable(1) %6)
br label %27
10: ; preds = %1
%11 = tail call i8* @strchr(i8* dereferenceable(1) %6, i8 signext 10)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%13 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%14 = sub nsw i64 %12, %13
%15 = call i32 @strncpy(i8* %3, i8* %6, i64 %14)
%16 = call i8* @my_malloc(i32 128)
%17 = call i8* @my_nstrchr(i8 signext 47, i8* %3, i32 5)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i8* @strcpy(i8* dereferenceable(1) %16, i8* %3)
br label %27
21: ; preds = %10
%22 = call i64 @strlen(i8* %3)
%23 = call i64 @strlen(i8* %17)
%24 = add i64 %22, 1
%25 = sub i64 %24, %23
%26 = call i32 @snprintf(i8* %16, i64 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %27
27: ; preds = %19, %21, %7
%28 = phi i8* [ %16, %19 ], [ %16, %21 ], [ %8, %7 ]
ret i8* %28
}