#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 */
struct gpiohandle_request {
  unsigned int *lineoffsets;
  unsigned int flags;
  unsigned int lines;
  int fd;
  int /*<<< orphan*/ default_values;
  int /*<<< orphan*/ consumer_label;
};
struct gpiohandle_data {
  int dummy;
};

/* Variables and functions */
int ENOMEM;
unsigned int GPIOHANDLE_REQUEST_OUTPUT;
int /*<<< orphan*/ GPIO_GET_LINEHANDLE_IOCTL;
int asprintf(char **, char *, char const *);
int close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int ioctl(int, int /*<<< orphan*/, struct gpiohandle_request *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, struct gpiohandle_data *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
                                 unsigned int nlines, unsigned int flag,
                                 struct gpiohandle_data *data,
                                 const char *consumer_label) {
  struct gpiohandle_request req;
  char *chrdev_name;
  int fd;
  int i;
  int ret;
  ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  if (ret < 0)
    return -ENOMEM;
  fd = open(chrdev_name, 0);
  if (fd == -1) {
    ret = -errno;
    fprintf(stderr, "Failed to open %s\n", chrdev_name);
    goto exit_close_error;
  }
  for (i = 0; i < nlines; i++)
    req.lineoffsets[i] = lines[i];
  req.flags = flag;
  strcpy(req.consumer_label, consumer_label);
  req.lines = nlines;
  if (flag & GPIOHANDLE_REQUEST_OUTPUT)
    memcpy(req.default_values, data, sizeof(req.default_values));
  ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
  if (ret == -1) {
    ret = -errno;
    fprintf(stderr, "Failed to issue GET LINEHANDLE IOCTL (%d)\n", ret);
  }
exit_close_error:
  if (close(fd) == -1)
    perror("Failed to close GPIO character device file");
  free(chrdev_name);
  return ret < 0 ? ret : req.fd;
}


