#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 *, 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 *);
char *strerror(int);
int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
                                 unsigned int num_lines, 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, %s\n", chrdev_name, strerror(errno));
    goto exit_free_name;
  }
  for (i = 0; i < num_lines; i++)
    req.lineoffsets[i] = lines[i];
  req.flags = flag;
  strcpy(req.consumer_label, consumer_label);
  req.lines = num_lines;
  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 %s (%d), %s\n",
            "GPIO_GET_LINEHANDLE_IOCTL", ret, strerror(errno));
  }
  if (close(fd) == -1)
    perror("Failed to close GPIO character device file");
exit_free_name:
  free(chrdev_name);
  return ret < 0 ? ret : req.fd;
}


x86 assembly (clang, -O0):
<gpiotools_request_linehandle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%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    0xe30(%rip),%rsi
call   1090 <asprintf@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11f5 <gpiotools_request_linehandle+0x55>
xor    %eax,%eax
mov    0x2de5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <gpiotools_request_linehandle+0x1e9>
mov    -0x58(%rbp),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    125a <gpiotools_request_linehandle+0xba>
xor    %eax,%eax
mov    0x2daa(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x64(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rdx
mov    %rax,%rcx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    135f <gpiotools_request_linehandle+0x1bf>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1291 <gpiotools_request_linehandle+0xf1>
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    1261 <gpiotools_request_linehandle+0xc1>
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    0x2d35(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12cf <gpiotools_request_linehandle+0x12f>
mov    -0x3c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x5c(%rbp),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    133d <gpiotools_request_linehandle+0x19d>
xor    %eax,%eax
mov    0x2cc6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%ecx
mov    %rax,%r8
lea    0xcf0(%rip),%rsi
lea    0xd06(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x5c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    135a <gpiotools_request_linehandle+0x1ba>
lea    0xd01(%rip),%rdi
call   10b0 <perror@plt>
jmp    135f <gpiotools_request_linehandle+0x1bf>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x64(%rbp)
jge    137d <gpiotools_request_linehandle+0x1dd>
mov    -0x64(%rbp),%eax
mov    %eax,-0x80(%rbp)
jmp    1383 <gpiotools_request_linehandle+0x1e3>
mov    -0x40(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    -0x80(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%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,%ebx
mov    %edx,%ebp
mov    %rdi,%rdx
lea    0xe40(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <asprintf@plt>
test   %eax,%eax
js     11f6 <gpiotools_request_linehandle+0x56>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1206 <gpiotools_request_linehandle+0x66>
test   %ebp,%ebp
je     1241 <gpiotools_request_linehandle+0xa1>
mov    %ebp,%eax
add    $0xffffffffffffffff,%rax
nopl   0x0(%rax)
add    $0xffffffffffffffff,%rax
jmp    11f0 <gpiotools_request_linehandle+0x50>
mov    0x2dd3(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    12df <gpiotools_request_linehandle+0x13f>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebx
neg    %ebx
mov    0x2dae(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rsp),%r14
call   10c0 <strerror@plt>
lea    0xddb(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12cd <gpiotools_request_linehandle+0x12d>
mov    %eax,%r15d
mov    %ebx,0x18(%rsp)
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %ebp,0x1c(%rsp)
mov    0x2d8d(%rip),%rax
test   %ebx,(%rax)
je     126c <gpiotools_request_linehandle+0xcc>
mov    %r14,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r15d,%edi
call   1050 <ioctl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    12c0 <gpiotools_request_linehandle+0x120>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebx
neg    %ebx
mov    0x2d2b(%rip),%rax
mov    (%rax),%ebp
call   10c0 <strerror@plt>
lea    0xd74(%rip),%rsi
lea    0xd8a(%rip),%rdx
mov    %ebp,%edi
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     12ee <gpiotools_request_linehandle+0x14e>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %ebx,%ebx
js     12df <gpiotools_request_linehandle+0x13f>
mov    0x20(%rsp),%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
call   10b0 <perror@plt>
jmp    12cd <gpiotools_request_linehandle+0x12d>

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 = [23 x i8] c"Failed to open %s, %s\0A\00"
@GPIOHANDLE_REQUEST_OUTPUT = global i32 0
@GPIO_GET_LINEHANDLE_IOCTL = global i32 0
@.str.2 = [29 x i8] c"Failed to issue %s (%d), %s\0A\00"
@.str.3 = [26 x i8] c"GPIO_GET_LINEHANDLE_IOCTL\00"
@.str.4 = [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 %109
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 %39
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 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* %37)
br label %97
39: ; preds = %26
store i32 0, i32* %17
br label %40
40: ; preds = %55, %39
%41 = load i32, i32* %17
%42 = load i32, i32* %10
%43 = icmp ult i32 %41, %42
br i1 %43, label %44, label %58
44: ; preds = %40
%45 = load i32*, i32** %9
%46 = load i32, i32* %17
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %48
%50 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 0
%51 = load i32*, i32** %50
%52 = load i32, i32* %17
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
store i32 %49, i32* %54
br label %55
55: ; preds = %44
%56 = load i32, i32* %17
%57 = add nsw i32 %56, 1
store i32 %57, i32* %17
br label %40
58: ; preds = %40
%59 = load i32, i32* %11
%60 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 1
store i32 %59, i32* %60
%61 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 5
%62 = load i32, i32* %61
%63 = load i8*, i8** %13
%64 = call i32 @strcpy(i32 %62, i8* %63)
%65 = load i32, i32* %10
%66 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 2
store i32 %65, i32* %66
%67 = load i32, i32* %11
%68 = load i32, i32* @GPIOHANDLE_REQUEST_OUTPUT
%69 = and i32 %67, %68
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %58
%72 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 4
%73 = load i32, i32* %72
%74 = load %struct.gpiohandle_data*, %struct.gpiohandle_data** %12
%75 = call i32 @memcpy(i32 %73, %struct.gpiohandle_data* %74, i32 4)
br label %76
76: ; preds = %71, %58
%77 = load i32, i32* %16
%78 = load i32, i32* @GPIO_GET_LINEHANDLE_IOCTL
%79 = call i32 @ioctl(i32 %77, i32 %78, %struct.gpiohandle_request* %14)
store i32 %79, i32* %18
%80 = load i32, i32* %18
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %90
82: ; preds = %76
%83 = load i32, i32* @errno
%84 = sub nsw i32 0, %83
store i32 %84, i32* %18
%85 = load i32, i32* @stderr
%86 = load i32, i32* %18
%87 = load i32, i32* @errno
%88 = call i8* @strerror(i32 %87)
%89 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %86, i8* %88)
br label %90
90: ; preds = %82, %76
%91 = load i32, i32* %16
%92 = call i32 @close(i32 %91)
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %96
94: ; preds = %90
%95 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %96
96: ; preds = %94, %90
br label %97
97: ; preds = %96, %31
%98 = load i8*, i8** %15
%99 = call i32 @free(i8* %98)
%100 = load i32, i32* %18
%101 = icmp slt i32 %100, 0
br i1 %101, label %102, label %104
102: ; preds = %97
%103 = load i32, i32* %18
br label %107
104: ; preds = %97
%105 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %14, i32 0, i32 3
%106 = load i32, i32* %105
br label %107
107: ; preds = %104, %102
%108 = phi i32 [ %103, %102 ], [ %106, %104 ]
store i32 %108, i32* %7
br label %109
109: ; preds = %107, %23
%110 = load i32, i32* %7
ret i32 %110
}

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 = [23 x i8] c"Failed to open %s, %s\0A\00"
@GPIOHANDLE_REQUEST_OUTPUT = global i32 0
@GPIO_GET_LINEHANDLE_IOCTL = global i32 0
@.str.2 = [29 x i8] c"Failed to issue %s (%d), %s\0A\00"
@.str.3 = [26 x i8] c"GPIO_GET_LINEHANDLE_IOCTL\00"
@.str.4 = [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 %68
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 %35, label %22
22: ; preds = %20
%23 = zext i32 %2 to i64
br label %31
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 i8* @strerror(i32 %25)
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %29)
br label %60
31: ; preds = %31, %22
%32 = phi i64 [ 0, %22 ], [ %33, %31 ]
%33 = add nuw nsw i64 %32, 1
%34 = icmp ne i64 %33, %23
br label %31
35: ; preds = %20
%36 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %7, i64 0, i32 1
store i32 %3, i32* %36
%37 = call i32 @strcpy(i32 undef, i8* %5)
%38 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %7, i64 0, i32 2
store i32 %2, i32* %38
%39 = load i32, i32* @GPIOHANDLE_REQUEST_OUTPUT
%40 = and i32 %39, %3
%41 = icmp eq i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %35
%43 = call i32 @memcpy(i32 undef, %struct.gpiohandle_data* %4, i32 4)
br label %44
44: ; preds = %42, %35
%45 = load i32, i32* @GPIO_GET_LINEHANDLE_IOCTL
%46 = call i32 @ioctl(i32 %18, i32 %45, %struct.gpiohandle_request* %7)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %54
48: ; preds = %44
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
%51 = load i32, i32* @stderr
%52 = call i8* @strerror(i32 %49)
%53 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %50, i8* %52)
br label %54
54: ; preds = %48, %44
%55 = phi i32 [ %50, %48 ], [ %46, %44 ]
%56 = call i32 @close(i32 %18)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %54, %58, %24
%61 = phi i32 [ %26, %24 ], [ %55, %58 ], [ %55, %54 ]
%62 = load i8*, i8** %8
%63 = call i32 @free(i8* %62)
%64 = icmp slt i32 %61, 0
%65 = getelementptr inbounds %struct.gpiohandle_request, %struct.gpiohandle_request* %7, i64 0, i32 3
%66 = load i32, i32* %65
%67 = select i1 %64, i32 %61, i32 %66
br label %68
68: ; preds = %60, %13
%69 = phi i32 [ %15, %13 ], [ %67, %60 ]
ret i32 %69
}