#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int rc();
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main() {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(EXIT_FAILURE);
  }
  if ((chdir("/")) < 0) {
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  while (1) {
    if (rc() == 256) {
      FILE *fd = popen("deluged", "r");
      pclose(fd);
    }
    sleep(120);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11d3 <main+0x33>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11ec <main+0x4c>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1217 <main+0x77>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xde2(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    123b <main+0x9b>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1030 <rc@plt>
cmp    $0x100,%eax
jne    1297 <main+0xf7>
lea    0xd84(%rip),%rdi
lea    0xd85(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
mov    $0x78,%edi
call   10b0 <sleep@plt>
jmp    1265 <main+0xc5>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     124d <main+0xad>
jne    125b <main+0xbb>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     124d <main+0xad>
lea    0xe28(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     124d <main+0xad>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
lea    0xdef(%rip),%r14
lea    0xdf0(%rip),%rbx
jmp    122a <main+0x8a>
nopl   0x0(%rax)
mov    $0x78,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
call   1030 <rc@plt>
cmp    $0x100,%eax
jne    1220 <main+0x80>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <popen@plt>
mov    %rax,%rdi
call   1050 <pclose@plt>
jmp    1220 <main+0x80>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [8 x i8] c"deluged\00"
@.str.2 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_SUCCESS
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
%18 = call i32 @umask(i32 0)
%19 = call i64 (...) @setsid()
store i64 %19, i64* %3
%20 = load i64, i64* %3
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %17
%26 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %25
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* @STDERR_FILENO
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %31, %45
%39 = call i32 (...) @rc()
%40 = icmp eq i32 %39, 256
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = call i32* @popen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %42, i32** %4
%43 = load i32*, i32** %4
%44 = call i32 @pclose(i32* %43)
br label %45
45: ; preds = %41, %38
%46 = call i32 @sleep(i32 120)
br label %38
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [8 x i8] c"deluged\00"
@.str.2 = [2 x i8] c"r\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %6
%12 = tail call i32 @umask(i32 0)
%13 = tail call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %11
%19 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %18
%25 = load i32, i32* @STDIN_FILENO
%26 = tail call i32 @close(i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = tail call i32 @close(i32 %27)
%29 = load i32, i32* @STDERR_FILENO
%30 = tail call i32 @close(i32 %29)
br label %31
31: ; preds = %37, %24
%32 = tail call i32 (...) @rc()
%33 = icmp eq i32 %32, 256
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32* @popen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%36 = tail call i32 @pclose(i32* %35)
br label %37
37: ; preds = %34, %31
%38 = tail call i32 @sleep(i32 120)
br label %31
}