#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 *strdup(char *);
char *strndup(char const *, int);
char *strstr(char const *, char *);
char *set_root(const char *path) {
  char *root = NULL;
  const char *end = NULL;
  if (!path)
    goto out;
  if ((end = strstr(path, "/usr/bin")) != NULL) {
    if (path == end) {
      root = strdup("/");
    } else {
      root = strndup(path, end - path);
    }
  } else if ((end = strstr(path, "/bin")) != NULL) {
    if (path == end) {
      root = strdup("/");
    } else {
      root = strndup(path, end - path);
    }
  } else {
    root = strdup("/");
  }
out:
  return root;
}


x86 assembly (clang, -O0):
<set_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    115c <set_root+0x2c>
jmp    1236 <set_root+0x106>
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11bc <set_root+0x8c>
mov    -0x8(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    119d <set_root+0x6d>
lea    0xe7a(%rip),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x10(%rbp)
jmp    11b7 <set_root+0x87>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1030 <strndup@plt>
mov    %rax,-0x10(%rbp)
jmp    1231 <set_root+0x101>
mov    -0x8(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     121c <set_root+0xec>
mov    -0x8(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11fd <set_root+0xcd>
lea    0xe1a(%rip),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x10(%rbp)
jmp    1217 <set_root+0xe7>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1030 <strndup@plt>
mov    %rax,-0x10(%rbp)
jmp    122c <set_root+0xfc>
lea    0xde6(%rip),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x10(%rbp)
jmp    1231 <set_root+0x101>
jmp    1236 <set_root+0x106>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_root>:
push   %rbx
test   %rdi,%rdi
je     1170 <set_root+0x40>
mov    %rdi,%rbx
lea    0xec0(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    115e <set_root+0x2e>
lea    0xeb3(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
je     1174 <set_root+0x44>
cmp    %rbx,%rax
je     1174 <set_root+0x44>
sub    %ebx,%eax
mov    %rbx,%rdi
mov    %eax,%esi
pop    %rbx
jmp    1030 <strndup@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe8e(%rip),%rdi
pop    %rbx
jmp    1040 <strdup@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/usr/bin\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [5 x i8] c"/bin\00"
define i8* @set_root(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
store i8* null, i8** %4
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
br label %52
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call i8* @strstr(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = load i8*, i8** %4
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %3
br label %27
18: ; preds = %12
%19 = load i8*, i8** %2
%20 = load i8*, i8** %4
%21 = load i8*, i8** %2
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @strndup(i8* %19, i32 %25)
store i8* %26, i8** %3
br label %27
27: ; preds = %18, %16
br label %51
28: ; preds = %8
%29 = load i8*, i8** %2
%30 = call i8* @strstr(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %48
32: ; preds = %28
%33 = load i8*, i8** %2
%34 = load i8*, i8** %4
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %37, i8** %3
br label %47
38: ; preds = %32
%39 = load i8*, i8** %2
%40 = load i8*, i8** %4
%41 = load i8*, i8** %2
%42 = ptrtoint i8* %40 to i64
%43 = ptrtoint i8* %41 to i64
%44 = sub i64 %42, %43
%45 = trunc i64 %44 to i32
%46 = call i8* @strndup(i8* %39, i32 %45)
store i8* %46, i8** %3
br label %47
47: ; preds = %38, %36
br label %50
48: ; preds = %28
%49 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %49, i8** %3
br label %50
50: ; preds = %48, %47
br label %51
51: ; preds = %50, %27
br label %52
52: ; preds = %51, %7
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
@.str = [9 x i8] c"/usr/bin\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [5 x i8] c"/bin\00"
define noalias i8* @set_root(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %31, label %3
3: ; preds = %1
%4 = tail call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = icmp eq i8* %4, %0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %31
10: ; preds = %6
%11 = ptrtoint i8* %4 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = tail call i8* @strndup(i8* %0, i32 %14)
br label %31
16: ; preds = %3
%17 = tail call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %29, label %19
19: ; preds = %16
%20 = icmp eq i8* %17, %0
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %31
23: ; preds = %19
%24 = ptrtoint i8* %17 to i64
%25 = ptrtoint i8* %0 to i64
%26 = sub i64 %24, %25
%27 = trunc i64 %26 to i32
%28 = tail call i8* @strndup(i8* %0, i32 %27)
br label %31
29: ; preds = %16
%30 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %10, %8, %21, %23, %29, %1
%32 = phi i8* [ %9, %8 ], [ %15, %10 ], [ %22, %21 ], [ %28, %23 ], [ %30, %29 ], [ null, %1 ]
ret i8* %32
}