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

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt(int, char **, char *);
char *optarg;
int /*<<< orphan*/ printf(char *);
int put_infos(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int main(int argc, char *argv[]) {
  char *path = "/sys/class/power_supply/BAT%i/uevent";
  char *format = "%s %i";
  int index = 0;
  int interval = 3;
  bool snoop = false;
  int opt;
  while ((opt = getopt(argc, argv, "hsf:i:p:n:")) != -1) {
    switch (opt) {
    case 'h':
      printf("battery [-h|-s|-f FORMAT|-i INTERVAL|-p PATH|-n INDEX]\n");
      exit(EXIT_SUCCESS);
      break;
    case 's':
      snoop = true;
      break;
    case 'f':
      format = optarg;
      break;
    case 'p':
      path = optarg;
      break;
    case 'i':
      interval = atoi(optarg);
      break;
    case 'n':
      index = atoi(optarg);
      break;
    }
  }
  char real_path[256] = {0};
  snprintf(real_path, sizeof(real_path), path, index);
  int exit_code;
  if (snoop)
    while ((exit_code = put_infos(real_path, format)) != EXIT_FAILURE)
      sleep(interval);
  else
    exit_code = put_infos(real_path, format);
  return exit_code;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe98(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xeb2(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x3,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
je     128c <main+0x10c>
mov    -0x30(%rbp),%eax
add    $0xffffff9a,%eax
mov    %eax,%ecx
mov    %rcx,-0x140(%rbp)
sub    $0xd,%eax
ja     1287 <main+0x107>
mov    -0x140(%rbp),%rax
lea    0xdf7(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x1,-0x2c(%rbp)
jmp    1287 <main+0x107>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1287 <main+0x107>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1287 <main+0x107>
mov    0x2d93(%rip),%rax
mov    (%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
jmp    1287 <main+0x107>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    11c4 <main+0x44>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x100,%esi
call   1040 <snprintf@plt>
cmpl   $0x0,-0x2c(%rbp)
je     12fd <main+0x17d>
jmp    12c6 <main+0x146>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <put_infos@plt>
mov    %eax,-0x134(%rbp)
mov    0x2cf5(%rip),%rcx
cmp    (%rcx),%eax
je     12f8 <main+0x178>
mov    -0x28(%rbp),%edi
call   10a0 <sleep@plt>
jmp    12c6 <main+0x146>
jmp    1313 <main+0x193>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <put_infos@plt>
mov    %eax,-0x134(%rbp)
mov    -0x134(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0xeab(%rip),%rax
mov    %rax,0x8(%rsp)
mov    $0x3,%r12d
movl   $0x0,(%rsp)
lea    0xeb7(%rip),%r13
lea    0xeb6(%rip),%r14
lea    0xe4c(%rip),%r15
movl   $0x0,0x4(%rsp)
jmp    11c5 <main+0x55>
xchg   %ax,%ax
cmp    $0xffffffff,%eax
je     1232 <main+0xc2>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1060 <getopt@plt>
lea    -0x66(%rax),%ecx
cmp    $0xd,%ecx
ja     11c0 <main+0x50>
movslq (%r15,%rcx,4),%rax
add    %r15,%rax
jmp    *%rax
mov    0x2e0e(%rip),%rax
mov    (%rax),%r13
jmp    11c5 <main+0x55>
mov    0x2e02(%rip),%rax
mov    (%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,%r12d
jmp    11c5 <main+0x55>
mov    0x2dee(%rip),%rax
mov    (%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,(%rsp)
jmp    11c5 <main+0x55>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
jmp    11c5 <main+0x55>
movl   $0x1,0x4(%rsp)
jmp    11c5 <main+0x55>
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    0x8(%rsp),%rdx
mov    (%rsp),%ecx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <put_infos@plt>
cmpl   $0x0,0x4(%rsp)
je     12f8 <main+0x188>
mov    0x2d05(%rip),%rbp
cmp    0x0(%rbp),%eax
je     12f8 <main+0x188>
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %r12d,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <put_infos@plt>
cmp    0x0(%rbp),%eax
jne    12e0 <main+0x170>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/class/power_supply/BAT%i/uevent\00"
@.str.1 = [6 x i8] c"%s %i\00"
@.str.2 = [11 x i8] c"hsf:i:p:n:\00"
@.str.3 = [56 x i8] c"battery [-h|-s|-f FORMAT|-i INTERVAL|-p PATH|-n INDEX]\0A\00"
@EXIT_SUCCESS = global i32 0
@optarg = global i8* null
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [256 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %7
store i32 0, i32* %8
store i32 3, i32* %9
store i32 0, i32* %10
br label %14
14: ; preds = %36, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call i32 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 %17, i32* %11
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %37
19: ; preds = %14
%20 = load i32, i32* %11
switch i32 %20, label %36 [
i32 104, label %21
i32 115, label %25
i32 102, label %26
i32 112, label %28
i32 105, label %30
i32 110, label %33
]
21: ; preds = %19
%22 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %19
store i32 1, i32* %10
br label %36
26: ; preds = %19
%27 = load i8*, i8** @optarg
store i8* %27, i8** %7
br label %36
28: ; preds = %19
%29 = load i8*, i8** @optarg
store i8* %29, i8** %6
br label %36
30: ; preds = %19
%31 = load i8*, i8** @optarg
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %9
br label %36
33: ; preds = %19
%34 = load i8*, i8** @optarg
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
br label %36
36: ; preds = %19, %33, %30, %28, %26, %25
br label %14
37: ; preds = %14
%38 = bitcast [256 x i8]* %12 to i8*
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%40 = load i8*, i8** %6
%41 = load i32, i32* %8
%42 = call i32 @snprintf(i8* %39, i32 256, i8* %40, i32 %41)
%43 = load i32, i32* %10
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %37
br label %46
46: ; preds = %52, %45
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%48 = load i8*, i8** %7
%49 = call i32 @put_infos(i8* %47, i8* %48)
store i32 %49, i32* %13
%50 = load i32, i32* @EXIT_FAILURE
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %46
%53 = load i32, i32* %9
%54 = call i32 @sleep(i32 %53)
br label %46
55: ; preds = %46
br label %60
56: ; preds = %37
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%58 = load i8*, i8** %7
%59 = call i32 @put_infos(i8* %57, i8* %58)
store i32 %59, i32* %13
br label %60
60: ; preds = %56, %55
%61 = load i32, i32* %13
ret i32 %61
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/class/power_supply/BAT%i/uevent\00"
@.str.1 = [6 x i8] c"%s %i\00"
@.str.2 = [11 x i8] c"hsf:i:p:n:\00"
@EXIT_SUCCESS = global i32 0
@optarg = global i8* null
@EXIT_FAILURE = global i32 0
@str = [55 x i8] c"battery [-h|-s|-f FORMAT|-i INTERVAL|-p PATH|-n INDEX]\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [256 x i8]
br label %4
4: ; preds = %30, %2
%5 = phi i8* [ %31, %30 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %2 ]
%6 = phi i32 [ %11, %30 ], [ 0, %2 ]
%7 = phi i32 [ %16, %30 ], [ 3, %2 ]
%8 = phi i32 [ %20, %30 ], [ 0, %2 ]
%9 = phi i8* [ %23, %30 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), %2 ]
br label %10
10: ; preds = %4, %37
%11 = phi i32 [ %6, %4 ], [ %39, %37 ]
%12 = phi i32 [ %7, %4 ], [ %16, %37 ]
%13 = phi i32 [ %8, %4 ], [ %20, %37 ]
%14 = phi i8* [ %9, %4 ], [ %23, %37 ]
br label %15
15: ; preds = %10, %34
%16 = phi i32 [ %12, %10 ], [ %36, %34 ]
%17 = phi i32 [ %13, %10 ], [ %20, %34 ]
%18 = phi i8* [ %14, %10 ], [ %23, %34 ]
br label %19
19: ; preds = %24, %15
%20 = phi i32 [ %17, %15 ], [ 1, %24 ]
%21 = phi i8* [ %18, %15 ], [ %23, %24 ]
br label %22
22: ; preds = %19, %32
%23 = phi i8* [ %21, %19 ], [ %33, %32 ]
br label %24
24: ; preds = %22, %24
%25 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
switch i32 %25, label %24 [
i32 -1, label %40
i32 104, label %26
i32 115, label %19
i32 102, label %30
i32 112, label %32
i32 105, label %34
i32 110, label %37
]
26: ; preds = %24
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_SUCCESS
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %24
%31 = load i8*, i8** @optarg
br label %4
32: ; preds = %24
%33 = load i8*, i8** @optarg
br label %22
34: ; preds = %24
%35 = load i8*, i8** @optarg
%36 = tail call i32 @atoi(i8* %35)
br label %15
37: ; preds = %24
%38 = load i8*, i8** @optarg
%39 = tail call i32 @atoi(i8* %38)
br label %10
40: ; preds = %24
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%42 = call i32 @snprintf(i8* %41, i32 256, i8* %23, i32 %11)
%43 = icmp eq i32 %20, 0
%44 = call i32 @put_infos(i8* %41, i8* %5)
%45 = load i32, i32* @EXIT_FAILURE
%46 = icmp eq i32 %44, %45
%47 = select i1 %43, i1 true, i1 %46
br i1 %47, label %53, label %48
48: ; preds = %40, %48
%49 = call i32 @sleep(i32 %16)
%50 = call i32 @put_infos(i8* %41, i8* %5)
%51 = load i32, i32* @EXIT_FAILURE
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %48
53: ; preds = %48, %40
%54 = phi i32 [ %44, %40 ], [ %50, %48 ]
ret i32 %54
}