#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 */
struct passwd {
  char *pw_shell;
};

/* Variables and functions */
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *strdup(char *);
char *strrchr(char *, char);
char *get_user_shell(void) {
  struct passwd *pwent;
  char *shell = getenv("SHELL");
  if (!shell) {
    pwent = getpwuid(getuid());
    shell = pwent->pw_shell;
  }
  shell = strrchr(shell, '/');
  if (!shell)
    return ("sh");
  shell++;
  return strdup(shell);
}


x86 assembly (clang, -O0):
<get_user_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <get_user_shell+0x40>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bd <get_user_shell+0x6d>
lea    0xe52(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11d6 <get_user_shell+0x86>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_shell>:
push   %rax
lea    0xea8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1173 <get_user_shell+0x23>
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1192 <get_user_shell+0x42>
add    $0x1,%rax
mov    %rax,%rdi
pop    %rax
jmp    1070 <strdup@plt>
lea    0xe6d(%rip),%rax
pop    %rcx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [3 x i8] c"sh\00"
define i8* @get_user_shell() {
%1 = alloca i8*
%2 = alloca %struct.passwd*
%3 = alloca i8*
%4 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %13, label %7
7: ; preds = %0
%8 = call i32 (...) @getuid()
%9 = call %struct.passwd* @getpwuid(i32 %8)
store %struct.passwd* %9, %struct.passwd** %2
%10 = load %struct.passwd*, %struct.passwd** %2
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i32 0, i32 0
%12 = load i8*, i8** %11
store i8* %12, i8** %3
br label %13
13: ; preds = %7, %0
%14 = load i8*, i8** %3
%15 = call i8* @strrchr(i8* %14, i8 signext 47)
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %13
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %24
19: ; preds = %13
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = call i8* @strdup(i8* %22)
store i8* %23, i8** %1
br label %24
24: ; preds = %19, %18
%25 = load i8*, i8** %1
ret i8* %25
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [3 x i8] c"sh\00"
define i8* @get_user_shell() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = tail call i32 (...) @getuid()
%5 = tail call %struct.passwd* @getpwuid(i32 %4)
%6 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%7 = load i8*, i8** %6
br label %8
8: ; preds = %3, %0
%9 = phi i8* [ %1, %0 ], [ %7, %3 ]
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %9, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = tail call i8* @strdup(i8* %13)
br label %15
15: ; preds = %8, %12
%16 = phi i8* [ %14, %12 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %8 ]
ret i8* %16
}