#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*/ W_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int lngpio_wait_for_pin(int pin) {
  char path[35];
  int max_retries = 20;
  int count = 0;
  snprintf(path, 35, "/sys/class/gpio/gpio%d/direction", pin);
  while (1) {
    if (count++ >= max_retries) {
      fprintf(stderr, "Pin %d not exported!\n", pin);
      return (-1);
    }
    if (access(path, W_OK) == 0) {
      break;
    }
    usleep(100000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<lngpio_wait_for_pin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x14,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x23,%esi
lea    0xe94(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x38(%rbp)
cmp    -0x34(%rbp),%eax
jl     11a9 <lngpio_wait_for_pin+0x69>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe89(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <lngpio_wait_for_pin+0xa0>
lea    -0x30(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
jne    11ca <lngpio_wait_for_pin+0x8a>
jmp    11d9 <lngpio_wait_for_pin+0x99>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
jmp    1171 <lngpio_wait_for_pin+0x31>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lngpio_wait_for_pin>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r14d
lea    0xeac(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x23,%esi
mov    %r14d,%ecx
call   1030 <snprintf@plt>
mov    $0x14,%ebp
mov    0x2e75(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rbx),%esi
mov    %r15,%rdi
call   1050 <access@plt>
test   %rax,%rax
je     11bd <lngpio_wait_for_pin+0x7d>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
add    $0xffffffff,%ebp
jne    1180 <lngpio_wait_for_pin+0x40>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    %r14d,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11bf <lngpio_wait_for_pin+0x7f>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Pin %d not exported!\0A\00"
@W_OK = global i32 0
define i32 @lngpio_wait_for_pin(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [35 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 20, i32* %5
store i32 0, i32* %6
%7 = getelementptr inbounds [35 x i8], [35 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %1, %25
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
store i32 %12, i32* %6
%13 = load i32, i32* %5
%14 = icmp sge i32 %11, %13
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i32, i32* %3
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 -1, i32* %2
br label %28
19: ; preds = %10
%20 = getelementptr inbounds [35 x i8], [35 x i8]* %4, i64 0, i64 0
%21 = load i32, i32* @W_OK
%22 = call i64 @access(i8* %20, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %27
25: ; preds = %19
%26 = call i32 @usleep(i32 100000)
br label %10
27: ; preds = %24
store i32 0, i32* %2
br label %28
28: ; preds = %27, %15
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Pin %d not exported!\0A\00"
@W_OK = global i32 0
define i32 @lngpio_wait_for_pin(i32 %0) {
%2 = alloca [35 x i8]
%3 = getelementptr inbounds [35 x i8], [35 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %8
5: ; preds = %13
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %17
8: ; preds = %1, %13
%9 = phi i32 [ 1, %1 ], [ %15, %13 ]
%10 = load i32, i32* @W_OK
%11 = call i64 @access(i8* %3, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %8
%14 = call i32 @usleep(i32 100000)
%15 = add nuw nsw i32 %9, 1
%16 = icmp eq i32 %15, 21
br i1 %16, label %5, label %8
17: ; preds = %8, %5
%18 = phi i32 [ -1, %5 ], [ 0, %8 ]
ret i32 %18
}