x86 assembly (clang, -O0):
<gpiotools_request_linehandle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x58(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1090 <asprintf@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11e2 <gpiotools_request_linehandle+0x52>
xor    %eax,%eax
mov    0x2df8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1333 <gpiotools_request_linehandle+0x1a3>
mov    -0x58(%rbp),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    1228 <gpiotools_request_linehandle+0x98>
xor    %eax,%eax
mov    0x2dbd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x64(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rdx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12ec <gpiotools_request_linehandle+0x15c>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    125f <gpiotools_request_linehandle+0xcf>
mov    -0x18(%rbp),%rax
movslq -0x60(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x50(%rbp),%rax
movslq -0x60(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    122f <gpiotools_request_linehandle+0x9f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d67(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     129d <gpiotools_request_linehandle+0x10d>
mov    -0x3c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x5c(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    12e7 <gpiotools_request_linehandle+0x157>
xor    %eax,%eax
mov    0x2cf8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
mov    -0x64(%rbp),%edx
lea    0xd3b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12ec <gpiotools_request_linehandle+0x15c>
mov    -0x5c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1309 <gpiotools_request_linehandle+0x179>
lea    0xd42(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x64(%rbp)
jge    1327 <gpiotools_request_linehandle+0x197>
mov    -0x64(%rbp),%eax
mov    %eax,-0x68(%rbp)
jmp    132d <gpiotools_request_linehandle+0x19d>
mov    -0x40(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpiotools_request_linehandle>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %r9,%r12
mov    %r8,%r14
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rdi,%rdx
lea    0xe50(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <asprintf@plt>
test   %eax,%eax
js     11e6 <gpiotools_request_linehandle+0x56>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     11f6 <gpiotools_request_linehandle+0x66>
test   %ebx,%ebx
je     121f <gpiotools_request_linehandle+0x8f>
mov    %ebx,%eax
add    $0xffffffffffffffff,%rax
nopl   0x0(%rax)
add    $0xffffffffffffffff,%rax
jmp    11e0 <gpiotools_request_linehandle+0x50>
mov    0x2de3(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12a7 <gpiotools_request_linehandle+0x117>
mov    0x2dc3(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xdf2(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1288 <gpiotools_request_linehandle+0xf8>
mov    %ebp,0x18(%rsp)
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %ebx,0x1c(%rsp)
mov    0x2db2(%rip),%rax
test   %ebp,(%rax)
je     1247 <gpiotools_request_linehandle+0xb7>
mov    %r14,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r15d,%edi
call   1050 <ioctl@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1288 <gpiotools_request_linehandle+0xf8>
mov    0x2d55(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     12b6 <gpiotools_request_linehandle+0x126>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %ebp,%ebp
js     12a7 <gpiotools_request_linehandle+0x117>
mov    0x20(%rsp),%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd89(%rip),%rdi
call   10b0 <perror@plt>
jmp    1295 <gpiotools_request_linehandle+0x105>

LLVM IR (-O0):
%struct.gpiohandle_data = type { i32 }
%struct.gpiohandle_request = type { i32*, i32, i32, i32, i32, i32 }
@.str = [8 x i8] c"/dev/%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"Failed to open %s\0A\00"
@GPIOHANDLE_REQUEST_OUTPUT = global i32 0
@GPIO_GET_LINEHANDLE_IOCTL = global i32 0
@.str.2 = [43 x i8] c"Failed to issue GET LINEHANDLE IOCTL (%d)\0A\00"
@.str.3 = [43 x i8] c"Failed to close GPIO character device file\00"
define i32 @gpiotools_request_linehandle(i8* %0, i32* %1, i32 %2, i32 %3, %struct.gpiohandle_data* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.gpiohandle_data*
%13 = alloca i8*
%14 = alloca %struct.gpiohandle_request
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %8
store i32* %1, i32** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store %struct.gpiohandle_data* %4, %struct.gpiohandle_data** %12
store i8* %5, i8** %13
%19 = load i8*, i8** %8
%20 = call i32 @asprintf(i8** %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 %20, i32* %18
%21 = load i32, i32* %18
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %6
%24 = load i32, i32* @ENOMEM
%25 = sub nsw i32 0, %24
store i32 %25, i32* %7
br label %105
26: ; preds = %6
%27 = load i8*, i8** %15
%28 = call i32 @open(i8* %27, i32 0)
store i32 %28, i32* %16
%29 = load i32, i32* %16
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
store i32 %33, i32* %18
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %15
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %87
37: ; preds = %26
store i32 0, i32* %17
br label %38
38: ; preds = %53, %37
%39 = load i32, i32* %17
%40 = load i32, i32* %10
%41 = icmp ult i32 %39, %40
br i1 %41, label %42, label %56
42: ; preds = %38
%43 = load i32*, i32** %9
%44 = load i32, i32* %17
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 0
%49 = load i32*, i32** %48
%50 = load i32, i32* %17
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
store i32 %47, i32* %52
br label %53
53: ; preds = %42
%54 = load i32, i32* %17
%55 = add nsw i32 %54, 1
store i32 %55, i32* %17
br label %38
56: ; preds = %38
%57 = load i32, i32* %11
%58 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 1
store i32 %57, i32* %58
%59 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 5
%60 = load i32, i32* %59
%61 = load i8*, i8** %13
%62 = call i32 @strcpy(i32 %60, i8* %61)
%63 = load i32, i32* %10
%64 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 2
store i32 %63, i32* %64
%65 = load i32, i32* %11
%66 = load i32, i32* @GPIOHANDLE_REQUEST_OUTPUT
%67 = and i32 %65, %66
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %56
%70 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 4
%71 = load i32, i32* %70
%72 = load %struct.gpiohandle_data*, %struct.gpiohandle_data** %12
%73 = call i32 @memcpy(i32 %71, %struct.gpiohandle_data* %72, i32 4)
br label %74
74: ; preds = %69, %56
%75 = load i32, i32* %16
%76 = load i32, i32* @GPIO_GET_LINEHANDLE_IOCTL
%77 = call i32 @ioctl(i32 %75, i32 %76, %struct.gpiohandle_request* %14)
store i32 %77, i32* %18
%78 = load i32, i32* %18
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %86
80: ; preds = %74
%81 = load i32, i32* @errno
%82 = sub nsw i32 0, %81
store i32 %82, i32* %18
%83 = load i32, i32* @stderr
%84 = load i32, i32* %18
%85 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %84)
br label %86
86: ; preds = %80, %74
br label %87
87: ; preds = %86, %31
%88 = load i32, i32* %16
%89 = call i32 @close(i32 %88)
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %93
91: ; preds = %87
%92 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %93
93: ; preds = %91, %87
%94 = load i8*, i8** %15
%95 = call i32 @free(i8* %94)
%96 = load i32, i32* %18
%97 = icmp slt i32 %96, 0
br i1 %97, label %98, label %100
98: ; preds = %93
%99 = load i32, i32* %18
br label %103
100: ; preds = %93
%101 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 3
%102 = load i32, i32* %101
br label %103
103: ; preds = %100, %98
%104 = phi i32 [ %99, %98 ], [ %102, %100 ]
store i32 %104, i32* %7
br label %105
105: ; preds = %103, %23
%106 = load i32, i32* %7
ret i32 %106
}

LLVM IR (-O2):
%struct.gpiohandle_data = type { i32 }
%struct.gpiohandle_request = type { i32*, i32, i32, i32, i32, i32 }
@.str = [8 x i8] c"/dev/%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"Failed to open %s\0A\00"
@GPIOHANDLE_REQUEST_OUTPUT = global i32 0
@GPIO_GET_LINEHANDLE_IOCTL = global i32 0
@.str.2 = [43 x i8] c"Failed to issue GET LINEHANDLE IOCTL (%d)\0A\00"
@.str.3 = [43 x i8] c"Failed to close GPIO character device file\00"
define i32 @gpiotools_request_linehandle(i8* %0, i32* nocapture %1, i32 %2, i32 %3, %struct.gpiohandle_data* %4, i8* %5) {
%7 = alloca %struct.gpiohandle_request
%8 = alloca i8*
%9 = bitcast %struct.gpiohandle_request* %7 to i8*
%10 = bitcast i8** %8 to i8*
%11 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = load i32, i32* @ENOMEM
%15 = sub nsw i32 0, %14
br label %65
16: ; preds = %6
%17 = load i8*, i8** %8
%18 = call i32 @open(i8* %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = icmp eq i32 %2, 0
br i1 %21, label %34, label %22
22: ; preds = %20
%23 = zext i32 %2 to i64
br label %30
24: ; preds = %16
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %8
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %52
30: ; preds = %30, %22
%31 = phi i64 [ 0, %22 ], [ %32, %30 ]
%32 = add nuw nsw i64 %31, 1
%33 = icmp ne i64 %32, %23
br label %30
34: ; preds = %20
%35 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %7, i64 0, i32 1
store i32 %3, i32* %35
%36 = call i32 @strcpy(i32 undef, i8* %5)
%37 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %7, i64 0, i32 2
store i32 %2, i32* %37
%38 = load i32, i32* @GPIOHANDLE_REQUEST_OUTPUT
%39 = and i32 %38, %3
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %34
%42 = call i32 @memcpy(i32 undef, %struct.gpiohandle_data* %4, i32 4)
br label %43
43: ; preds = %41, %34
%44 = load i32, i32* @GPIO_GET_LINEHANDLE_IOCTL
%45 = call i32 @ioctl(i32 %18, i32 %44, %struct.gpiohandle_request* %7)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %52
52: ; preds = %43, %47, %24
%53 = phi i32 [ %26, %24 ], [ %49, %47 ], [ %45, %43 ]
%54 = call i32 @close(i32 %18)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = load i8*, i8** %8
%60 = call i32 @free(i8* %59)
%61 = icmp slt i32 %53, 0
%62 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %7, i64 0, i32 3
%63 = load i32, i32* %62
%64 = select i1 %61, i32 %53, i32 %63
br label %65
65: ; preds = %58, %13
%66 = phi i32 [ %15, %13 ], [ %64, %58 ]
ret i32 %66
}