#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*/ uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int gpio_get_val_fd(int);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ set_gpio_val_by_fd(int, int /*<<< orphan*/);
int set_gpio_val(uint32_t gpio, uint8_t val) {
  int fd, ret = 0;
  fd = gpio_get_val_fd(gpio);
  if (fd < 0) {
    printf("GPIO %d: failed to get value control\n", gpio);
    ret = 1;
    goto out;
  }
  if (set_gpio_val_by_fd(fd, val) != 0) {
    printf("GPIO %d: failed to set value\n", gpio);
    ret = 1;
  }
  close(fd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<set_gpio_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <gpio_get_val_fd@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1185 <set_gpio_val+0x45>
mov    -0x4(%rbp),%esi
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x10(%rbp)
jmp    11b8 <set_gpio_val+0x78>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <set_gpio_val_by_fd@plt>
cmp    $0x0,%rax
je     11b0 <set_gpio_val+0x70>
mov    -0x4(%rbp),%esi
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gpio_val>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
call   1060 <gpio_get_val_fd@plt>
test   %eax,%eax
js     1178 <set_gpio_val+0x38>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14d,%esi
call   1050 <set_gpio_val_by_fd@plt>
test   %rax,%rax
je     118d <set_gpio_val+0x4d>
lea    0xebc(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    $0x1,%ebp
jmp    118f <set_gpio_val+0x4f>
lea    0xe81(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    $0x1,%ebp
jmp    1196 <set_gpio_val+0x56>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"GPIO %d: failed to get value control\0A\00"
@.str.1 = [30 x i8] c"GPIO %d: failed to set value\0A\00"
define i32 @set_gpio_val(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %6
%7 = load i32, i32* %3
%8 = call i32 @gpio_get_val_fd(i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* %3
%13 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 1, i32* %6
br label %25
14: ; preds = %2
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = call i64 @set_gpio_val_by_fd(i32 %15, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %3
%21 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 1, i32* %6
br label %22
22: ; preds = %19, %14
%23 = load i32, i32* %5
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22, %11
%26 = load i32, i32* %6
ret i32 %26
}

LLVM IR (-O2):
@.str = [38 x i8] c"GPIO %d: failed to get value control\0A\00"
@.str.1 = [30 x i8] c"GPIO %d: failed to set value\0A\00"
define i32 @set_gpio_val(i32 %0, i32 %1) {
%3 = tail call i32 @gpio_get_val_fd(i32 %0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %15
7: ; preds = %2
%8 = tail call i64 @set_gpio_val_by_fd(i32 %3, i32 %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ 1, %10 ], [ 0, %7 ]
%14 = tail call i32 @close(i32 %3)
br label %15
15: ; preds = %12, %5
%16 = phi i32 [ 1, %5 ], [ %13, %12 ]
ret i32 %16
}