#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 *getpwent();
char *getusershell();
char *get_shell(void) {
  struct passwd *passwd;
  char *shell;
  passwd = getpwent();
  if (passwd != NULL && passwd->pw_shell != NULL) {
    return passwd->pw_shell;
  }
  shell = getenv("SHELL");
  if (shell)
    return shell;
  shell = getusershell();
  if (shell)
    return shell;
  return "/bin/sh";
}


x86 assembly (clang, -O0):
<get_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1050 <getpwent@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116c <get_shell+0x3c>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     116c <get_shell+0x3c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11c2 <get_shell+0x92>
lea    0xe8d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1194 <get_shell+0x64>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c2 <get_shell+0x92>
mov    $0x0,%al
call   1040 <getusershell@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b7 <get_shell+0x87>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c2 <get_shell+0x92>
lea    0xe48(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_shell>:
push   %rax
xor    %eax,%eax
call   1050 <getpwent@plt>
test   %rax,%rax
je     1147 <get_shell+0x17>
mov    (%rax),%rax
test   %rax,%rax
je     1147 <get_shell+0x17>
pop    %rcx
ret
lea    0xeb2(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1145 <get_shell+0x15>
xor    %eax,%eax
call   1040 <getusershell@plt>
mov    %rax,%rcx
test   %rax,%rax
lea    0xe9a(%rip),%rax
cmovne %rcx,%rax
pop    %rcx
ret

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

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