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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ read(int, char *, unsigned int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char const *);
int /*<<< orphan*/ sscanf(char *, char *, char *);
int pwm_read(char *pwm_key, const char *filename, char *value,
             unsigned int length) {
  if (pwm_key == NULL)
    return -1;
  int fd, len;
  char filepath[64];
  snprintf(filepath, sizeof(filepath), "%s/%s/%s", "/sys/devices/ocp.2",
           pwm_key, filename);
  if ((fd = open(filepath, O_RDONLY | O_NONBLOCK)) < 0)
    return -1;
  lseek(fd, 0, SEEK_SET);
  read(fd, value, length);
  sscanf(value, "%s\n", value);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<pwm_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118e <pwm_read+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <pwm_read+0xca>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
mov    $0x40,%esi
lea    0xe5a(%rip),%rdx
lea    0xe5c(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11e5 <pwm_read+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <pwm_read+0xca>
mov    -0x28(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe06(%rip),%rsi
call   1070 <sscanf@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwm_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
test   %rdi,%rdi
je     11f9 <pwm_read+0x99>
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r9
mov    %rdi,%r8
lea    0xe7a(%rip),%rdx
lea    0xe7c(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e3c(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f9 <pwm_read+0x99>
mov    %eax,%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
lea    0xe37(%rip),%rsi
mov    %r15,%rdi
mov    %r15,%rdx
call   1070 <sscanf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11fe <pwm_read+0x9e>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [19 x i8] c"/sys/devices/ocp.2\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @pwm_read(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [64 x i8]
%6 = icmp eq i8* %0, null
br i1 %6, label %23, label %7
7: ; preds = %4
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%9 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = call i32 @open(i8* %8, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %7
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @lseek(i32 %13, i32 0, i32 %16)
%18 = call i32 @read(i32 %13, i8* %2, i32 %3)
%19 = call i32 @sscanf(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%20 = call i32 @close(i32 %13)
br label %21
21: ; preds = %7, %15
%22 = phi i32 [ 0, %15 ], [ -1, %7 ]
br label %23
23: ; preds = %4, %21
%24 = phi i32 [ %22, %21 ], [ -1, %4 ]
ret i32 %24
}