#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*/ ENOMEM;
int /*<<< orphan*/ ENOSYS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int strlen(char const *);
int open_prop_file(const char *prop_path, const char *prop_name, int *fd) {
  char *path;
  int len;
  len = strlen(prop_path) + strlen(prop_name) + 1 + 1;
  path = malloc(len);
  if (path == NULL) {
    errno = ENOMEM;
    return -1;
  }
  snprintf(path, len, "%s/%s", prop_path, prop_name);
  *fd = open(path, O_RDONLY);
  free(path);
  if (*fd < 0) {
    errno = ENOSYS;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<open_prop_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11be <open_prop_file+0x6e>
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
mov    0x2e18(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <open_prop_file+0xde>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe2c(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1227 <open_prop_file+0xd7>
mov    0x2dd0(%rip),%rax
mov    (%rax),%ecx
mov    0x2daf(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <open_prop_file+0xde>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_prop_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x2,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c9 <open_prop_file+0x79>
mov    %rax,%rbx
lea    0xe74(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r12,%rcx
mov    %r15,%r8
call   1050 <snprintf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
test   %ebp,%ebp
jns    11e0 <open_prop_file+0x90>
mov    0x2e19(%rip),%rax
jmp    11d0 <open_prop_file+0x80>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    0x2def(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@ENOSYS = global i32 0
define i32 @open_prop_file(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = load i8*, i8** %6
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %11, %13
%15 = add nsw i32 %14, 1
%16 = add nsw i32 %15, 1
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i32, i32* @ENOMEM
store i32 %22, i32* @errno
store i32 -1, i32* %4
br label %41
23: ; preds = %3
%24 = load i8*, i8** %8
%25 = load i32, i32* %9
%26 = load i8*, i8** %5
%27 = load i8*, i8** %6
%28 = call i32 @snprintf(i8* %24, i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
%29 = load i8*, i8** %8
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
%32 = load i32*, i32** %7
store i32 %31, i32* %32
%33 = load i8*, i8** %8
%34 = call i32 @free(i8* %33)
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %23
%39 = load i32, i32* @ENOSYS
store i32 %39, i32* @errno
store i32 -1, i32* %4
br label %41
40: ; preds = %23
store i32 0, i32* %4
br label %41
41: ; preds = %40, %38, %21
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@ENOSYS = global i32 0
define i32 @open_prop_file(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = add i32 %4, 2
%7 = add i32 %6, %5
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = tail call i32 @snprintf(i8* %8, i32 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %8, i32 %12)
store i32 %13, i32* %2
%14 = tail call i32 @free(i8* %8)
%15 = icmp slt i32 %13, 0
br i1 %15, label %16, label %19
16: ; preds = %10, %3
%17 = phi i32* [ @ENOMEM, %3 ], [ @ENOSYS, %10 ]
%18 = load i32, i32* %17
store i32 %18, i32* @errno
br label %19
19: ; preds = %16, %10
%20 = phi i32 [ 0, %10 ], [ -1, %16 ]
ret i32 %20
}