#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *isIt(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
scalar_t__ strncmp(char *, char *, int);
void power_management_read_acpi_ac_adapter(int *result) {
  int adapter = 1;
  int done = 0;
  (*result) = 0;
  while (!done) {
    char fname[2048];
    FILE *proc;
    snprintf(fname, 2048, "%s/ac_adapter/%d/status", "/proc/acpi", adapter);
    proc = fopen(fname, "rt");
    if (proc) {
      while (!feof(proc)) {
        char line[2048], *ptr;
        fgets(line, 2048, proc);
        if ((ptr = isIt("Status:", line))) {
          if (strncmp(ptr, "on-line", 7) == 0) {
            (*result) = 1;
          }
        }
      }
      fclose(proc);
      if (*result)
        return;
    } else {
      return;
    }
    adapter++;
  }
}


x86 assembly (clang, -O0):
<power_management_read_acpi_ac_adapter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ad <power_management_read_acpi_ac_adapter+0x3d>
jmp    12c6 <power_management_read_acpi_ac_adapter+0x156>
lea    -0x810(%rbp),%rdi
mov    -0xc(%rbp),%r8d
mov    $0x800,%esi
lea    0xe3c(%rip),%rdx
lea    0xe4d(%rip),%rcx
call   1060 <snprintf@plt>
lea    -0x810(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0x0,-0x818(%rbp)
je     12b3 <power_management_read_acpi_ac_adapter+0x143>
jmp    11fd <power_management_read_acpi_ac_adapter+0x8d>
mov    -0x818(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121e <power_management_read_acpi_ac_adapter+0xae>
jmp    1290 <power_management_read_acpi_ac_adapter+0x120>
lea    -0x1020(%rbp),%rdi
mov    -0x818(%rbp),%rdx
mov    $0x800,%esi
call   1070 <fgets@plt>
lea    -0x1020(%rbp),%rsi
lea    0xde2(%rip),%rdi
call   1030 <isIt@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     128b <power_management_read_acpi_ac_adapter+0x11b>
mov    -0x1028(%rbp),%rdi
lea    0xdc6(%rip),%rsi
mov    $0x7,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1286 <power_management_read_acpi_ac_adapter+0x116>
mov    -0x8(%rbp),%rax
movl   $0x1,(%rax)
jmp    128b <power_management_read_acpi_ac_adapter+0x11b>
jmp    11fd <power_management_read_acpi_ac_adapter+0x8d>
mov    -0x818(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
je     12ae <power_management_read_acpi_ac_adapter+0x13e>
jmp    12c6 <power_management_read_acpi_ac_adapter+0x156>
jmp    12b8 <power_management_read_acpi_ac_adapter+0x148>
jmp    12c6 <power_management_read_acpi_ac_adapter+0x156>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1197 <power_management_read_acpi_ac_adapter+0x27>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<power_management_read_acpi_ac_adapter>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdi,%r13
movl   $0x0,(%rdi)
mov    $0x1,%r8d
lea    0x10(%rsp),%r14
lea    0xe8a(%rip),%r15
lea    0xe8b(%rip),%rbp
jmp    11cc <power_management_read_acpi_ac_adapter+0x5c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0xc(%rsp),%r8d
add    $0x1,%r8d
cmpl   $0x0,0x0(%r13)
jne    125b <power_management_read_acpi_ac_adapter+0xeb>
lea    0x810(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x800,%esi
lea    0xe1d(%rip),%rdx
lea    0xe2e(%rip),%rcx
mov    %r8d,0xc(%rsp)
call   1060 <snprintf@plt>
mov    %rbx,%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     125b <power_management_read_acpi_ac_adapter+0xeb>
mov    %rax,%r12
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    11b0 <power_management_read_acpi_ac_adapter+0x40>
mov    %r14,%rdi
mov    $0x800,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <isIt@plt>
test   %rax,%rax
je     1210 <power_management_read_acpi_ac_adapter+0xa0>
mov    %rax,%rdi
mov    %rbp,%rsi
mov    $0x7,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1210 <power_management_read_acpi_ac_adapter+0xa0>
movl   $0x1,0x0(%r13)
jmp    1210 <power_management_read_acpi_ac_adapter+0xa0>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"%s/ac_adapter/%d/status\00"
@.str.1 = [11 x i8] c"/proc/acpi\00"
@.str.2 = [3 x i8] c"rt\00"
@.str.3 = [8 x i8] c"Status:\00"
@.str.4 = [8 x i8] c"on-line\00"
define void @power_management_read_acpi_ac_adapter(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2048 x i8]
%6 = alloca i32*
%7 = alloca [2048 x i8]
%8 = alloca i8*
store i32* %0, i32** %2
store i32 1, i32* %3
store i32 0, i32* %4
%9 = load i32*, i32** %2
store i32 0, i32* %9
br label %10
10: ; preds = %52, %1
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
br i1 %13, label %14, label %55
14: ; preds = %10
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%16 = load i32, i32* %3
%17 = call i32 @snprintf(i8* %15, i32 2048, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %51
22: ; preds = %14
br label %23
23: ; preds = %42, %22
%24 = load i32*, i32** %6
%25 = call i32 @feof(i32* %24)
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br i1 %27, label %28, label %43
28: ; preds = %23
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%30 = load i32*, i32** %6
%31 = call i32 @fgets(i8* %29, i32 2048, i32* %30)
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%33 = call i8* @isIt(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %32)
store i8* %33, i8** %8
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %42
35: ; preds = %28
%36 = load i8*, i8** %8
%37 = call i64 @strncmp(i8* %36, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 7)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i32*, i32** %2
store i32 1, i32* %40
br label %41
41: ; preds = %39, %35
br label %42
42: ; preds = %41, %28
br label %23
43: ; preds = %23
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i32*, i32** %2
%47 = load i32, i32* %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
br label %55
50: ; preds = %43
br label %52
51: ; preds = %14
br label %55
52: ; preds = %50
%53 = load i32, i32* %3
%54 = add nsw i32 %53, 1
store i32 %54, i32* %3
br label %10
55: ; preds = %49, %51, %10
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"%s/ac_adapter/%d/status\00"
@.str.1 = [11 x i8] c"/proc/acpi\00"
@.str.2 = [3 x i8] c"rt\00"
@.str.3 = [8 x i8] c"Status:\00"
@.str.4 = [8 x i8] c"on-line\00"
define void @power_management_read_acpi_ac_adapter(i32* nocapture %0) {
%2 = alloca [2048 x i8]
%3 = alloca [2048 x i8]
store i32 0, i32* %0
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
br label %6
6: ; preds = %26, %1
%7 = phi i32 [ 1, %1 ], [ %30, %26 ]
%8 = call i32 @snprintf(i8* %4, i32 2048, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = call i32 @feof(i32* %9)
%13 = icmp eq i32 %12, 0
br i1 %13, label %15, label %26
14: ; preds = %6
br label %31
15: ; preds = %11, %23
%16 = call i32 @fgets(i8* %5, i32 2048, i32* %9)
%17 = call i8* @isIt(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%18 = icmp eq i8* %17, null
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 7)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 1, i32* %0
br label %23
23: ; preds = %19, %22, %15
%24 = call i32 @feof(i32* %9)
%25 = icmp eq i32 %24, 0
br i1 %25, label %15, label %26
26: ; preds = %23, %11
%27 = call i32 @fclose(i32* %9)
%28 = load i32, i32* %0
%29 = icmp eq i32 %28, 0
%30 = add nuw nsw i32 %7, 1
br i1 %29, label %6, label %31
31: ; preds = %26, %14
ret void
}