#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 EINVAL;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ goio_direction(unsigned int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
int gpio_set_value(unsigned offset, const char *value) {
  int ret;
  int fd;
  char file[128];
  goio_direction(offset, 1);
  sprintf(file,
          "/sys/class/gpio/"
          "gpio%d/value",
          offset);
  fd = open(file, O_WRONLY);
  if (fd < 0) {
    printf("open file [%s] failed [%d]\n", file, errno);
    return -EINVAL;
  }
  ret = write(fd, value, strlen(value));
  if (ret < 0) {
    printf("write [%s] to file [%s] failed [%d]\n", value, file, errno);
  } else {
    printf("set [%d] gpio to be %s\n", offset, value);
    ret = 0;
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1080 <goio_direction@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f8 <gpio_set_value+0x88>
lea    -0xa0(%rbp),%rsi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    0x2dea(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1287 <gpio_set_value+0x117>
mov    -0x18(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125d <gpio_set_value+0xed>
mov    -0x10(%rbp),%rsi
lea    -0xa0(%rbp),%rdx
mov    0x2d98(%rip),%rax
mov    (%rax),%ecx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1279 <gpio_set_value+0x109>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_value>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x1,%esi
call   1080 <goio_direction@plt>
lea    0xe6d(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1090 <sprintf@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e8 <gpio_set_value+0x78>
mov    %eax,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     120f <gpio_set_value+0x9f>
lea    0xe86(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    122e <gpio_set_value+0xbe>
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
lea    0xe25(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dcd(%rip),%rax
sub    (%rax),%ebx
jmp    1236 <gpio_set_value+0xc6>
mov    %eax,%ebx
mov    0x2dc8(%rip),%rax
mov    (%rax),%ecx
lea    0xe18(%rip),%rdi
mov    %rsp,%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [28 x i8] c"open file [%s] failed [%d]\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [37 x i8] c"write [%s] to file [%s] failed [%d]\0A\00"
@.str.3 = [24 x i8] c"set [%d] gpio to be %s\0A\00"
define i32 @gpio_set_value(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = call i32 @goio_direction(i32 %9, i32 1)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%12 = load i32, i32* %4
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%21 = load i32, i32* @errno
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %21)
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
store i32 %24, i32* %3
br label %46
25: ; preds = %2
%26 = load i32, i32* %7
%27 = load i8*, i8** %5
%28 = load i8*, i8** %5
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @write(i32 %26, i8* %27, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %25
%34 = load i8*, i8** %5
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %35, i32 %36)
br label %42
38: ; preds = %25
%39 = load i32, i32* %4
%40 = load i8*, i8** %5
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %39, i8* %40)
store i32 0, i32* %6
br label %42
42: ; preds = %38, %33
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %6
store i32 %45, i32* %3
br label %46
46: ; preds = %42, %19
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [28 x i8] c"open file [%s] failed [%d]\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [37 x i8] c"write [%s] to file [%s] failed [%d]\0A\00"
@.str.3 = [24 x i8] c"set [%d] gpio to be %s\0A\00"
define i32 @gpio_set_value(i32 %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @goio_direction(i32 %0, i32 1)
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 %11)
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %27
15: ; preds = %2
%16 = tail call i32 @strlen(i8* %1)
%17 = tail call i32 @write(i32 %8, i8* %1, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %4, i32 %20)
br label %24
22: ; preds = %15
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %0, i8* %1)
br label %24
24: ; preds = %22, %19
%25 = phi i32 [ %17, %19 ], [ 0, %22 ]
%26 = call i32 @close(i32 %8)
br label %27
27: ; preds = %24, %10
%28 = phi i32 [ %14, %10 ], [ %25, %24 ]
ret i32 %28
}