#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 open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, ...);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int _gpio_offset(unsigned offset, const char *file) {
  int ret;
  int fd;
  char buf[32];
  fd = open(file, O_WRONLY);
  if (fd < 0) {
    printf("open file [%s] failed [%d]\n", file, errno);
    return -EINVAL;
  }
  sprintf(buf, "%d", offset);
  ret = write(fd, buf, strlen(buf));
  if (ret <= 0) {
    printf("write [%s] to file [%s] failed [%d]\n", buf, file, errno);
  } else {
    ret = 0;
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<_gpio_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bc <_gpio_offset+0x5c>
mov    -0x10(%rbp),%rsi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    0x2e26(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123a <_gpio_offset+0xda>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe52(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1225 <_gpio_offset+0xc5>
lea    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2dd0(%rip),%rax
mov    (%rax),%ecx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    122c <_gpio_offset+0xcc>
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    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_gpio_offset>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e0 <_gpio_offset+0x80>
mov    %eax,%ebp
lea    0xe8f(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %ebx,%edx
call   1080 <sprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
test   %eax,%eax
jg     11d7 <_gpio_offset+0x77>
mov    %eax,%r15d
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
lea    0xe58(%rip),%rdi
mov    %rsp,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1205 <_gpio_offset+0xa5>
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    0xe10(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd5(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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