#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*/ LOG_ALERT;
int LOG_CONS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_ERR;
int LOG_PERROR;
int O_NDELAY;
int O_RDWR;
int /*<<< orphan*/ TIOCMBIS;
int /*<<< orphan*/ TIOCMGET;
int TIOCM_CAR;
int TIOCM_DSR;
int TIOCM_DTR;
int /*<<< orphan*/ closelog();
size_t errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int);
int /*<<< orphan*/ openlog(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ powerfail(int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *sys_errlist;
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int main(int argc, char **argv) {
  int fd;
  int dtr_bit = TIOCM_DTR;
  int flags;
  int status, oldstat = -1;
  int count = 0;
  int tries = 0;
  if (argc < 2) {
    fprintf(stderr, "Usage: powerd <device>\n");
    exit(1);
  }
  openlog("powerd", LOG_CONS | LOG_PERROR, LOG_DAEMON);
  if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
    syslog(LOG_ERR, "%s: %s", argv[1], sys_errlist[errno]);
    closelog();
    exit(1);
  }
  ioctl(fd, TIOCMBIS, &dtr_bit);
  switch (fork()) {
  case 0:
    closelog();
    setsid();
    break;
  case -1:
    syslog(LOG_ERR, "can't fork.");
    closelog();
    exit(1);
  default:
    closelog();
    exit(0);
  }
  openlog("powerd", LOG_CONS, LOG_DAEMON);
  while (1) {
    ioctl(fd, TIOCMGET, &flags);
    tries = 0;
    while ((flags & TIOCM_DSR) == 0) {
      if ((tries % 60) == 0)
        syslog(LOG_ALERT, "UPS connection error");
      sleep(2);
      tries++;
      ioctl(fd, TIOCMGET, &flags);
    }
    if (tries > 0)
      syslog(LOG_ALERT, "UPS connection OK");
    status = (flags & TIOCM_CAR);
    if (oldstat != 0 && status == 0) {
      count++;
      if (count > 3)
        powerfail(0);
      else {
        sleep(1);
        continue;
      }
    }
    if (oldstat == 0 && status > 0) {
      count++;
      if (count > 3)
        powerfail(1);
      else {
        sleep(1);
        continue;
      }
    }
    count = 0;
    oldstat = status;
    sleep(2);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1210 <main+0x60>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    0x2d90(%rip),%rax
or     (%rax),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
lea    0xde6(%rip),%rdi
call   10a0 <openlog@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    0x2d29(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12a9 <main+0xf9>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    0x2d7e(%rip),%rax
mov    (%rax),%rax
mov    0x2d04(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     12fb <main+0x14b>
jmp    12d8 <main+0x128>
mov    -0x30(%rbp),%eax
test   %eax,%eax
jne    1323 <main+0x173>
jmp    12e8 <main+0x138>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
jmp    1331 <main+0x181>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
lea    0xd1b(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2c48(%rip),%rax
mov    (%rax),%esi
mov    0x2c97(%rip),%rax
mov    (%rax),%edx
lea    0xcce(%rip),%rdi
call   10a0 <openlog@plt>
mov    -0x14(%rbp),%edi
mov    0x2c4f(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2c53(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    13d8 <main+0x228>
mov    -0x2c(%rbp),%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    13ab <main+0x1fb>
mov    0x2c35(%rip),%rax
mov    (%rax),%edi
lea    0xc8e(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2be0(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
jmp    136b <main+0x1bb>
cmpl   $0x0,-0x2c(%rbp)
jle    13f9 <main+0x249>
mov    0x2be7(%rip),%rax
mov    (%rax),%edi
lea    0xc55(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    -0x1c(%rbp),%eax
mov    0x2bbd(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     144f <main+0x29f>
cmpl   $0x0,-0x20(%rbp)
jne    144f <main+0x29f>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x3,-0x28(%rbp)
jle    143b <main+0x28b>
xor    %edi,%edi
call   1090 <powerfail@plt>
jmp    144a <main+0x29a>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    134f <main+0x19f>
jmp    144f <main+0x29f>
cmpl   $0x0,-0x24(%rbp)
jne    1499 <main+0x2e9>
cmpl   $0x0,-0x20(%rbp)
jle    1499 <main+0x2e9>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x3,-0x28(%rbp)
jle    1485 <main+0x2d5>
mov    $0x1,%edi
call   1090 <powerfail@plt>
jmp    1494 <main+0x2e4>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    134f <main+0x19f>
jmp    1499 <main+0x2e9>
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x2,%edi
call   10c0 <sleep@plt>
jmp    134f <main+0x19f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
cmp    $0x1,%edi
jle    1435 <main+0x285>
mov    %rsi,%r14
mov    0x2da2(%rip),%r15
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
or     (%r15),%esi
mov    0x2de7(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xe1d(%rip),%rdi
call   10a0 <openlog@plt>
mov    0x8(%r14),%rdi
mov    0x2dd5(%rip),%rax
mov    0x2d66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1454 <main+0x2a4>
mov    %eax,%ebx
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
jne    1417 <main+0x267>
xor    %eax,%eax
call   1030 <closelog@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
mov    (%r15),%esi
mov    0x0(%rbp),%edx
lea    0xdb4(%rip),%rdi
call   10a0 <openlog@plt>
mov    $0xffffffff,%ebp
lea    0x4(%rsp),%r14
mov    0x2d46(%rip),%r13
xor    %r12d,%r12d
jmp    1298 <main+0xe8>
add    $0x1,%r12d
xor    %r15d,%r15d
mov    $0x1,%edi
nopl   0x0(%rax,%rax,1)
call   10c0 <sleep@plt>
mov    %r15d,%ebp
cmp    $0x4,%r12d
mov    $0x3,%eax
cmovge %r12d,%eax
mov    %eax,0x10(%rsp)
mov    %r12d,(%rsp)
mov    %ebp,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
mov    0x4(%rsp),%r12d
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
test   %r12d,%eax
je     12f0 <main+0x140>
mov    %r12d,%r15d
jmp    13a0 <main+0x1f0>
xchg   %ax,%ax
mov    %eax,0xc(%rsp)
xor    %r13d,%r13d
xor    %ebp,%ebp
jmp    1335 <main+0x185>
nopl   0x0(%rax,%rax,1)
mov    $0x2,%edi
call   10c0 <sleep@plt>
add    $0x1,%ebp
mov    0x2c94(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
mov    0x4(%rsp),%r15d
add    $0xffffffff,%r13d
mov    0x2c98(%rip),%rax
test   %r15d,(%rax)
jne    1370 <main+0x1c0>
mov    %ebp,%eax
mov    $0x88888889,%ecx
imul   %rcx,%rax
shr    $0x25,%rax
imul   $0x3c,%eax,%eax
add    %r13d,%eax
jne    1300 <main+0x150>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
lea    0xcd6(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
jmp    1300 <main+0x150>
cs nopw 0x0(%rax,%rax,1)
nop
test   %r12d,0xc(%rsp)
mov    0x2c44(%rip),%r13
mov    0x8(%rsp),%ebp
jne    13a0 <main+0x1f0>
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
lea    0xcb5(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    0x4(%rsp),%r15d
xchg   %ax,%ax
and    0x0(%r13),%r15d
test   %ebp,%ebp
je     13d0 <main+0x220>
test   %r15d,%r15d
jne    13d0 <main+0x220>
mov    (%rsp),%eax
cmp    0x10(%rsp),%eax
je     1400 <main+0x250>
add    $0x1,%eax
mov    %eax,(%rsp)
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    12c0 <main+0x110>
nopl   0x0(%rax,%rax,1)
xor    %r12d,%r12d
mov    $0x2,%edi
test   %ebp,%ebp
jne    1290 <main+0xe0>
test   %r15d,%r15d
jle    1290 <main+0xe0>
mov    (%rsp),%r12d
cmp    $0x2,%r12d
jle    127f <main+0xcf>
mov    $0x1,%edi
jmp    1405 <main+0x255>
xchg   %ax,%ax
xor    %edi,%edi
xor    %r15d,%r15d
call   1090 <powerfail@plt>
xor    %r12d,%r12d
mov    $0x2,%edi
jmp    1290 <main+0xe0>
cmp    $0xffffffff,%eax
jne    1497 <main+0x2e7>
mov    0x2b45(%rip),%rax
mov    (%rax),%edi
lea    0xbfa(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
jmp    1486 <main+0x2d6>
mov    0x2b54(%rip),%rax
mov    (%rax),%edi
lea    0xbbb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2b0d(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
mov    0x2b90(%rip),%rax
mov    (%rax),%rax
mov    0x2b16(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xba0(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@TIOCM_DTR = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"Usage: powerd <device>\0A\00"
@.str.1 = [7 x i8] c"powerd\00"
@LOG_CONS = global i32 0
@LOG_PERROR = global i32 0
@LOG_DAEMON = global i32 0
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@LOG_ERR = global i32 0
@.str.2 = [7 x i8] c"%s: %s\00"
@sys_errlist = global i32* null
@errno = global i64 0
@TIOCMBIS = global i32 0
@.str.3 = [12 x i8] c"can't fork.\00"
@TIOCMGET = global i32 0
@TIOCM_DSR = global i32 0
@LOG_ALERT = global i32 0
@.str.4 = [21 x i8] c"UPS connection error\00"
@.str.5 = [18 x i8] c"UPS connection OK\00"
@TIOCM_CAR = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @TIOCM_DTR
store i32 %13, i32* %7
store i32 -1, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @LOG_CONS
%22 = load i32, i32* @LOG_PERROR
%23 = or i32 %21, %22
%24 = load i32, i32* @LOG_DAEMON
%25 = call i32 @openlog(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_NDELAY
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %28, i32 %31)
store i32 %32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %20
%35 = load i32, i32* @LOG_ERR
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = load i32*, i32** @sys_errlist
%40 = load i64, i64* @errno
%41 = getelementptr inbounds i32, i32* %39, i64 %40
%42 = load i32, i32* %41
%43 = call i32 (i32, i8*, ...) @syslog(i32 %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %42)
%44 = call i32 (...) @closelog()
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %20
%47 = load i32, i32* %6
%48 = load i32, i32* @TIOCMBIS
%49 = call i32 @ioctl(i32 %47, i32 %48, i32* %7)
%50 = call i32 (...) @fork()
switch i32 %50, label %59 [
i32 0, label %51
i32 -1, label %54
]
51: ; preds = %46
%52 = call i32 (...) @closelog()
%53 = call i32 (...) @setsid()
br label %62
54: ; preds = %46
%55 = load i32, i32* @LOG_ERR
%56 = call i32 (i32, i8*, ...) @syslog(i32 %55, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 (...) @closelog()
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %46
%60 = call i32 (...) @closelog()
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %51
%63 = load i32, i32* @LOG_CONS
%64 = load i32, i32* @LOG_DAEMON
%65 = call i32 @openlog(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %63, i32 %64)
br label %66
66: ; preds = %62, %111, %127, %130
%67 = load i32, i32* %6
%68 = load i32, i32* @TIOCMGET
%69 = call i32 @ioctl(i32 %67, i32 %68, i32* %8)
store i32 0, i32* %12
br label %70
70: ; preds = %82, %66
%71 = load i32, i32* %8
%72 = load i32, i32* @TIOCM_DSR
%73 = and i32 %71, %72
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %89
75: ; preds = %70
%76 = load i32, i32* %12
%77 = srem i32 %76, 60
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %75
%80 = load i32, i32* @LOG_ALERT
%81 = call i32 (i32, i8*, ...) @syslog(i32 %80, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %82
82: ; preds = %79, %75
%83 = call i32 @sleep(i32 2)
%84 = load i32, i32* %12
%85 = add nsw i32 %84, 1
store i32 %85, i32* %12
%86 = load i32, i32* %6
%87 = load i32, i32* @TIOCMGET
%88 = call i32 @ioctl(i32 %86, i32 %87, i32* %8)
br label %70
89: ; preds = %70
%90 = load i32, i32* %12
%91 = icmp sgt i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32, i32* @LOG_ALERT
%94 = call i32 (i32, i8*, ...) @syslog(i32 %93, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %95
95: ; preds = %92, %89
%96 = load i32, i32* %8
%97 = load i32, i32* @TIOCM_CAR
%98 = and i32 %96, %97
store i32 %98, i32* %9
%99 = load i32, i32* %10
%100 = icmp ne i32 %99, 0
br i1 %100, label %101, label %114
101: ; preds = %95
%102 = load i32, i32* %9
%103 = icmp eq i32 %102, 0
br i1 %103, label %104, label %114
104: ; preds = %101
%105 = load i32, i32* %11
%106 = add nsw i32 %105, 1
store i32 %106, i32* %11
%107 = load i32, i32* %11
%108 = icmp sgt i32 %107, 3
br i1 %108, label %109, label %111
109: ; preds = %104
%110 = call i32 @powerfail(i32 0)
br label %113
111: ; preds = %104
%112 = call i32 @sleep(i32 1)
br label %66
113: ; preds = %109
br label %114
114: ; preds = %113, %101, %95
%115 = load i32, i32* %10
%116 = icmp eq i32 %115, 0
br i1 %116, label %117, label %130
117: ; preds = %114
%118 = load i32, i32* %9
%119 = icmp sgt i32 %118, 0
br i1 %119, label %120, label %130
120: ; preds = %117
%121 = load i32, i32* %11
%122 = add nsw i32 %121, 1
store i32 %122, i32* %11
%123 = load i32, i32* %11
%124 = icmp sgt i32 %123, 3
br i1 %124, label %125, label %127
125: ; preds = %120
%126 = call i32 @powerfail(i32 1)
br label %129
127: ; preds = %120
%128 = call i32 @sleep(i32 1)
br label %66
129: ; preds = %125
br label %130
130: ; preds = %129, %117, %114
store i32 0, i32* %11
%131 = load i32, i32* %9
store i32 %131, i32* %10
%132 = call i32 @sleep(i32 2)
br label %66
}

LLVM IR (-O2):
@TIOCM_DTR = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"Usage: powerd <device>\0A\00"
@.str.1 = [7 x i8] c"powerd\00"
@LOG_CONS = global i32 0
@LOG_PERROR = global i32 0
@LOG_DAEMON = global i32 0
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@LOG_ERR = global i32 0
@.str.2 = [7 x i8] c"%s: %s\00"
@sys_errlist = global i32* null
@errno = global i64 0
@TIOCMBIS = global i32 0
@.str.3 = [12 x i8] c"can't fork.\00"
@TIOCMGET = global i32 0
@TIOCM_DSR = global i32 0
@LOG_ALERT = global i32 0
@.str.4 = [21 x i8] c"UPS connection error\00"
@.str.5 = [18 x i8] c"UPS connection OK\00"
@TIOCM_CAR = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @TIOCM_DTR
store i32 %6, i32* %3
%7 = bitcast i32* %4 to i8*
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @LOG_CONS
%15 = load i32, i32* @LOG_PERROR
%16 = or i32 %15, %14
%17 = load i32, i32* @LOG_DAEMON
%18 = tail call i32 @openlog(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_NDELAY
%23 = or i32 %22, %21
%24 = tail call i32 @open(i8* %20, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %13
%27 = load i32, i32* @LOG_ERR
%28 = load i8*, i8** %19
%29 = load i32*, i32** @sys_errlist
%30 = load i64, i64* @errno
%31 = getelementptr inbounds i32, i32* %29, i64 %30
%32 = load i32, i32* %31
%33 = tail call i32 (i32, i8*, ...) @syslog(i32 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %28, i32 %32)
%34 = tail call i32 (...) @closelog()
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %13
%37 = load i32, i32* @TIOCMBIS
%38 = call i32 @ioctl(i32 %24, i32 %37, i32* %3)
%39 = call i32 (...) @fork()
switch i32 %39, label %51 [
i32 0, label %40
i32 -1, label %46
]
40: ; preds = %36
%41 = call i32 (...) @closelog()
%42 = call i32 (...) @setsid()
%43 = load i32, i32* @LOG_CONS
%44 = load i32, i32* @LOG_DAEMON
%45 = call i32 @openlog(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %44)
br label %102
46: ; preds = %36
%47 = load i32, i32* @LOG_ERR
%48 = call i32 (i32, i8*, ...) @syslog(i32 %47, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 (...) @closelog()
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %36
%52 = call i32 (...) @closelog()
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %102, %91
%55 = phi i32 [ %92, %91 ], [ %104, %102 ]
%56 = load i32, i32* @TIOCMGET
%57 = call i32 @ioctl(i32 %24, i32 %56, i32* %4)
%58 = load i32, i32* %4
%59 = load i32, i32* @TIOCM_DSR
%60 = and i32 %59, %58
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %83
62: ; preds = %54, %69
%63 = phi i32 [ %71, %69 ], [ 0, %54 ]
%64 = urem i32 %63, 60
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @LOG_ALERT
%68 = call i32 (i32, i8*, ...) @syslog(i32 %67, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %66, %62
%70 = call i32 @sleep(i32 2)
%71 = add nuw nsw i32 %63, 1
%72 = load i32, i32* @TIOCMGET
%73 = call i32 @ioctl(i32 %24, i32 %72, i32* %4)
%74 = load i32, i32* %4
%75 = load i32, i32* @TIOCM_DSR
%76 = and i32 %75, %74
%77 = icmp eq i32 %76, 0
br i1 %77, label %62, label %78
78: ; preds = %69
br i1 %61, label %79, label %83
79: ; preds = %78
%80 = load i32, i32* @LOG_ALERT
%81 = call i32 (i32, i8*, ...) @syslog(i32 %80, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%82 = load i32, i32* %4
br label %83
83: ; preds = %54, %79, %78
%84 = phi i32 [ %82, %79 ], [ %74, %78 ], [ %58, %54 ]
%85 = load i32, i32* @TIOCM_CAR
%86 = and i32 %85, %84
%87 = icmp eq i32 %86, 0
%88 = select i1 %105, i1 %87, i1 false
br i1 %88, label %89, label %94
89: ; preds = %83
%90 = icmp eq i32 %55, %106
br i1 %90, label %107, label %91
91: ; preds = %89
%92 = add i32 %55, 1
%93 = call i32 @sleep(i32 1)
br label %54
94: ; preds = %83
%95 = icmp eq i32 %103, 0
%96 = icmp sgt i32 %86, 0
%97 = select i1 %95, i1 %96, i1 false
br i1 %97, label %98, label %111
98: ; preds = %94
%99 = icmp sgt i32 %55, 2
br i1 %99, label %107, label %100
100: ; preds = %98
%101 = add nsw i32 %55, 1
br label %111
102: ; preds = %111, %40
%103 = phi i32 [ -1, %40 ], [ %113, %111 ]
%104 = phi i32 [ 0, %40 ], [ %114, %111 ]
%105 = icmp ne i32 %103, 0
%106 = call i32 @llvm.smax.i32(i32 %104, i32 3)
br label %54
107: ; preds = %89, %98
%108 = phi i32 [ 1, %98 ], [ 0, %89 ]
%109 = phi i32 [ %86, %98 ], [ 0, %89 ]
%110 = call i32 @powerfail(i32 %108)
br label %111
111: ; preds = %94, %107, %100
%112 = phi i32 [ 1, %100 ], [ 2, %107 ], [ 2, %94 ]
%113 = phi i32 [ 0, %100 ], [ %109, %107 ], [ %86, %94 ]
%114 = phi i32 [ %101, %100 ], [ 0, %107 ], [ 0, %94 ]
%115 = call i32 @sleep(i32 %112)
br label %102
}