#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 stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ invalid_filename(char *, int /*<<< orphan*/);
int /*<<< orphan*/ is_dir(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ *strstr(char *, char *);
char *guess_shell(void) {
  char *shell = NULL;
  struct stat s;
  shell = getenv("SHELL");
  if (shell) {
    invalid_filename(shell, 0);
    if (!is_dir(shell) && strstr(shell, "..") == NULL && stat(shell, &s) == 0 &&
        access(shell, X_OK) == 0)
      return shell;
  }
  char *shells[] = {"/bin/bash", "/bin/csh", "/usr/bin/zsh",
                    "/bin/sh",   "/bin/ash", NULL};
  int i = 0;
  while (shells[i] != NULL) {
    if (stat(shells[i], &s) == 0 && access(shells[i], X_OK) == 0) {
      shell = shells[i];
      break;
    }
    i++;
  }
  return shell;
}


x86 assembly (clang, -O0):
<guess_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movq   $0x0,-0x10(%rbp)
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1217 <guess_shell+0xa7>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1080 <invalid_filename@plt>
mov    -0x10(%rbp),%rdi
call   1040 <is_dir@plt>
cmp    $0x0,%eax
jne    1212 <guess_shell+0xa2>
mov    -0x10(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
jne    1212 <guess_shell+0xa2>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1212 <guess_shell+0xa2>
mov    -0x10(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    1212 <guess_shell+0xa2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12da <guess_shell+0x16a>
jmp    1217 <guess_shell+0xa7>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
lea    0xddb(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xdda(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xdd8(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xdda(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xdd7(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmpq   $0x0,-0x50(%rbp,%rax,8)
je     12d2 <guess_shell+0x162>
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    12c4 <guess_shell+0x154>
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    12c4 <guess_shell+0x154>
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    %rax,-0x10(%rbp)
jmp    12d2 <guess_shell+0x162>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1265 <guess_shell+0xf5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guess_shell>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
lea    0xe8c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
test   %rax,%rax
je     11d7 <guess_shell+0x77>
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <invalid_filename@plt>
mov    %r14,%rdi
call   1040 <is_dir@plt>
test   %eax,%eax
jne    11d7 <guess_shell+0x77>
lea    0xe68(%rip),%rsi
mov    %r14,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
jne    11d7 <guess_shell+0x77>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    11d7 <guess_shell+0x77>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1265 <guess_shell+0x105>
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
lea    0xe23(%rip),%r12
mov    %r12,0x10(%rsp)
lea    0x18(%rsp),%rbx
lea    0xe1c(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe19(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xe1a(%rip),%rax
mov    %rax,0x28(%rsp)
lea    0xe16(%rip),%rax
mov    %rax,0x30(%rsp)
lea    0x8(%rsp),%r15
mov    0x2dbc(%rip),%r13
jmp    123c <guess_shell+0xdc>
xchg   %ax,%ax
mov    (%rbx),%r12
add    $0x8,%rbx
test   %r12,%r12
je     125f <guess_shell+0xff>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    1230 <guess_shell+0xd0>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1060 <access@plt>
test   %rax,%rax
jne    1230 <guess_shell+0xd0>
jmp    1262 <guess_shell+0x102>
mov    %r14,%r12
mov    %r12,%r14
mov    %r14,%rax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [3 x i8] c"..\00"
@X_OK = global i32 0
@.str.2 = [10 x i8] c"/bin/bash\00"
@.str.3 = [9 x i8] c"/bin/csh\00"
@.str.4 = [13 x i8] c"/usr/bin/zsh\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [9 x i8] c"/bin/ash\00"
define i8* @guess_shell() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca [6 x i8*]
%5 = alloca i32
store i8* null, i8** %2
%6 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %31
9: ; preds = %0
%10 = load i8*, i8** %2
%11 = call i32 @invalid_filename(i8* %10, i32 0)
%12 = load i8*, i8** %2
%13 = call i32 @is_dir(i8* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %9
%16 = load i8*, i8** %2
%17 = call i32* @strstr(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = load i8*, i8** %2
%21 = call i64 @stat(i8* %20, %struct.stat* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = load i8*, i8** %2
%25 = load i32, i32* @X_OK
%26 = call i64 @access(i8* %24, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i8*, i8** %2
store i8* %29, i8** %1
br label %70
30: ; preds = %23, %19, %15, %9
br label %31
31: ; preds = %30, %0
%32 = bitcast [6 x i8*]* %4 to i8*
%33 = bitcast i8* %32 to [6 x i8*]*
%34 = getelementptr inbounds [6 x i8*], [6 x i8*]* %33, i32 0, i32 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8** %34
%35 = getelementptr inbounds [6 x i8*], [6 x i8*]* %33, i32 0, i32 1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i32 0, i32 0), i8** %35
%36 = getelementptr inbounds [6 x i8*], [6 x i8*]* %33, i32 0, i32 2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i32 0, i32 0), i8** %36
%37 = getelementptr inbounds [6 x i8*], [6 x i8*]* %33, i32 0, i32 3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i32 0, i32 0), i8** %37
%38 = getelementptr inbounds [6 x i8*], [6 x i8*]* %33, i32 0, i32 4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i32 0, i32 0), i8** %38
store i32 0, i32* %5
br label %39
39: ; preds = %65, %31
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %41
%43 = load i8*, i8** %42
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %68
45: ; preds = %39
%46 = load i32, i32* %5
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %47
%49 = load i8*, i8** %48
%50 = call i64 @stat(i8* %49, %struct.stat* %3)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %65
52: ; preds = %45
%53 = load i32, i32* %5
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %54
%56 = load i8*, i8** %55
%57 = load i32, i32* @X_OK
%58 = call i64 @access(i8* %56, i32 %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %52
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %62
%64 = load i8*, i8** %63
store i8* %64, i8** %2
br label %68
65: ; preds = %52, %45
%66 = load i32, i32* %5
%67 = add nsw i32 %66, 1
store i32 %67, i32* %5
br label %39
68: ; preds = %60, %39
%69 = load i8*, i8** %2
store i8* %69, i8** %1
br label %70
70: ; preds = %68, %28
%71 = load i8*, i8** %1
ret i8* %71
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [3 x i8] c"..\00"
@X_OK = global i32 0
@.str.2 = [10 x i8] c"/bin/bash\00"
@.str.3 = [9 x i8] c"/bin/csh\00"
@.str.4 = [13 x i8] c"/usr/bin/zsh\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [9 x i8] c"/bin/ash\00"
define i8* @guess_shell() {
%1 = alloca %struct.stat
%2 = alloca [6 x i8*]
%3 = bitcast %struct.stat* %1 to i8*
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %20, label %6
6: ; preds = %0
%7 = tail call i32 @invalid_filename(i8* %4, i32 0)
%8 = tail call i32 @is_dir(i8* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %6
%11 = tail call i32* @strstr(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = call i64 @stat(i8* %4, %struct.stat* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @X_OK
%18 = call i64 @access(i8* %4, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %45, label %20
20: ; preds = %0, %16, %13, %10, %6
%21 = bitcast [6 x i8*]* %2 to i8*
%22 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 4
%23 = bitcast i8** %22 to i8*
%24 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %24
%25 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %25
%26 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8** %26
%27 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %27
%28 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8** %28
br label %29
29: ; preds = %20, %38
%30 = phi i64 [ 0, %20 ], [ %39, %38 ]
%31 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %20 ], [ %41, %38 ]
%32 = call i64 @stat(i8* %31, %struct.stat* %1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32, i32* @X_OK
%36 = call i64 @access(i8* %31, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %34, %29
%39 = add nuw i64 %30, 1
%40 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 %39
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %29
43: ; preds = %38, %34
%44 = phi i8* [ %4, %38 ], [ %31, %34 ]
br label %45
45: ; preds = %16, %43
%46 = phi i8* [ %44, %43 ], [ %4, %16 ]
ret i8* %46
}