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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int SetPWMDuty(int PWM_num, double duty) {
  FILE *PWM_duty_addr;
  FILE *PWM_period_addr;
  char PWM_pin_num[5];
  char PWM_period[15] = {
      '\0',
  };
  char PWM_addr[60] = "/sys/class/pwm/pwm";
  sprintf(PWM_pin_num, "%i", PWM_num);
  strcat(strcat(PWM_addr, PWM_pin_num), "/period_ns");
  PWM_period_addr = fopen(PWM_addr, "r");
  fread(PWM_period, 1, 15, PWM_period_addr);
  strcpy(PWM_addr, "/sys/class/pwm/pwm");
  sprintf(PWM_pin_num, "%i", PWM_num);
  strcat(strcat(PWM_addr, PWM_pin_num), "/duty_ns");
  if (duty > 100.0)
    duty = 100.0;
  if (duty < 0.0)
    duty = 0.0;
  PWM_duty_addr = fopen(PWM_addr, "w");
  if (PWM_duty_addr == NULL)
    printf("PWM Duty failed to open\n");
  fseek(PWM_duty_addr, 0, SEEK_SET);
  fprintf(PWM_duty_addr, "%i",
          (int)((duty / 100) * ((double)atoi(PWM_period))));
  fflush(PWM_duty_addr);
  fclose(PWM_duty_addr);
  return 0;
}


