#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 */
typedef int /*<<< orphan*/ sbuf;

/* Variables and functions */
scalar_t__ file_exists(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strsave(char *);
char *get_default_emulator(char *progname) {
  char sbuf[2048];
  char *s;
  if (strlen(progname) >= sizeof(sbuf))
    return "erl";
  strcpy(sbuf, progname);
  for (s = sbuf + strlen(sbuf); s >= sbuf; s--) {
    if (((*s) == '/')) {
      strcpy(s + 1, "erl");
      if (file_exists(sbuf))
        return strsave(sbuf);
      break;
    }
  }
  return "erl";
}


x86 assembly (clang, -O0):
<get_default_emulator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x800,%rax
jb     1176 <get_default_emulator+0x36>
lea    0xe93(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    124e <get_default_emulator+0x10e>
lea    -0x810(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x810(%rbp),%rax
mov    %rax,-0x820(%rbp)
lea    -0x810(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x820(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x818(%rbp)
mov    -0x818(%rbp),%rax
lea    -0x810(%rbp),%rcx
cmp    %rcx,%rax
jb     1243 <get_default_emulator+0x103>
mov    -0x818(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1227 <get_default_emulator+0xe7>
mov    -0x818(%rbp),%rdi
add    $0x1,%rdi
lea    0xe0e(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x810(%rbp),%rdi
call   1040 <file_exists@plt>
cmp    $0x0,%rax
je     1222 <get_default_emulator+0xe2>
lea    -0x810(%rbp),%rdi
call   1060 <strsave@plt>
mov    %rax,-0x8(%rbp)
jmp    124e <get_default_emulator+0x10e>
jmp    1243 <get_default_emulator+0x103>
jmp    122c <get_default_emulator+0xec>
mov    -0x818(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x818(%rbp)
jmp    11b6 <get_default_emulator+0x76>
lea    0xdb6(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_default_emulator>:
push   %r15
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0xea5(%rip),%r14
cmp    $0x7ff,%eax
ja     11cb <get_default_emulator+0x8b>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
js     11cb <get_default_emulator+0x8b>
mov    %eax,%eax
lea    (%rsp,%rax,1),%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpb   $0x2f,(%rdi)
je     11a0 <get_default_emulator+0x60>
add    $0xffffffffffffffff,%rdi
cmp    %r15,%rdi
jae    1190 <get_default_emulator+0x50>
jmp    11cb <get_default_emulator+0x8b>
add    $0x1,%rdi
lea    0xe55(%rip),%r14
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rdi
call   1040 <file_exists@plt>
test   %rax,%rax
je     11cb <get_default_emulator+0x8b>
mov    %rsp,%rdi
call   1060 <strsave@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"erl\00"
define i8* @get_default_emulator(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [2048 x i8]
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = sext i32 %7 to i64
%9 = icmp uge i64 %8, 2048
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %2
br label %45
11: ; preds = %1
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%13 = load i8*, i8** %3
%14 = call i32 @strcpy(i8* %12, i8* %13)
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%16 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
store i8* %19, i8** %5
br label %20
20: ; preds = %41, %11
%21 = load i8*, i8** %5
%22 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%23 = icmp uge i8* %21, %22
br i1 %23, label %24, label %44
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 47
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = call i32 @strcpy(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%33 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%34 = call i64 @file_exists(i8* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%38 = call i8* @strsave(i8* %37)
store i8* %38, i8** %2
br label %45
39: ; preds = %29
br label %44
40: ; preds = %24
br label %41
41: ; preds = %40
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i32 -1
store i8* %43, i8** %5
br label %20
44: ; preds = %39, %20
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %2
br label %45
45: ; preds = %44, %36, %10
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@.str = [4 x i8] c"erl\00"
define i8* @get_default_emulator(i8* %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp ugt i32 %4, 2047
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = call i32 @strcpy(i8* %3, i8* %0)
%8 = call i32 @strlen(i8* %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %27, label %10
10: ; preds = %6
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 %11
br label %13
13: ; preds = %10, %24
%14 = phi i8* [ %25, %24 ], [ %12, %10 ]
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = getelementptr inbounds i8, i8* %14, i64 1
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%20 = call i64 @file_exists(i8* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = call i8* @strsave(i8* %3)
br label %27
24: ; preds = %13
%25 = getelementptr inbounds i8, i8* %14, i64 -1
%26 = icmp ult i8* %25, %3
br i1 %26, label %27, label %13
27: ; preds = %24, %6, %17, %1, %22
%28 = phi i8* [ %23, %22 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %17 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %24 ]
ret i8* %28
}