#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char const *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int pwm_write(char *pwm_key, const char *filename, char *value) {
  if (pwm_key == NULL)
    return -1;
  int fd;
  char filepath[64];
  snprintf(filepath, sizeof(filepath), "%s/%s/%s", "/sys/devices/ocp.2",
           pwm_key, filename);
  if ((fd = open(filepath, O_WRONLY)) < 0)
    return -1;
  write(fd, value, strlen(value));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<pwm_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117e <pwm_write+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <pwm_write+0xb0>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
mov    $0x40,%esi
lea    0xe6a(%rip),%rdx
lea    0xe6c(%rip),%rcx
call   1050 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11cc <pwm_write+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <pwm_write+0xb0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwm_write>:
test   %rdi,%rdi
je     11b8 <pwm_write+0x68>
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%rbx
mov    %rsi,%r9
mov    %rdi,%r8
lea    0xe95(%rip),%rdx
lea    0xe97(%rip),%rcx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1050 <snprintf@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11be <pwm_write+0x6e>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11c3 <pwm_write+0x73>
mov    $0xffffffff,%eax
ret
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
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_WRONLY = global i32 0
define i32 @pwm_write(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [64 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %31
13: ; preds = %3
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = call i32 @snprintf(i8* %14, 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* %15, i8* %16)
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%19 = load i32, i32* @O_WRONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13
store i32 -1, i32* %4
br label %31
23: ; preds = %13
%24 = load i32, i32* %8
%25 = load i8*, i8** %7
%26 = load i8*, i8** %7
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @write(i32 %24, i8* %25, i32 %27)
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 0, i32* %4
br label %31
31: ; preds = %23, %22, %12
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [19 x i8] c"/sys/devices/ocp.2\00"
@O_WRONLY = global i32 0
define i32 @pwm_write(i8* %0, i8* %1, i8* %2) {
%4 = alloca [64 x i8]
%5 = icmp eq i8* %0, null
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, 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)
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %7, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %6
%13 = call i32 @strlen(i8* %2)
%14 = call i32 @write(i32 %10, i8* %2, i32 %13)
%15 = call i32 @close(i32 %10)
br label %16
16: ; preds = %6, %12
%17 = phi i32 [ 0, %12 ], [ -1, %6 ]
br label %18
18: ; preds = %3, %16
%19 = phi i32 [ %17, %16 ], [ -1, %3 ]
ret i32 %19
}