#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*/ endusershell();
char *getusershell();
int /*<<< orphan*/ setusershell();
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *strrchr(char *, char);
char *dup_shell(char *name) {
  char *p, *sh, *ret;
  setusershell();
  while ((sh = getusershell())) {
    if (!strcmp(name, sh)) {
      endusershell();
      return (strdup(name));
    }
    if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) {
      ret = strdup(sh);
      endusershell();
      return (ret);
    }
  }
  endusershell();
  return (NULL);
}


x86 assembly (clang, -O0):
<dup_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <setusershell@plt>
mov    $0x0,%al
call   1050 <getusershell@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1215 <dup_shell+0xb5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11b8 <dup_shell+0x58>
mov    $0x0,%al
call   1080 <endusershell@plt>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1224 <dup_shell+0xc4>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1210 <dup_shell+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1210 <dup_shell+0xb0>
mov    -0x20(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <endusershell@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1224 <dup_shell+0xc4>
jmp    1173 <dup_shell+0x13>
mov    $0x0,%al
call   1080 <endusershell@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup_shell>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <setusershell@plt>
xor    %eax,%eax
call   1050 <getusershell@plt>
test   %rax,%rax
je     11dc <dup_shell+0x7c>
mov    %rax,%rbx
jmp    118f <dup_shell+0x2f>
nop
xor    %eax,%eax
call   1050 <getusershell@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11dc <dup_shell+0x7c>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11ed <dup_shell+0x8d>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     1180 <dup_shell+0x20>
add    $0x1,%rax
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1180 <dup_shell+0x20>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <endusershell@plt>
mov    %rbx,%rax
jmp    11e5 <dup_shell+0x85>
xor    %eax,%eax
call   1080 <endusershell@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1080 <endusershell@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <strdup@plt>

LLVM IR (-O0):
define i8* @dup_shell(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = call i32 (...) @setusershell()
br label %8
8: ; preds = %35, %1
%9 = call i8* (...) @getusershell()
store i8* %9, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %36
11: ; preds = %8
%12 = load i8*, i8** %3
%13 = load i8*, i8** %5
%14 = call i64 @strcmp(i8* %12, i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = call i32 (...) @endusershell()
%18 = load i8*, i8** %3
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %2
br label %38
20: ; preds = %11
%21 = load i8*, i8** %5
%22 = call i8* @strrchr(i8* %21, i8 signext 47)
store i8* %22, i8** %4
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = call i64 @strcmp(i8* %25, i8* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i8*, i8** %5
%32 = call i8* @strdup(i8* %31)
store i8* %32, i8** %6
%33 = call i32 (...) @endusershell()
%34 = load i8*, i8** %6
store i8* %34, i8** %2
br label %38
35: ; preds = %24, %20
br label %8
36: ; preds = %8
%37 = call i32 (...) @endusershell()
store i8* null, i8** %2
br label %38
38: ; preds = %36, %30, %16
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
define noalias i8* @dup_shell(i8* %0) {
%2 = tail call i32 (...) @setusershell()
%3 = tail call i8* (...) @getusershell()
%4 = icmp eq i8* %3, null
br i1 %4, label %25, label %5
5: ; preds = %1, %22
%6 = phi i8* [ %23, %22 ], [ %3, %1 ]
%7 = tail call i64 @strcmp(i8* %0, i8* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = tail call i32 (...) @endusershell()
%11 = tail call i8* @strdup(i8* %0)
br label %27
12: ; preds = %5
%13 = tail call i8* @strrchr(i8* %6, i8 signext 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %13, i64 1
%17 = tail call i64 @strcmp(i8* %0, i8* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i8* @strdup(i8* %6)
%21 = tail call i32 (...) @endusershell()
br label %27
22: ; preds = %15, %12
%23 = tail call i8* (...) @getusershell()
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %5
25: ; preds = %22, %1
%26 = tail call i32 (...) @endusershell()
br label %27
27: ; preds = %25, %19, %9
%28 = phi i8* [ %20, %19 ], [ %11, %9 ], [ null, %25 ]
ret i8* %28
}