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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int sprintf(char *, char *, int);
int write(int, char *, int);
int sysfs_write_val(int fd, int val) {
  int n, rc;
  char szVal[32] = {0};
  n = sprintf(szVal, "%d", val);
  lseek(fd, 0, SEEK_SET);
  rc = write(fd, szVal, n + 1);
  if (rc < 0) {
    return -errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_write_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe94(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b9 <sysfs_write_val+0x79>
xor    %eax,%eax
mov    0x2e31(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11c0 <sysfs_write_val+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_write_val>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%edx
mov    %edi,%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xeae(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <sprintf@plt>
mov    %eax,%ebp
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r15d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
add    $0x1,%ebp
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    0x2e55(%rip),%rcx
sub    (%rcx),%ebx
sar    $0x1f,%eax
and    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @sysfs_write_val(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = bitcast [32 x i8]* %8 to i8*
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %4
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %13, i32 0, i32 %14)
%16 = load i32, i32* %4
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
store i32 %25, i32* %3
br label %27
26: ; preds = %2
store i32 0, i32* %3
br label %27
27: ; preds = %26, %23
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @sysfs_write_val(i32 %0, i32 %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %0, i32 0, i32 %6)
%8 = add nsw i32 %5, 1
%9 = call i32 @write(i32 %0, i8* %4, i32 %8)
%10 = icmp slt i32 %9, 0
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
%13 = select i1 %10, i32 %12, i32 0
ret i32 %13
}