#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 uint32_t;

/* Variables and functions */
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
uint32_t librpip_sysfs_node_file_open(uint32_t sysfsid, uint32_t fileid,
                                      int flags, int *fd) {
  const char *node = NULL;
  const char *file = NULL;
  char sfile[200];
  switch (sysfsid) {
  case 2:
    node = "/sys/class/pwm/pwmchip0/pwm0";
    break;
  case 3:
    node = "/sys/class/pwm/pwmchip0/pwm1";
    break;
  }
  switch (fileid) {
  case 1:
    file = "duty_cycle";
    break;
  case 2:
    file = "enable";
    break;
  case 3:
    file = "period";
    break;
  case 4:
    file = "polarity";
    break;
  }
  if (node && file) {
    sprintf(sfile, "%s/%s", node, file);
    *fd = open(sfile, flags);
    return *fd < 0 ? 0 : 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<librpip_sysfs_node_file_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xf4(%rbp)
sub    $0x2,%eax
je     1173 <librpip_sysfs_node_file_open+0x53>
jmp    115f <librpip_sysfs_node_file_open+0x3f>
mov    -0xf4(%rbp),%eax
sub    $0x3,%eax
je     1183 <librpip_sysfs_node_file_open+0x63>
jmp    118e <librpip_sysfs_node_file_open+0x6e>
lea    0xe96(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    118e <librpip_sysfs_node_file_open+0x6e>
lea    0xea3(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x100(%rbp)
sub    $0x3,%eax
ja     11f8 <librpip_sysfs_node_file_open+0xd8>
mov    -0x100(%rbp),%rax
lea    0xe4c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe86(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11f8 <librpip_sysfs_node_file_open+0xd8>
lea    0xe81(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11f8 <librpip_sysfs_node_file_open+0xd8>
lea    0xe78(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11f8 <librpip_sysfs_node_file_open+0xd8>
lea    0xe6f(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     125b <librpip_sysfs_node_file_open+0x13b>
cmpq   $0x0,-0x28(%rbp)
je     125b <librpip_sysfs_node_file_open+0x13b>
lea    -0xf0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe48(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0xf0(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <librpip_sysfs_node_file_open+0x142>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<librpip_sysfs_node_file_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0xd0,%rsp
mov    %rcx,%r14
mov    %edx,%ebp
cmp    $0x3,%edi
je     1143 <librpip_sysfs_node_file_open+0x23>
cmp    $0x2,%edi
jne    114e <librpip_sysfs_node_file_open+0x2e>
lea    0xebf(%rip),%rdx
jmp    114a <librpip_sysfs_node_file_open+0x2a>
lea    0xed3(%rip),%rdx
mov    $0x1,%cl
jmp    1152 <librpip_sysfs_node_file_open+0x32>
xor    %ecx,%ecx
xor    %edx,%edx
add    $0xffffffff,%esi
xor    %eax,%eax
cmp    $0x3,%esi
ja     1195 <librpip_sysfs_node_file_open+0x75>
test   %cl,%cl
je     1195 <librpip_sysfs_node_file_open+0x75>
movslq %esi,%rax
lea    0xefa(%rip),%rsi
movslq (%rsi,%rax,4),%rcx
add    %rsi,%rcx
lea    0xee4(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <sprintf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <open@plt>
mov    %eax,(%r14)
not    %eax
shr    $0x1f,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/pwm/pwmchip0/pwm0\00"
@.str.1 = [29 x i8] c"/sys/class/pwm/pwmchip0/pwm1\00"
@.str.2 = [11 x i8] c"duty_cycle\00"
@.str.3 = [7 x i8] c"enable\00"
@.str.4 = [7 x i8] c"period\00"
@.str.5 = [9 x i8] c"polarity\00"
@.str.6 = [6 x i8] c"%s/%s\00"
define i32 @librpip_sysfs_node_file_open(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [200 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i8* null, i8** %10
store i8* null, i8** %11
%13 = load i32, i32* %6
switch i32 %13, label %16 [
i32 2, label %14
i32 3, label %15
]
14: ; preds = %4
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8** %10
br label %16
15: ; preds = %4
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %16
16: ; preds = %4, %15, %14
%17 = load i32, i32* %7
switch i32 %17, label %22 [
i32 1, label %18
i32 2, label %19
i32 3, label %20
i32 4, label %21
]
18: ; preds = %16
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %11
br label %22
19: ; preds = %16
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %11
br label %22
20: ; preds = %16
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8** %11
br label %22
21: ; preds = %16
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8** %11
br label %22
22: ; preds = %16, %21, %20, %19, %18
%23 = load i8*, i8** %10
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %42
25: ; preds = %22
%26 = load i8*, i8** %11
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %42
28: ; preds = %25
%29 = getelementptr inbounds [200 x i8], [200 x i8]* %12, i64 0, i64 0
%30 = load i8*, i8** %10
%31 = load i8*, i8** %11
%32 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %30, i8* %31)
%33 = getelementptr inbounds [200 x i8], [200 x i8]* %12, i64 0, i64 0
%34 = load i32, i32* %8
%35 = call i32 @open(i8* %33, i32 %34)
%36 = load i32*, i32** %9
store i32 %35, i32* %36
%37 = load i32*, i32** %9
%38 = load i32, i32* %37
%39 = icmp slt i32 %38, 0
%40 = zext i1 %39 to i64
%41 = select i1 %39, i32 0, i32 1
store i32 %41, i32* %5
br label %43
42: ; preds = %25, %22
store i32 0, i32* %5
br label %43
43: ; preds = %42, %28
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/pwm/pwmchip0/pwm0\00"
@.str.1 = [29 x i8] c"/sys/class/pwm/pwmchip0/pwm1\00"
@.str.2 = [11 x i8] c"duty_cycle\00"
@.str.3 = [7 x i8] c"enable\00"
@.str.4 = [7 x i8] c"period\00"
@.str.5 = [9 x i8] c"polarity\00"
@.str.6 = [6 x i8] c"%s/%s\00"
@reltable.librpip_sysfs_node_file_open = [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str.2 to i64), i64 ptrtoint ([4 x i32]* @reltable.librpip_sysfs_node_file_open to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.3 to i64), i64 ptrtoint ([4 x i32]* @reltable.librpip_sysfs_node_file_open to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.4 to i64), i64 ptrtoint ([4 x i32]* @reltable.librpip_sysfs_node_file_open to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.5 to i64), i64 ptrtoint ([4 x i32]* @reltable.librpip_sysfs_node_file_open to i64)) to i32)]
define i32 @librpip_sysfs_node_file_open(i32 %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = alloca [200 x i8]
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
switch i32 %0, label %9 [
i32 2, label %7
i32 3, label %8
]
7: ; preds = %4
br label %9
8: ; preds = %4
br label %9
9: ; preds = %4, %8, %7
%10 = phi i1 [ false, %4 ], [ true, %8 ], [ true, %7 ]
%11 = phi i8* [ null, %4 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %8 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %7 ]
%12 = add i32 %1, -1
%13 = icmp ult i32 %12, 4
%14 = xor i1 %13, true
%15 = xor i1 %10, true
%16 = or i1 %14, %15
br i1 %16, label %25, label %17
17: ; preds = %9
%18 = sext i32 %12 to i64
%19 = shl i64 %18, 2
%20 = call i8* @llvm.load.relative.i64(i8* bitcast ([4 x i32]* @reltable.librpip_sysfs_node_file_open to i8*), i64 %19)
%21 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %11, i8* %20)
%22 = call i32 @open(i8* %6, i32 %2)
store i32 %22, i32* %3
%23 = xor i32 %22, -1
%24 = lshr i32 %23, 31
br label %25
25: ; preds = %9, %17
%26 = phi i32 [ %24, %17 ], [ 0, %9 ]
ret i32 %26
}