#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 */
typedef int /*<<< orphan*/ vstr;
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ err;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ add_error_msg(char *);
int errno;
int fd_lookup(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int open_value_file(int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int strnlen(char *, int);
scalar_t__ write(int, char *, scalar_t__);
int gpio_set_value(int gpio, unsigned int value) {
  int fd = fd_lookup(gpio);
  int e_no;
  char filename[50];
  char vstr[16];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
  do {
    do {
      if (!(strnlen(filename, sizeof(filename)) < sizeof(filename) - 1)) {
        fprintf(stderr, "ASSRT failed at %s:%d (%s)\n",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/xtacocorex/CHIP_IO/refs/"
                "heads/master/source/event_gpio.c",
                487,
                "strnlen(filename, sizeof(filename)) < sizeof(filename)-1");
        fflush(stderr);
        abort();
      }
    } while (0);
  } while (0);
  if (!fd) {
    if ((fd = open_value_file(gpio)) == -1) {
      char err[256];
      snprintf(err, sizeof(err),
               "gpio_get_value: could not open GPIO %d value file", gpio);
      add_error_msg(err);
      return -1;
    }
  }
  if (value) {
    strncpy(vstr, "1", (sizeof(vstr) / sizeof(vstr[0])) - 1);
  } else {
    strncpy(vstr, "0", (sizeof(vstr) / sizeof(vstr[0])) - 1);
  }
  ssize_t s = write(fd, vstr, strlen(vstr));
  e_no = errno;
  if (s != strlen(vstr)) {
    char err[256];
    snprintf(err, sizeof(err),
             "gpio_set_value: could not write '%s' to %s (%s)", vstr, filename,
             strerror(e_no));
    add_error_msg(err);
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2a0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   10d0 <fd_lookup@plt>
mov    %eax,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x32,%esi
lea    0xe11(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
jmp    11fb <gpio_set_value+0x3b>
lea    -0x50(%rbp),%rdi
mov    $0x32,%esi
call   1080 <strnlen@plt>
cltq
cmp    $0x31,%rax
jb     1252 <gpio_set_value+0x92>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
lea    0xe0d(%rip),%rdx
mov    $0x1e7,%ecx
lea    0xe94(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1257 <gpio_set_value+0x97>
jmp    125c <gpio_set_value+0x9c>
cmpl   $0x0,-0x10(%rbp)
jne    12b4 <gpio_set_value+0xf4>
mov    -0x8(%rbp),%edi
call   10b0 <open_value_file@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    12af <gpio_set_value+0xef>
lea    -0x160(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe75(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x160(%rbp),%rdi
call   10a0 <add_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d2 <gpio_set_value+0x212>
jmp    12b4 <gpio_set_value+0xf4>
cmpl   $0x0,-0xc(%rbp)
je     12d8 <gpio_set_value+0x118>
lea    -0x60(%rbp),%rdi
lea    0xe3a(%rip),%rsi
mov    $0xf,%edx
call   1040 <strncpy@plt>
jmp    12ed <gpio_set_value+0x12d>
lea    -0x60(%rbp),%rdi
lea    0xe54(%rip),%rsi
mov    $0xf,%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x284(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x280(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x284(%rbp),%edi
mov    -0x280(%rbp),%rsi
mov    %rax,%rdx
call   1050 <write@plt>
mov    %rax,-0x168(%rbp)
mov    0x2cab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x168(%rbp),%rax
mov    %rax,-0x278(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x278(%rbp),%rax
cmp    %rcx,%rax
je     13cb <gpio_set_value+0x20b>
lea    -0x270(%rbp),%rax
mov    %rax,-0x2a0(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x298(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x290(%rbp)
mov    -0x14(%rbp),%edi
call   10e0 <strerror@plt>
mov    -0x2a0(%rbp),%rdi
mov    -0x298(%rbp),%rcx
mov    -0x290(%rbp),%r8
mov    %rax,%r9
mov    $0x100,%esi
lea    0xd8d(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x270(%rbp),%rdi
call   10a0 <add_error_msg@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13d2 <gpio_set_value+0x212>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_value>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x158,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
call   10d0 <fd_lookup@plt>
mov    %eax,%ebx
lea    0xe20(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x32,%esi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %r15,%rdi
mov    $0x32,%esi
call   1080 <strnlen@plt>
cmp    $0x31,%eax
jae    12ec <gpio_set_value+0x12c>
test   %ebx,%ebx
jne    1222 <gpio_set_value+0x62>
mov    %ebp,%edi
call   10b0 <open_value_file@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12b4 <gpio_set_value+0xf4>
test   %r14d,%r14d
lea    0xf0b(%rip),%rax
lea    0xed0(%rip),%rsi
cmove  %rax,%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xf,%edx
call   1040 <strncpy@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1050 <write@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rbx
je     12de <gpio_set_value+0x11e>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xeb3(%rip),%rdx
lea    0x50(%rsp),%rbx
mov    %rsp,%rcx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rax,%r9
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   10a0 <add_error_msg@plt>
mov    $0xfffffffe,%eax
jmp    12de <gpio_set_value+0x11e>
lea    0xe4a(%rip),%rdx
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   10a0 <add_error_msg@plt>
mov    $0xffffffff,%eax
add    $0x158,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ced(%rip),%rbx
mov    (%rbx),%edi
lea    0xd21(%rip),%rsi
lea    0xd36(%rip),%rdx
lea    0xdc2(%rip),%r8
mov    $0x1e7,%ecx
call   1090 <fprintf@plt>
mov    (%rbx),%edi
call   10c0 <fflush@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"ASSRT failed at %s:%d (%s)\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/xtacocorex/CHIP_IO/refs/heads/master/source/event_gpio.c\00"
@.str.3 = [57 x i8] c"strnlen(filename, sizeof(filename)) < sizeof(filename)-1\00"
@.str.4 = [50 x i8] c"gpio_get_value: could not open GPIO %d value file\00"
@.str.5 = [2 x i8] c"1\00"
@.str.6 = [2 x i8] c"0\00"
@errno = global i32 0
@.str.7 = [48 x i8] c"gpio_set_value: could not write '%s' to %s (%s)\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [50 x i8]
%9 = alloca [16 x i8]
%10 = alloca [256 x i8]
%11 = alloca i64
%12 = alloca [256 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%13 = load i32, i32* %4
%14 = call i32 @fd_lookup(i32 %13)
store i32 %14, i32* %6
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %15, i32 50, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %2
br label %19
19: ; preds = %18
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%21 = call i32 @strnlen(i8* %20, i32 50)
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, 49
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 487, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = call i32 @fflush(i32 %27)
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %19
br label %31
31: ; preds = %30
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %46, label %35
35: ; preds = %32
%36 = load i32, i32* %4
%37 = call i32 @open_value_file(i32 %36)
store i32 %37, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%41 = load i32, i32* %4
%42 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %40, i32 256, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%44 = call i32 @add_error_msg(i8* %43)
store i32 -1, i32* %3
br label %76
45: ; preds = %35
br label %46
46: ; preds = %45, %32
%47 = load i32, i32* %5
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%51 = call i32 @strncpy(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 15)
br label %55
52: ; preds = %46
%53 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%54 = call i32 @strncpy(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 15)
br label %55
55: ; preds = %52, %49
%56 = load i32, i32* %6
%57 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%58 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%59 = call i64 @strlen(i8* %58)
%60 = call i64 @write(i32 %56, i8* %57, i64 %59)
store i64 %60, i64* %11
%61 = load i32, i32* @errno
store i32 %61, i32* %7
%62 = load i64, i64* %11
%63 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%64 = call i64 @strlen(i8* %63)
%65 = icmp ne i64 %62, %64
br i1 %65, label %66, label %75
66: ; preds = %55
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%68 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%69 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%70 = load i32, i32* %7
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %67, i32 256, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i8* %68, i8* %69, i8* %71)
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%74 = call i32 @add_error_msg(i8* %73)
store i32 -2, i32* %3
br label %76
75: ; preds = %55
store i32 0, i32* %3
br label %76
76: ; preds = %75, %66, %39
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"ASSRT failed at %s:%d (%s)\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/xtacocorex/CHIP_IO/refs/heads/master/source/event_gpio.c\00"
@.str.3 = [57 x i8] c"strnlen(filename, sizeof(filename)) < sizeof(filename)-1\00"
@.str.4 = [50 x i8] c"gpio_get_value: could not open GPIO %d value file\00"
@.str.5 = [2 x i8] c"1\00"
@.str.6 = [2 x i8] c"0\00"
@errno = global i32 0
@.str.7 = [48 x i8] c"gpio_set_value: could not write '%s' to %s (%s)\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca [50 x i8]
%4 = alloca [16 x i8]
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = tail call i32 @fd_lookup(i32 %0)
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%10 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 50, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = call i32 @strnlen(i8* %8, i32 50)
%12 = icmp ult i32 %11, 49
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 487, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32, i32* @stderr
%17 = call i32 @fflush(i32 %16)
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %2
%20 = icmp eq i32 %7, 0
br i1 %20, label %21, label %28
21: ; preds = %19
%22 = call i32 @open_value_file(i32 %0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%26 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %25, i32 256, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%27 = call i32 @add_error_msg(i8* %25)
br label %43
28: ; preds = %21, %19
%29 = phi i32 [ %7, %19 ], [ %22, %21 ]
%30 = icmp eq i32 %1, 0
%31 = select i1 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0)
%32 = call i32 @strncpy(i8* %9, i8* %31, i32 15)
%33 = call i64 @strlen(i8* %9)
%34 = call i64 @write(i32 %29, i8* %9, i64 %33)
%35 = call i64 @strlen(i8* %9)
%36 = icmp eq i64 %34, %35
br i1 %36, label %43, label %37
37: ; preds = %28
%38 = load i32, i32* @errno
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%40 = call i8* @strerror(i32 %38)
%41 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %39, i32 256, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i8* %9, i8* %8, i8* %40)
%42 = call i32 @add_error_msg(i8* %39)
br label %43
43: ; preds = %37, %28, %24
%44 = phi i32 [ -1, %24 ], [ -2, %37 ], [ 0, %28 ]
ret i32 %44
}