#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int strtol(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int led_util_read_number(const char *path) {
  int res = -1;
  int fd = -1;
  char tmp[64];
  if ((fd = open(path, O_RDONLY)) == -1) {
    goto cleanup;
  }
  int rc = read(fd, tmp, sizeof tmp - 1);
  if (rc < 0) {
    goto cleanup;
  }
  tmp[rc] = 0;
  res = strtol(tmp, 0, 0);
cleanup:
  if (fd != -1)
    close(fd);
  return res;
}


x86 assembly (clang, -O0):
<led_util_read_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    117d <led_util_read_number+0x3d>
jmp    11b9 <led_util_read_number+0x79>
mov    -0x10(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3f,%edx
call   1040 <read@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    11a0 <led_util_read_number+0x60>
jmp    11b9 <led_util_read_number+0x79>
movslq -0x54(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <strtol@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     11cb <led_util_read_number+0x8b>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<led_util_read_number>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1184 <led_util_read_number+0x44>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x3f,%edx
call   1040 <read@plt>
test   %eax,%eax
js     118b <led_util_read_number+0x4b>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,%ebp
jmp    1190 <led_util_read_number+0x50>
mov    $0xffffffff,%ebp
jmp    1197 <led_util_read_number+0x57>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @led_util_read_number(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 -1, i32* %4
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
br label %25
12: ; preds = %1
%13 = load i32, i32* %4
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 63)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
br label %25
19: ; preds = %12
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%24 = call i32 @strtol(i8* %23, i32 0, i32 0)
store i32 %24, i32* %3
br label %25
25: ; preds = %19, %18, %11
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @led_util_read_number(i8* nocapture %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = call i32 @read(i32 %5, i8* %3, i32 63)
%9 = icmp slt i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 @strtol(i8* %3, i32 0, i32 0)
br label %14
14: ; preds = %7, %10
%15 = phi i32 [ %13, %10 ], [ -1, %7 ]
%16 = call i32 @close(i32 %5)
br label %17
17: ; preds = %1, %14
%18 = phi i32 [ %15, %14 ], [ -1, %1 ]
ret i32 %18
}