#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 ENODEV;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ asprintf(char **, char *, char const *, int);
int find_type_by_name(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int write_sysfs_int(char *, char *, unsigned int);
int write_sysfs_int2(char *, char *, unsigned int, unsigned int);
int __write_devattr(const char *dir, char *device_name, char *attr,
                    unsigned int value, int type2, unsigned int value2) {
  char *dev_dir_name;
  int dev_num, ret = 0;
  dev_num = find_type_by_name(device_name, "iio:device");
  if (dev_num < 0) {
    syslog(LOG_INFO, "Failed to find the %s\n", device_name);
    ret = -ENODEV;
    goto error_ret;
  }
  asprintf(&dev_dir_name, "%siio:device%d", dir, dev_num);
  if (type2)
    ret = write_sysfs_int2(attr, dev_dir_name, value, value2);
  else
    ret = write_sysfs_int(attr, dev_dir_name, value);
  if (ret < 0) {
    syslog(LOG_INFO, "write_sysfs_int failed (%d)\n", 205);
  }
  free(dev_dir_name);
error_ret:
  return ret;
}


x86 assembly (clang, -O0):
<__write_devattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1080 <find_type_by_name@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11d1 <__write_devattr+0x71>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
xor    %eax,%eax
mov    0x2e19(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x38(%rbp)
jmp    124f <__write_devattr+0xef>
mov    -0x8(%rbp),%rdx
mov    -0x34(%rbp),%ecx
lea    -0x30(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1060 <asprintf@plt>
cmpl   $0x0,-0x20(%rbp)
je     120d <__write_devattr+0xad>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
call   1040 <write_sysfs_int2@plt>
mov    %eax,-0x38(%rbp)
jmp    1220 <__write_devattr+0xc0>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <write_sysfs_int@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1246 <__write_devattr+0xe6>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
mov    $0xcd,%edx
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__write_devattr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8d,%r13d
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe7c(%rip),%rsi
mov    %rbp,%rdi
call   1080 <find_type_by_name@plt>
test   %eax,%eax
js     11bd <__write_devattr+0x5d>
lea    0xe8b(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1060 <asprintf@plt>
mov    (%rsp),%rsi
mov    %r12,%rdi
mov    %r15d,%edx
test   %r13d,%r13d
je     11e4 <__write_devattr+0x84>
mov    %r14d,%ecx
call   1040 <write_sysfs_int2@plt>
jmp    11e9 <__write_devattr+0x89>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
mov    0x2e00(%rip),%rax
sub    (%rax),%ebx
jmp    1214 <__write_devattr+0xb4>
call   1070 <write_sysfs_int@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    120b <__write_devattr+0xab>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    $0xcd,%edx
xor    %eax,%eax
call   1050 <syslog@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"iio:device\00"
@LOG_INFO = global i32 0
@.str.1 = [23 x i8] c"Failed to find the %s\0A\00"
@ENODEV = global i32 0
@.str.2 = [15 x i8] c"%siio:device%d\00"
@.str.3 = [29 x i8] c"write_sysfs_int failed (%d)\0A\00"
define i32 @__write_devattr(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
store i32 0, i32* %15
%16 = load i8*, i8** %8
%17 = call i32 @find_type_by_name(i8* %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %14
%18 = load i32, i32* %14
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %6
%21 = load i32, i32* @LOG_INFO
%22 = load i8*, i8** %8
%23 = call i32 (i32, i8*, ...) @syslog(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @ENODEV
%25 = sub nsw i32 0, %24
store i32 %25, i32* %15
br label %52
26: ; preds = %6
%27 = load i8*, i8** %7
%28 = load i32, i32* %14
%29 = call i32 @asprintf(i8** %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32 %28)
%30 = load i32, i32* %11
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i8*, i8** %9
%34 = load i8*, i8** %13
%35 = load i32, i32* %10
%36 = load i32, i32* %12
%37 = call i32 @write_sysfs_int2(i8* %33, i8* %34, i32 %35, i32 %36)
store i32 %37, i32* %15
br label %43
38: ; preds = %26
%39 = load i8*, i8** %9
%40 = load i8*, i8** %13
%41 = load i32, i32* %10
%42 = call i32 @write_sysfs_int(i8* %39, i8* %40, i32 %41)
store i32 %42, i32* %15
br label %43
43: ; preds = %38, %32
%44 = load i32, i32* %15
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* @LOG_INFO
%48 = call i32 (i32, i8*, ...) @syslog(i32 %47, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 205)
br label %49
49: ; preds = %46, %43
%50 = load i8*, i8** %13
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %49, %20
%53 = load i32, i32* %15
ret i32 %53
}

LLVM IR (-O2):
@.str = [11 x i8] c"iio:device\00"
@LOG_INFO = global i32 0
@.str.1 = [23 x i8] c"Failed to find the %s\0A\00"
@ENODEV = global i32 0
@.str.2 = [15 x i8] c"%siio:device%d\00"
@.str.3 = [29 x i8] c"write_sysfs_int failed (%d)\0A\00"
define i32 @__write_devattr(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = bitcast i8** %7 to i8*
%9 = tail call i32 @find_type_by_name(i8* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* @LOG_INFO
%13 = tail call i32 (i32, i8*, ...) @syslog(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%14 = load i32, i32* @ENODEV
%15 = sub nsw i32 0, %14
br label %33
16: ; preds = %6
%17 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %9)
%18 = icmp eq i32 %4, 0
%19 = load i8*, i8** %7
br i1 %18, label %22, label %20
20: ; preds = %16
%21 = call i32 @write_sysfs_int2(i8* %2, i8* %19, i32 %3, i32 %5)
br label %24
22: ; preds = %16
%23 = call i32 @write_sysfs_int(i8* %2, i8* %19, i32 %3)
br label %24
24: ; preds = %22, %20
%25 = phi i32 [ %21, %20 ], [ %23, %22 ]
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @LOG_INFO
%29 = call i32 (i32, i8*, ...) @syslog(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 205)
br label %30
30: ; preds = %27, %24
%31 = load i8*, i8** %7
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %30, %11
%34 = phi i32 [ %15, %11 ], [ %25, %30 ]
ret i32 %34
}