#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);
    if (root_path == NULL)
      return NULL;
    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);
    if (root_path == NULL)
      return NULL;
    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,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
movq   $0x0,-0x428(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe28(%rip),%rsi
mov    $0x6,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%eax
jne    1245 <get_socket_base_path+0xb5>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x18(%rbp)
mov    $0x80,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1229 <get_socket_base_path+0x99>
movq   $0x0,-0x8(%rbp)
jmp    138a <get_socket_base_path+0x1fa>
mov    -0x428(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdcc(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    137f <get_socket_base_path+0x1ef>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x20(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x440(%rbp)
mov    -0x20(%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)
cmpq   $0x0,-0x428(%rbp)
jne    12e4 <get_socket_base_path+0x154>
movq   $0x0,-0x8(%rbp)
jmp    138a <get_socket_base_path+0x1fa>
lea    -0x420(%rbp),%rsi
mov    $0x2f,%edi
mov    $0x5,%edx
call   10a0 <my_nstrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1328 <get_socket_base_path+0x198>
mov    -0x428(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xce9(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    137a <get_socket_base_path+0x1ea>
mov    -0x428(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x420(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x450(%rbp)
mov    -0x18(%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    0xc92(%rip),%rdx
call   1080 <snprintf@plt>
jmp    137f <get_socket_base_path+0x1ef>
mov    -0x428(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%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     1271 <get_socket_base_path+0xe1>
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,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
mov    $0x80,%edi
call   1030 <my_malloc@plt>
test   %rax,%rax
je     1290 <get_socket_base_path+0x100>
mov    %rax,%r14
mov    %rsp,%rsi
mov    $0x2f,%edi
mov    $0x5,%edx
call   10b0 <my_nstrchr@plt>
test   %rax,%rax
je     1295 <get_socket_base_path+0x105>
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    0xda6(%rip),%rdx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15,%rcx
call   1090 <snprintf@plt>
jmp    12a0 <get_socket_base_path+0x110>
mov    $0x80,%edi
call   1030 <my_malloc@plt>
test   %rax,%rax
je     1290 <get_socket_base_path+0x100>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strcpy@plt>
jmp    12a0 <get_socket_base_path+0x110>
xor    %r14d,%r14d
jmp    12a0 <get_socket_base_path+0x110>
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 i8*
%6 = alloca [1024 x i8]
%7 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
%8 = bitcast [1024 x i8]* %6 to i8*
store i8* null, i8** %7
%9 = load i8*, i8** %3
%10 = call i32 @strncmp(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%11 = icmp ne i32 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i8* @strchr(i8* %13, i8 signext 47)
store i8* %14, i8** %4
%15 = call i8* @my_malloc(i32 128)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %12
store i8* null, i8** %2
br label %62
19: ; preds = %12
%20 = load i8*, i8** %7
%21 = load i8*, i8** %4
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %60
23: ; preds = %1
%24 = load i8*, i8** %3
%25 = call i8* @strchr(i8* %24, i8 signext 47)
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = call i8* @strchr(i8* %26, i8 signext 10)
store i8* %27, i8** %5
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = load i8*, i8** %4
%30 = load i8*, i8** %4
%31 = call i64 @strlen(i8* %30)
%32 = load i8*, i8** %5
%33 = call i64 @strlen(i8* %32)
%34 = sub nsw i64 %31, %33
%35 = call i32 @strncpy(i8* %28, i8* %29, i64 %34)
%36 = call i8* @my_malloc(i32 128)
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %23
store i8* null, i8** %2
br label %62
40: ; preds = %23
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i8* @my_nstrchr(i8 signext 47, i8* %41, i32 5)
store i8* %42, i8** %4
%43 = load i8*, i8** %4
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = load i8*, i8** %7
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %47)
br label %59
49: ; preds = %40
%50 = load i8*, i8** %7
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%52 = call i64 @strlen(i8* %51)
%53 = load i8*, i8** %4
%54 = call i64 @strlen(i8* %53)
%55 = sub nsw i64 %52, %54
%56 = add nsw i64 %55, 1
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%58 = call i32 @snprintf(i8* %50, i64 %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %49, %45
br label %60
60: ; preds = %59, %19
%61 = load i8*, i8** %7
store i8* %61, i8** %2
br label %62
62: ; preds = %60, %39, %18
%63 = load i8*, i8** %2
ret i8* %63
}

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 %12
7: ; preds = %1
%8 = tail call i8* @my_malloc(i32 128)
%9 = icmp eq i8* %8, null
br i1 %9, label %31, label %10
10: ; preds = %7
%11 = tail call i8* @strcpy(i8* %8, i8* dereferenceable(1) %6)
br label %31
12: ; preds = %1
%13 = tail call i8* @strchr(i8* dereferenceable(1) %6, i8 signext 10)
%14 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%15 = tail call i64 @strlen(i8* dereferenceable(1) %13)
%16 = sub nsw i64 %14, %15
%17 = call i32 @strncpy(i8* %3, i8* %6, i64 %16)
%18 = call i8* @my_malloc(i32 128)
%19 = icmp eq i8* %18, null
br i1 %19, label %31, label %20
20: ; preds = %12
%21 = call i8* @my_nstrchr(i8 signext 47, i8* %3, i32 5)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i8* @strcpy(i8* %18, i8* %3)
br label %31
25: ; preds = %20
%26 = call i64 @strlen(i8* %3)
%27 = call i64 @strlen(i8* %21)
%28 = add i64 %26, 1
%29 = sub i64 %28, %27
%30 = call i32 @snprintf(i8* %18, i64 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %31
31: ; preds = %10, %25, %23, %12, %7
%32 = phi i8* [ null, %7 ], [ null, %12 ], [ %18, %23 ], [ %18, %25 ], [ %8, %10 ]
ret i8* %32
}