x86 assembly (clang, -O0):
<SetPWMDuty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
lea    -0x34(%rbp),%rdi
xor    %esi,%esi
mov    $0xf,%edx
call   1070 <memset@plt>
lea    -0x70(%rbp),%rdi
lea    0xe12(%rip),%rsi
mov    $0x3c,%edx
call   1090 <memcpy@plt>
lea    -0x25(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe36(%rip),%rsi
call   10f0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
lea    -0x25(%rbp),%rsi
call   10e0 <strcat@plt>
mov    %rax,%rdi
lea    0xe1d(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x70(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x34(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0xf,%edx
call   1040 <fread@plt>
lea    -0x70(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x25(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xdcc(%rip),%rsi
call   10f0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
lea    -0x25(%rbp),%rsi
call   10e0 <strcat@plt>
mov    %rax,%rdi
lea    0xdd3(%rip),%rsi
call   10e0 <strcat@plt>
movsd  -0x10(%rbp),%xmm0
movsd  0xd52(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    12c5 <SetPWMDuty+0xf5>
movsd  0xd40(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    12db <SetPWMDuty+0x10b>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    -0x70(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1306 <SetPWMDuty+0x136>
lea    0xd79(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2ccf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xcd1(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x80(%rbp)
lea    -0x34(%rbp),%rdi
call   10d0 <atoi@plt>
movsd  -0x80(%rbp),%xmm0
mov    -0x78(%rbp),%rdi
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edx
lea    0xcee(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <fflush@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetPWMDuty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
movsd  %xmm0,(%rsp)
mov    %edi,%ebp
movq   $0x0,0xf(%rsp)
movq   $0x0,0x8(%rsp)
movups 0xe52(%rip),%xmm0
movups %xmm0,0x4c(%rsp)
movaps 0xe3a(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe1e(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe02(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xe32(%rip),%r15
lea    0x1b(%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   10e0 <sprintf@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
movabs $0x5f646f697265702f,%rcx
6f 64 5f
mov    %rcx,0x20(%rsp,%rax,1)
movl   $0x736e5f,0x27(%rsp,%rax,1)
lea    0xdf8(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0xf,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
lea    0xdd4(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   10e0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
movabs $0x736e5f797475642f,%rcx
5f 6e 73
mov    %rcx,0x20(%rsp,%rax,1)
movb   $0x0,0x28(%rsp,%rax,1)
movsd  0xd34(%rip),%xmm0
minsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
lea    0xd9b(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12f9 <SetPWMDuty+0x139>
lea    0xd86(%rip),%rdi
call   1040 <puts@plt>
xorpd  %xmm0,%xmm0
maxsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
mov    0x2cd2(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
movsd  (%rsp),%xmm0
divsd  0xcd9(%rip),%xmm0
movsd  %xmm0,(%rsp)
lea    0x8(%rsp),%rdi
call   10c0 <atoi@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  (%rsp),%xmm0
cvttsd2si %xmm0,%edx
lea    0xcfe(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1090 <fflush@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.SetPWMDuty.PWM_addr = [60 x i8] c"/sys/class/pwm/pwm\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [3 x i8] c"%i\00"
@.str.1 = [11 x i8] c"/period_ns\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"/sys/class/pwm/pwm\00"
@.str.4 = [9 x i8] c"/duty_ns\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [25 x i8] c"PWM Duty failed to open\0A\00"
@SEEK_SET = global i32 0
define i32 @SetPWMDuty(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [5 x i8]
%8 = alloca [15 x i8]
%9 = alloca [60 x i8]
store i32 %0, i32* %3
store double %1, double* %4
%10 = bitcast [15 x i8]* %8 to i8*
%11 = bitcast [60 x i8]* %9 to i8*
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%17 = call i8* @strcat(i8* %15, i8* %16)
%18 = call i8* @strcat(i8* %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %6
%21 = getelementptr inbounds [15 x i8], [15 x i8]* %8, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i32 @fread(i8* %21, i32 1, i32 15, i32* %22)
%24 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%27 = load i32, i32* %3
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%31 = call i8* @strcat(i8* %29, i8* %30)
%32 = call i8* @strcat(i8* %31, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%33 = load double, double* %4
%34 = fcmp ogt double %33, 1.000000e+02
br i1 %34, label %35, label %36
35: ; preds = %2
store double 1.000000e+02, double* %4
br label %36
36: ; preds = %35, %2
%37 = load double, double* %4
%38 = fcmp olt double %37, 0.000000e+00
br i1 %38, label %39, label %40
39: ; preds = %36
store double 0.000000e+00, double* %4
br label %40
40: ; preds = %39, %36
%41 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %42, i32** %5
%43 = load i32*, i32** %5
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = load i32*, i32** %5
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @fseek(i32* %48, i32 0, i32 %49)
%51 = load i32*, i32** %5
%52 = load double, double* %4
%53 = fdiv double %52, 1.000000e+02
%54 = getelementptr inbounds [15 x i8], [15 x i8]* %8, i64 0, i64 0
%55 = call i64 @atoi(i8* %54)
%56 = sitofp i64 %55 to double
%57 = fmul double %53, %56
%58 = fptosi double %57 to i32
%59 = call i32 @fprintf(i32* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %58)
%60 = load i32*, i32** %5
%61 = call i32 @fflush(i32* %60)
%62 = load i32*, i32** %5
%63 = call i32 @fclose(i32* %62)
ret i32 0
}

LLVM IR (-O2):
@__const.SetPWMDuty.PWM_addr = [60 x i8] c"/sys/class/pwm/pwm\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [3 x i8] c"%i\00"
@.str.1 = [11 x i8] c"/period_ns\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"/sys/class/pwm/pwm\00"
@.str.4 = [9 x i8] c"/duty_ns\00"
@.str.5 = [2 x i8] c"w\00"
@SEEK_SET = global i32 0
@str = [24 x i8] c"PWM Duty failed to open\00"
define i32 @SetPWMDuty(i32 %0, double %1) {
%3 = alloca [5 x i8]
%4 = alloca [15 x i8]
%5 = alloca [60 x i8]
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [60 x i8], [60 x i8]* %5, i64 0, i64 0
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = call i8* @strcat(i8* %8, i8* %6)
%11 = call i64 @strlen(i8* %8)
%12 = getelementptr [60 x i8], [60 x i8]* %5, i64 0, i64 %11
%13 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @fread(i8* %7, i32 1, i32 15, i32* %13)
%15 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = call i8* @strcat(i8* %8, i8* %6)
%18 = call i64 @strlen(i8* %8)
%19 = getelementptr [60 x i8], [60 x i8]* %5, i64 0, i64 %18
%20 = fcmp ogt double %1, 1.000000e+02
%21 = select i1 %20, double 1.000000e+02, double %1
%22 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %24, %2
%27 = fcmp olt double %21, 0.000000e+00
%28 = select i1 %27, double 0.000000e+00, double %21
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %22, i32 0, i32 %29)
%31 = fdiv double %28, 1.000000e+02
%32 = call i64 @atoi(i8* %7)
%33 = sitofp i64 %32 to double
%34 = fmul double %31, %33
%35 = fptosi double %34 to i32
%36 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = call i32 @fflush(i32* %22)
%38 = call i32 @fclose(i32* %22)
ret i32 0
}