#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
FILE *psnd_open_script_file(char *filename, char *argv0) {
  FILE *scriptfile;
  char *p, *q;
  if ((scriptfile = fopen(filename, "r")) != NULL)
    return scriptfile;
  if (strchr(filename, '/') != NULL)
    return NULL;
  if (argv0 != NULL) {
    if ((p = strchr(argv0, '/')) != NULL) {
      p++;
      q = (char *)malloc(strlen(filename) + strlen(argv0) + 1);
      assert(q);
      strncpy(q, argv0, (p - argv0));
      strcat(q, filename);
      scriptfile = fopen(q, "r");
      free(q);
      if (scriptfile != NULL)
        return scriptfile;
    }
  }
  if ((p = getenv("PSNDSCRIPTDIR")) == NULL)
    return NULL;
  q = (char *)malloc(strlen(filename) + strlen(p) + 2);
  assert(q);
  strcpy(q, p);
  if (p[strlen(p) - 1] != '/')
    strcat(q, "/");
  strcat(q, filename);
  scriptfile = fopen(q, "r");
  free(q);
  return scriptfile;
}


x86 assembly (clang, -O0):
<psnd_open_script_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11db <psnd_open_script_file+0x3b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1397 <psnd_open_script_file+0x1f7>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
cmp    $0x0,%rax
je     1200 <psnd_open_script_file+0x60>
movq   $0x0,-0x8(%rbp)
jmp    1397 <psnd_open_script_file+0x1f7>
cmpq   $0x0,-0x18(%rbp)
je     12c8 <psnd_open_script_file+0x128>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12c3 <psnd_open_script_file+0x123>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x34(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <assert@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <strncpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xd6c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x20(%rbp)
je     12be <psnd_open_script_file+0x11e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1397 <psnd_open_script_file+0x1f7>
jmp    12c3 <psnd_open_script_file+0x123>
jmp    12c8 <psnd_open_script_file+0x128>
lea    0xd33(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12ef <psnd_open_script_file+0x14f>
movq   $0x0,-0x8(%rbp)
jmp    1397 <psnd_open_script_file+0x1f7>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x44(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <assert@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1365 <psnd_open_script_file+0x1c5>
mov    -0x30(%rbp),%rdi
lea    0xcb0(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xc83(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psnd_open_script_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe4a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d7 <psnd_open_script_file+0x37>
mov    %r12,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
test   %rax,%rax
je     11e4 <psnd_open_script_file+0x44>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
test   %r14,%r14
je     126d <psnd_open_script_file+0xcd>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
test   %rax,%rax
je     126d <psnd_open_script_file+0xcd>
mov    %rax,%r15
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1070 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <assert@plt>
sub    %r14d,%r15d
add    $0x1,%r15d
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <strncpy@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   10c0 <strcat@plt>
lea    0xdaf(%rip),%rsi
mov    %r13,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1040 <free@plt>
test   %rbx,%rbx
jne    11d7 <psnd_open_script_file+0x37>
lea    0xd8e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d5 <psnd_open_script_file+0x35>
mov    %rax,%r15
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1070 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <assert@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <strcpy@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r15,1)
je     12d9 <psnd_open_script_file+0x139>
lea    0xd3f(%rip),%rsi
mov    %r14,%rdi
call   10c0 <strcat@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   10c0 <strcat@plt>
lea    0xd15(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <free@plt>
jmp    11d7 <psnd_open_script_file+0x37>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"PSNDSCRIPTDIR\00"
@.str.2 = [2 x i8] c"/\00"
define i32* @psnd_open_script_file(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32*, i32** %6
store i32* %13, i32** %3
br label %100
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i8* @strchr(i8* %15, i8 signext 47)
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i32* null, i32** %3
br label %100
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %61
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = call i8* @strchr(i8* %23, i8 signext 47)
store i8* %24, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %60
26: ; preds = %22
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %7
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
%31 = load i8*, i8** %5
%32 = call i32 @strlen(i8* %31)
%33 = add nsw i32 %30, %32
%34 = add nsw i32 %33, 1
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = call i32 @assert(i8* %37)
%39 = load i8*, i8** %8
%40 = load i8*, i8** %5
%41 = load i8*, i8** %7
%42 = load i8*, i8** %5
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = trunc i64 %45 to i32
%47 = call i32 @strncpy(i8* %39, i8* %40, i32 %46)
%48 = load i8*, i8** %8
%49 = load i8*, i8** %4
%50 = call i32 @strcat(i8* %48, i8* %49)
%51 = load i8*, i8** %8
%52 = call i32* @fopen(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %52, i32** %6
%53 = load i8*, i8** %8
%54 = call i32 @free(i8* %53)
%55 = load i32*, i32** %6
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %59
57: ; preds = %26
%58 = load i32*, i32** %6
store i32* %58, i32** %3
br label %100
59: ; preds = %26
br label %60
60: ; preds = %59, %22
br label %61
61: ; preds = %60, %19
%62 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i8* %62, i8** %7
%63 = icmp eq i8* %62, null
br i1 %63, label %64, label %65
64: ; preds = %61
store i32* null, i32** %3
br label %100
65: ; preds = %61
%66 = load i8*, i8** %4
%67 = call i32 @strlen(i8* %66)
%68 = load i8*, i8** %7
%69 = call i32 @strlen(i8* %68)
%70 = add nsw i32 %67, %69
%71 = add nsw i32 %70, 2
%72 = call i64 @malloc(i32 %71)
%73 = inttoptr i64 %72 to i8*
store i8* %73, i8** %8
%74 = load i8*, i8** %8
%75 = call i32 @assert(i8* %74)
%76 = load i8*, i8** %8
%77 = load i8*, i8** %7
%78 = call i32 @strcpy(i8* %76, i8* %77)
%79 = load i8*, i8** %7
%80 = load i8*, i8** %7
%81 = call i32 @strlen(i8* %80)
%82 = sub nsw i32 %81, 1
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %79, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp ne i32 %86, 47
br i1 %87, label %88, label %91
88: ; preds = %65
%89 = load i8*, i8** %8
%90 = call i32 @strcat(i8* %89, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %91
91: ; preds = %88, %65
%92 = load i8*, i8** %8
%93 = load i8*, i8** %4
%94 = call i32 @strcat(i8* %92, i8* %93)
%95 = load i8*, i8** %8
%96 = call i32* @fopen(i8* %95, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %96, i32** %6
%97 = load i8*, i8** %8
%98 = call i32 @free(i8* %97)
%99 = load i32*, i32** %6
store i32* %99, i32** %3
br label %100
100: ; preds = %91, %64, %57, %18, %12
%101 = load i32*, i32** %3
ret i32* %101
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"PSNDSCRIPTDIR\00"
@.str.2 = [2 x i8] c"/\00"
define noalias i32* @psnd_open_script_file(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %55
5: ; preds = %2
%6 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %55
8: ; preds = %5
%9 = icmp eq i8* %1, null
br i1 %9, label %31, label %10
10: ; preds = %8
%11 = tail call i8* @strchr(i8* %1, i8 signext 47)
%12 = icmp eq i8* %11, null
br i1 %12, label %31, label %13
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = tail call i32 @strlen(i8* %0)
%16 = tail call i32 @strlen(i8* %1)
%17 = add i32 %15, 1
%18 = add i32 %17, %16
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i32 @assert(i8* %20)
%22 = ptrtoint i8* %14 to i64
%23 = ptrtoint i8* %1 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = tail call i32 @strncpy(i8* %20, i8* %1, i32 %25)
%27 = tail call i32 @strcat(i8* %20, i8* %0)
%28 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%29 = tail call i32 @free(i8* %20)
%30 = icmp eq i32* %28, null
br i1 %30, label %31, label %55
31: ; preds = %10, %13, %8
%32 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %55, label %34
34: ; preds = %31
%35 = tail call i32 @strlen(i8* %0)
%36 = tail call i32 @strlen(i8* %32)
%37 = add i32 %35, 2
%38 = add i32 %37, %36
%39 = tail call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
%41 = tail call i32 @assert(i8* %40)
%42 = tail call i32 @strcpy(i8* %40, i8* %32)
%43 = tail call i32 @strlen(i8* %32)
%44 = add nsw i32 %43, -1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %32, i64 %45
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 47
br i1 %48, label %51, label %49
49: ; preds = %34
%50 = tail call i32 @strcat(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %34
%52 = tail call i32 @strcat(i8* %40, i8* %0)
%53 = tail call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%54 = tail call i32 @free(i8* %40)
br label %55
55: ; preds = %31, %13, %5, %2, %51
%56 = phi i32* [ %53, %51 ], [ %3, %2 ], [ null, %5 ], [ %28, %13 ], [ null, %31 ]
ret i32* %56
}