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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ file_exists(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
char *sprintf_alloc(char *, char const *, char const *);
int is_valid_harmony_home(const char *dir) {
  static const char *home_file[] = {"bin/hinfo", "bin/tuna",
                                    "libexec/random.so", NULL};
  int i, valid = 1;
  char *tmpbuf;
  for (i = 0; valid && home_file[i]; ++i) {
    tmpbuf = sprintf_alloc("%s/%s", dir, home_file[i]);
    if (!tmpbuf) {
      perror("Could not allocate memory for file path");
      exit(-1);
    }
    if (!file_exists(tmpbuf))
      valid = 0;
    free(tmpbuf);
  }
  return valid;
}


x86 assembly (clang, -O0):
<is_valid_harmony_home>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x19(%rbp)
je     118f <is_valid_harmony_home+0x3f>
movslq -0xc(%rbp),%rcx
lea    0x2ecc(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    119f <is_valid_harmony_home+0x4f>
jmp    1213 <is_valid_harmony_home+0xc3>
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rcx
lea    0x2ea2(%rip),%rax
mov    (%rax,%rcx,8),%rdx
lea    0xe6c(%rip),%rdi
call   1050 <sprintf_alloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <is_valid_harmony_home+0x93>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <file_exists@plt>
cmp    $0x0,%eax
jne    11fc <is_valid_harmony_home+0xac>
movl   $0x0,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116a <is_valid_harmony_home+0x1a>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_harmony_home>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %r12d,%r12d
lea    0x2c98(%rip),%r13
lea    0xeb6(%rip),%r14
nop
mov    (%r12,%r13,1),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <sprintf_alloc@plt>
test   %rax,%rax
je     11c1 <is_valid_harmony_home+0x71>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <file_exists@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
je     11ab <is_valid_harmony_home+0x5b>
lea    0x8(%r12),%rax
cmp    $0x10,%r12
mov    %rax,%r12
jne    1170 <is_valid_harmony_home+0x20>
xor    %eax,%eax
test   %ebp,%ebp
setne  %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@is_valid_harmony_home.home_file = internal global [4 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str = [10 x i8] c"bin/hinfo\00"
@.str.1 = [9 x i8] c"bin/tuna\00"
@.str.2 = [18 x i8] c"libexec/random.so\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [40 x i8] c"Could not allocate memory for file path\00"
define i32 @is_valid_harmony_home(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i32 1, i32* %4
store i32 0, i32* %3
br label %6
6: ; preds = %37, %1
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [4 x i8*], [4 x i8*]* @is_valid_harmony_home.home_file, i64 0, i64 %11
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br label %15
15: ; preds = %9, %6
%16 = phi i1 [ false, %6 ], [ %14, %9 ]
br i1 %16, label %17, label %40
17: ; preds = %15
%18 = load i8*, i8** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [4 x i8*], [4 x i8*]* @is_valid_harmony_home.home_file, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = call i8* @sprintf_alloc(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %18, i8* %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %29, label %26
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %17
%30 = load i8*, i8** %5
%31 = call i32 @file_exists(i8* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %29
store i32 0, i32* %4
br label %34
34: ; preds = %33, %29
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %34
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
br label %6
40: ; preds = %15
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@is_valid_harmony_home.home_file = internal unnamed_addr constant [4 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str = [10 x i8] c"bin/hinfo\00"
@.str.1 = [9 x i8] c"bin/tuna\00"
@.str.2 = [18 x i8] c"libexec/random.so\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [40 x i8] c"Could not allocate memory for file path\00"
define i32 @is_valid_harmony_home(i8* %0) {
br label %2
2: ; preds = %1, %11
%3 = phi i64 [ 0, %1 ], [ %15, %11 ]
%4 = getelementptr inbounds [4 x i8*], [4 x i8*]* @is_valid_harmony_home.home_file, i64 0, i64 %3
%5 = load i8*, i8** %4
%6 = tail call i8* @sprintf_alloc(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = tail call i32 @file_exists(i8* %6)
%13 = icmp ne i32 %12, 0
%14 = tail call i32 @free(i8* %6)
%15 = add nuw nsw i64 %3, 1
%16 = xor i1 %13, true
%17 = icmp eq i64 %15, 3
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %19, label %2
19: ; preds = %11
%20 = zext i1 %13 to i32
ret i32 %20
}