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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open_sysfs_file(char const *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strnlen(char *, int);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int set_fps(const char *devicename, const char *fps) {
  int result = 1;
  char _fps[100];
  int fd = open_sysfs_file(devicename, "format", O_WRONLY);
  if (fd < 0)
    return 1;
  snprintf(_fps, sizeof(_fps) - 1, "@%s", fps);
  _fps[sizeof(_fps) - 1] = 0;
  if (write(fd, _fps, strnlen(_fps, sizeof(_fps))) < 0) {
    perror("failed to set fps");
    goto done;
  }
  result = 0;
done:
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<set_fps>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
lea    0xe72(%rip),%rsi
call   1070 <open_sysfs_file@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    11b2 <set_fps+0x52>
movl   $0x1,-0x4(%rbp)
jmp    123b <set_fps+0xdb>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x63,%esi
lea    0xe41(%rip),%rdx
call   1040 <snprintf@plt>
movb   $0x0,-0x1d(%rbp)
mov    -0x84(%rbp),%eax
mov    %eax,-0x94(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
call   1050 <strnlen@plt>
mov    -0x94(%rbp),%edi
mov    -0x90(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1223 <set_fps+0xc3>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
jmp    122a <set_fps+0xca>
movl   $0x0,-0x1c(%rbp)
mov    -0x84(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_fps>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %rsi,%r14
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
lea    0xe85(%rip),%rsi
call   1070 <open_sysfs_file@plt>
test   %eax,%eax
js     11ce <set_fps+0x6e>
mov    %eax,%ebx
lea    0xe7a(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x63,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
movb   $0x0,0x63(%rsp)
mov    %rbp,%rdi
mov    $0x64,%esi
call   1050 <strnlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
test   %rax,%rax
js     11de <set_fps+0x7e>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11d3 <set_fps+0x73>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    11c5 <set_fps+0x65>

LLVM IR (-O0):
@.str = [7 x i8] c"format\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"@%s\00"
@.str.2 = [18 x i8] c"failed to set fps\00"
define i32 @set_fps(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [100 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open_sysfs_file(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %33
15: ; preds = %2
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%17 = load i8*, i8** %5
%18 = call i32 @snprintf(i8* %16, i32 99, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 99
store i8 0, i8* %19
%20 = load i32, i32* %8
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%23 = call i32 @strnlen(i8* %22, i32 100)
%24 = call i64 @write(i32 %20, i8* %21, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %29
28: ; preds = %15
store i32 0, i32* %6
br label %29
29: ; preds = %28, %26
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %6
store i32 %32, i32* %3
br label %33
33: ; preds = %29, %14
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [7 x i8] c"format\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"@%s\00"
@.str.2 = [18 x i8] c"failed to set fps\00"
define i32 @set_fps(i8* %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open_sysfs_file(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %2
%9 = call i32 @snprintf(i8* %4, i32 99, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 99
store i8 0, i8* %10
%11 = call i32 @strnlen(i8* %4, i32 100)
%12 = call i64 @write(i32 %6, i8* %4, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %8, %14
%17 = phi i32 [ 1, %14 ], [ 0, %8 ]
%18 = call i32 @close(i32 %6)
br label %19
19: ; preds = %2, %16
%20 = phi i32 [ %17, %16 ], [ 1, %2 ]
ret i32 %20
}