#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *, char *,
                         int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ is_critical();
int kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int nagbar_nag(void) {
  int status;
  pid_t pid = fork();
  if (pid == 0) {
    execl("/usr/bin/i3-nagbar", "i3-nagbar", "-t", "error", "-m",
          "Battery level is critical.", NULL);
    perror("exec");
  } else if (pid == -1) {
    return -1;
  }
  for (;;) {
    if (!is_critical()) {
      kill(pid, SIGTERM);
      wait(NULL);
      return 0;
    }
    waitpid(-1, &status, WNOHANG);
    if (kill(pid, 0) == -1) {
      return -1;
    }
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<nagbar_nag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11e6 <nagbar_nag+0x66>
lea    0xe5d(%rip),%rdi
lea    0xe5f(%rip),%rsi
lea    0xe62(%rip),%rdx
lea    0xe5e(%rip),%rcx
lea    0xe5d(%rip),%r8
lea    0xe59(%rip),%r9
xor    %eax,%eax
movq   $0x0,(%rsp)
call   1070 <execl@plt>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
jmp    1201 <nagbar_nag+0x81>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fc <nagbar_nag+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <nagbar_nag+0x101>
jmp    1201 <nagbar_nag+0x81>
jmp    1206 <nagbar_nag+0x86>
mov    $0x0,%al
call   1040 <is_critical@plt>
cmp    $0x0,%eax
jne    123c <nagbar_nag+0xbc>
mov    -0xc(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1030 <kill@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
movl   $0x0,-0x4(%rbp)
jmp    1281 <nagbar_nag+0x101>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1050 <waitpid@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <kill@plt>
cmp    $0xffffffff,%eax
jne    1272 <nagbar_nag+0xf2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <nagbar_nag+0x101>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1206 <nagbar_nag+0x86>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nagbar_nag>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     120e <nagbar_nag+0x8e>
test   %ebx,%ebx
je     121d <nagbar_nag+0x9d>
xor    %eax,%eax
call   1040 <is_critical@plt>
test   %eax,%eax
je     11f3 <nagbar_nag+0x73>
mov    0x2e2e(%rip),%r15
lea    0x14(%rsp),%r14
mov    $0xffffffff,%ebp
nopl   0x0(%rax)
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %r14,%rsi
call   1050 <waitpid@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <kill@plt>
cmp    $0xffffffff,%eax
je     1210 <nagbar_nag+0x90>
mov    $0x1,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1040 <is_critical@plt>
test   %eax,%eax
jne    11c0 <nagbar_nag+0x40>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <kill@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %ebp,%ebp
jmp    1210 <nagbar_nag+0x90>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movq   $0x0,(%rsp)
lea    0xdd4(%rip),%rdi
lea    0xdd6(%rip),%rsi
lea    0xdd9(%rip),%rdx
lea    0xdd5(%rip),%rcx
lea    0xdd4(%rip),%r8
lea    0xdd0(%rip),%r9
call   1070 <execl@plt>
lea    0xddf(%rip),%rdi
call   1060 <perror@plt>
jmp    11a0 <nagbar_nag+0x20>

LLVM IR (-O0):
@.str = [19 x i8] c"/usr/bin/i3-nagbar\00"
@.str.1 = [10 x i8] c"i3-nagbar\00"
@.str.2 = [3 x i8] c"-t\00"
@.str.3 = [6 x i8] c"error\00"
@.str.4 = [3 x i8] c"-m\00"
@.str.5 = [27 x i8] c"Battery level is critical.\00"
@.str.6 = [5 x i8] c"exec\00"
@SIGTERM = global i32 0
@WNOHANG = global i32 0
define i32 @nagbar_nag() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @execl(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32* null)
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %15
10: ; preds = %0
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 -1, i32* %1
br label %33
14: ; preds = %10
br label %15
15: ; preds = %14, %7
br label %16
16: ; preds = %31, %15
%17 = call i32 (...) @is_critical()
%18 = icmp ne i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = load i32, i32* %3
%21 = load i32, i32* @SIGTERM
%22 = call i32 @kill(i32 %20, i32 %21)
%23 = call i32 @wait(i32* null)
store i32 0, i32* %1
br label %33
24: ; preds = %16
%25 = load i32, i32* @WNOHANG
%26 = call i32 @waitpid(i32 -1, i32* %2, i32 %25)
%27 = load i32, i32* %3
%28 = call i32 @kill(i32 %27, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %1
br label %33
31: ; preds = %24
%32 = call i32 @sleep(i32 1)
br label %16
33: ; preds = %30, %19, %13
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [19 x i8] c"/usr/bin/i3-nagbar\00"
@.str.1 = [10 x i8] c"i3-nagbar\00"
@.str.2 = [3 x i8] c"-t\00"
@.str.3 = [6 x i8] c"error\00"
@.str.4 = [3 x i8] c"-m\00"
@.str.5 = [27 x i8] c"Battery level is critical.\00"
@.str.6 = [5 x i8] c"exec\00"
@SIGTERM = global i32 0
@WNOHANG = global i32 0
define i32 @nagbar_nag() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %7 [
i32 0, label %4
i32 -1, label %23
]
4: ; preds = %0
%5 = tail call i32 @execl(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32* null)
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %7
7: ; preds = %0, %4
%8 = call i32 (...) @is_critical()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %19, %7
%11 = load i32, i32* @SIGTERM
%12 = call i32 @kill(i32 %3, i32 %11)
%13 = call i32 @wait(i32* null)
br label %23
14: ; preds = %7, %19
%15 = load i32, i32* @WNOHANG
%16 = call i32 @waitpid(i32 -1, i32* %1, i32 %15)
%17 = call i32 @kill(i32 %3, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = call i32 @sleep(i32 1)
%21 = call i32 (...) @is_critical()
%22 = icmp eq i32 %21, 0
br i1 %22, label %10, label %14
23: ; preds = %14, %0, %10
%24 = phi i32 [ 0, %10 ], [ %3, %0 ], [ -1, %14 ]
ret i32 %24
}