#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;
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int gpioReadValue(int gpio_fd, char *value, int value_size) {
  if (lseek(gpio_fd, 0, SEEK_SET) < 0) {
    return 0;
  }
  int r = read(gpio_fd, value, value_size);
  if (r > 1) {
    if (value[r - 1] == '\n')
      value[r - 1] = '\0';
    else
      value[r] = '\0';
  }
  return r;
}


x86 assembly (clang, -O0):
<gpioReadValue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    115b <gpioReadValue+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    11be <gpioReadValue+0x9e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jle    11b8 <gpioReadValue+0x98>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11a7 <gpioReadValue+0x87>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11b3 <gpioReadValue+0x93>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11b8 <gpioReadValue+0x98>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpioReadValue>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <lseek@plt>
test   %rax,%rax
js     116d <gpioReadValue+0x4d>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
mov    %eax,%ebx
cmp    $0x2,%eax
jl     116d <gpioReadValue+0x4d>
lea    -0x1(%rbx),%eax
cmpb   $0xa,(%r14,%rax,1)
mov    %ebx,%ecx
cmove  %rax,%rcx
movb   $0x0,(%r14,%rcx,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @gpioReadValue(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* @SEEK_SET
%11 = call i64 @lseek(i32 %9, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 0, i32* %4
br label %44
14: ; preds = %3
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = call i32 @read(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %42
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = load i8*, i8** %6
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
br label %41
36: ; preds = %21
%37 = load i8*, i8** %6
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %36, %30
br label %42
42: ; preds = %41, %14
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %13
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @gpioReadValue(i32 %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @SEEK_SET
%5 = tail call i64 @lseek(i32 %0, i32 0, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = add nsw i32 %8, -1
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
%16 = zext i32 %8 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = select i1 %15, i8* %13, i8* %17
store i8 0, i8* %18
br label %19
19: ; preds = %10, %7, %3
%20 = phi i32 [ 0, %3 ], [ %8, %7 ], [ %8, %10 ]
ret i32 %20
}