#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ get_first_file_in_a_directory(char *, char *, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
char *strerror(int /*<<< orphan*/);
int open_acpi_temperature(const char *name) {
  char path[256];
  char buf[64];
  int fd;
  if (name == NULL || strcmp(name, "*") == 0) {
    static int rep;
    if (!get_first_file_in_a_directory("/proc/acpi/thermal_zone/", buf, &rep))
      return -1;
    name = buf;
  }
  snprintf(path, 255, "/proc/acpi/thermal_zone/%s/temperature", name);
  fd = open(path, O_RDONLY);
  if (fd < 0)
    fprintf(stderr,
            "torsmo: "
            "can't open '%s': %s"
            "\n",
            path, strerror(errno));
  return fd;
}


x86 assembly (clang, -O0):
<open_acpi_temperature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1194 <open_acpi_temperature+0x34>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11ce <open_acpi_temperature+0x6e>
lea    -0x150(%rbp),%rsi
lea    0xe60(%rip),%rdi
lea    0x2eab(%rip),%rdx
call   1040 <get_first_file_in_a_directory@plt>
cmp    $0x0,%eax
jne    11c3 <open_acpi_temperature+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <open_acpi_temperature+0x102>
lea    -0x150(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xe36(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0x0,-0x154(%rbp)
jge    1259 <open_acpi_temperature+0xf9>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x164(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x160(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x164(%rbp),%edi
mov    -0x160(%rbp),%rdx
mov    %rax,%rcx
lea    0xdee(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x154(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_acpi_temperature>:
push   %rbp
push   %r14
push   %rbx
sub    $0x140,%rsp
test   %rdi,%rdi
je     1184 <open_acpi_temperature+0x24>
mov    %rdi,%rbx
lea    0xe86(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11a1 <open_acpi_temperature+0x41>
mov    %rsp,%rbx
lea    0xe74(%rip),%rdi
lea    0x2ebf(%rip),%rdx
mov    %rbx,%rsi
call   1040 <get_first_file_in_a_directory@plt>
test   %eax,%eax
je     1203 <open_acpi_temperature+0xa3>
lea    0xe73(%rip),%rdx
lea    0x40(%rsp),%r14
mov    %r14,%rdi
mov    $0xff,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1208 <open_acpi_temperature+0xa8>
mov    0x2dfd(%rip),%rax
mov    (%rax),%ebp
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe50(%rip),%rsi
lea    0x40(%rsp),%rdx
mov    %ebp,%edi
mov    %rax,%rcx
call   1060 <fprintf@plt>
jmp    1208 <open_acpi_temperature+0xa8>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x140,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"*\00"
@open_acpi_temperature.rep = internal global i32 0
@.str.1 = [25 x i8] c"/proc/acpi/thermal_zone/\00"
@.str.2 = [39 x i8] c"/proc/acpi/thermal_zone/%s/temperature\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [29 x i8] c"torsmo: can't open '%s': %s\0A\00"
@errno = global i32 0
define i32 @open_acpi_temperature(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca [64 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %9, %1
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%15 = call i32 @get_first_file_in_a_directory(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32* @open_acpi_temperature.rep)
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %13
store i32 -1, i32* %2
br label %37
18: ; preds = %13
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
store i8* %19, i8** %3
br label %20
20: ; preds = %18, %9
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%22 = load i8*, i8** %3
%23 = call i32 @snprintf(i8* %21, i32 255, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %31, i8* %33)
br label %35
35: ; preds = %29, %20
%36 = load i32, i32* %6
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %17
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"*\00"
@open_acpi_temperature.rep = internal global i32 0
@.str.1 = [25 x i8] c"/proc/acpi/thermal_zone/\00"
@.str.2 = [39 x i8] c"/proc/acpi/thermal_zone/%s/temperature\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [29 x i8] c"torsmo: can't open '%s': %s\0A\00"
@errno = global i32 0
define i32 @open_acpi_temperature(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = icmp eq i8* %0, null
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7, %1
%11 = call i32 @get_first_file_in_a_directory(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32* @open_acpi_temperature.rep)
%12 = icmp eq i32 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %10, %7
%14 = phi i8* [ %0, %7 ], [ %5, %10 ]
%15 = call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %4, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %4, i8* %22)
br label %24
24: ; preds = %13, %19, %10
%25 = phi i32 [ -1, %10 ], [ %17, %19 ], [ %17, %13 ]
ret i32 %25
}