#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 timespec {
  long tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ write(int, char *, int);
int gpio_set_direction(unsigned int gpio, unsigned int in_flag) {
  int retry;
  struct timespec delay;
  int fd;
  char filename[33];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction",
           gpio);
  delay.tv_sec = 0;
  delay.tv_nsec = 10000000L;
  for (retry = 0; retry < 100; retry++) {
    if ((fd = open(filename, O_WRONLY)) >= 0)
      break;
    nanosleep(&delay, NULL);
  }
  if (retry >= 100)
    return -1;
  if (in_flag)
    write(fd, "in", 3);
  else
    write(fd, "out", 4);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x21,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x989680,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x64,-0x10(%rbp)
jge    11d5 <gpio_set_direction+0x85>
lea    -0x50(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jl     11ba <gpio_set_direction+0x6a>
jmp    11d5 <gpio_set_direction+0x85>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <nanosleep@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118d <gpio_set_direction+0x3d>
cmpl   $0x64,-0x10(%rbp)
jl     11eb <gpio_set_direction+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <gpio_set_direction+0xe1>
cmpl   $0x0,-0xc(%rbp)
je     120e <gpio_set_direction+0xbe>
mov    -0x24(%rbp),%edi
lea    0xe22(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
jmp    1222 <gpio_set_direction+0xd2>
mov    -0x24(%rbp),%edi
lea    0xe0c(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_direction>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r14d
mov    %edi,%ecx
lea    0xea6(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x21,%esi
call   1040 <snprintf@plt>
movaps 0xe7d(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    $0x64,%ebx
mov    0x2e45(%rip),%r13
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    11c6 <gpio_set_direction+0x76>
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <nanosleep@plt>
add    $0xffffffff,%ebx
jne    11a0 <gpio_set_direction+0x50>
mov    $0xffffffff,%eax
jmp    11f9 <gpio_set_direction+0xa9>
mov    %eax,%ebp
test   %r14d,%r14d
je     11dd <gpio_set_direction+0x8d>
lea    0xe5d(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
jmp    11eb <gpio_set_direction+0x9b>
lea    0xe50(%rip),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"in\00"
@.str.2 = [4 x i8] c"out\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timespec
%8 = alloca i32
%9 = alloca [33 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [33 x i8], [33 x i8]* %9, i64 0, i64 0
%11 = load i32, i32* %4
%12 = call i32 @snprintf(i8* %10, i32 33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
store i64 10000000, i64* %14
store i32 0, i32* %6
br label %15
15: ; preds = %26, %2
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 100
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = getelementptr inbounds [33 x i8], [33 x i8]* %9, i64 0, i64 0
%20 = load i32, i32* @O_WRONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %8
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %29
24: ; preds = %18
%25 = call i32 @nanosleep(%struct.timespec* %7, i32* null)
br label %26
26: ; preds = %24
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %15
29: ; preds = %23, %15
%30 = load i32, i32* %6
%31 = icmp sge i32 %30, 100
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %3
br label %45
33: ; preds = %29
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %8
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 3)
br label %42
39: ; preds = %33
%40 = load i32, i32* %8
%41 = call i32 @write(i32 %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %42
42: ; preds = %39, %36
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %32
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"in\00"
@.str.2 = [4 x i8] c"out\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca <2 x i64>
%4 = bitcast <2 x i64>* %3 to %struct.timespec*
%5 = alloca [33 x i8]
%6 = bitcast <2 x i64>* %3 to i8*
%7 = getelementptr inbounds [33 x i8], [33 x i8]* %5, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
store <2 x i64> <i64 10000000, i64 0>, <2 x i64>* %3
br label %9
9: ; preds = %2, %14
%10 = phi i32 [ 0, %2 ], [ %16, %14 ]
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %7, i32 %11)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
%16 = add nuw nsw i32 %10, 1
%17 = icmp eq i32 %16, 100
br i1 %17, label %26, label %9
18: ; preds = %9
%19 = icmp eq i32 %1, 0
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = call i32 @write(i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 3)
br label %24
22: ; preds = %18
%23 = call i32 @write(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %24
24: ; preds = %22, %20
%25 = call i32 @close(i32 %12)
br label %26
26: ; preds = %14, %24
%27 = phi i32 [ 0, %24 ], [ -1, %14 ]
ret i32 %27
}