#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 */
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ call_self_nocheckpoint(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ kill_parent();
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ unsetenv(char *);
int main(int argc, char *argv[]) {
  char *preload = getenv("LD_PRELOAD");
  if (argv[1] && strcmp(argv[1], "--called-as-nocheckpoint") == 0) {
    if (preload && strlen(preload) > 0) {
      kill_parent();
    }
    return 0;
  }
  if (argv[1] &&
      strcmp(argv[1], "--called-as-nocheckpoint-with-user-LD_PRELOAD") == 0) {
    if (preload == NULL || strcmp(preload, "USER_STRING") != 0) {
      kill_parent();
    }
    return 0;
  }
  struct timespec tenth_second;
  tenth_second.tv_sec = 0;
  tenth_second.tv_nsec = 100000000;
  int i = 0;
  while (1) {
    unsetenv("LD_PRELOAD");
    call_self_nocheckpoint(argv[0]);
    setenv("LD_PRELOAD", "", 1);
    call_self_nocheckpoint(argv[0]);
    setenv("LD_PRELOAD", "USER_STRING", 1);
    call_self_nocheckpoint(argv[0]);
    nanosleep(&tenth_second, NULL);
    if (++i % 20 == 0) {
      printf("%2d ", i / 20);
      fflush(stdout);
    }
  }
  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)
lea    0xe7f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1224 <main+0x84>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1c(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1224 <main+0x84>
cmpq   $0x0,-0x18(%rbp)
je     1218 <main+0x78>
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%rax
jle    1218 <main+0x78>
mov    $0x0,%al
call   1040 <kill_parent@plt>
movl   $0x0,-0x4(%rbp)
jmp    1352 <main+0x1b2>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1289 <main+0xe9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd7(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1289 <main+0xe9>
cmpq   $0x0,-0x18(%rbp)
je     1276 <main+0xd6>
mov    -0x18(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     127d <main+0xdd>
mov    $0x0,%al
call   1040 <kill_parent@plt>
movl   $0x0,-0x4(%rbp)
jmp    1352 <main+0x1b2>
movq   $0x0,-0x20(%rbp)
movl   $0x5f5e100,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xd96(%rip),%rdi
call   10c0 <unsetenv@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <call_self_nocheckpoint@plt>
lea    0xd7e(%rip),%rdi
lea    0xd92(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <call_self_nocheckpoint@plt>
lea    0xd5a(%rip),%rdi
lea    0xd5e(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <call_self_nocheckpoint@plt>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1090 <nanosleep@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x14,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    134d <main+0x1ad>
mov    -0x2c(%rbp),%eax
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
lea    0xd19(%rip),%rdi
call   1080 <printf@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    129f <main+0xff>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
lea    0xe84(%rip),%rdi
call   1030 <getenv@plt>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11fc <main+0x5c>
mov    %rax,%rbp
lea    0xe30(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12c7 <main+0x127>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11fc <main+0x5c>
lea    0xe2b(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12db <main+0x13b>
movq   $0x0,0x10(%rsp)
movl   $0x5f5e100,0x8(%rsp)
05
mov    $0x1,%r15d
lea    0xe22(%rip),%r14
lea    0x8(%rsp),%r13
jmp    1234 <main+0x94>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
mov    %r15d,%ebp
mov    $0xcccccccd,%eax
imul   %rax,%rbp
shr    $0x24,%rbp
lea    0x0(,%rbp,4),%eax
lea    (%rax,%rax,4),%r12d
mov    %r14,%rdi
call   10c0 <unsetenv@plt>
mov    (%rbx),%rdi
call   1060 <call_self_nocheckpoint@plt>
mov    %r14,%rdi
lea    0xdee(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    (%rbx),%rdi
call   1060 <call_self_nocheckpoint@plt>
mov    %r14,%rdi
lea    0xdc2(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    (%rbx),%rdi
call   1060 <call_self_nocheckpoint@plt>
mov    %r13,%rdi
xor    %esi,%esi
call   1090 <nanosleep@plt>
cmp    %r12d,%r15d
jne    1230 <main+0x90>
lea    0xda6(%rip),%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    1230 <main+0x90>
test   %rbp,%rbp
je     12fb <main+0x15b>
mov    %rbp,%rdi
call   1070 <strlen@plt>
test   %rax,%rax
jg     12f4 <main+0x154>
jmp    12fb <main+0x15b>
test   %rbp,%rbp
je     12f4 <main+0x154>
lea    0xd60(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12fb <main+0x15b>
xor    %eax,%eax
call   1040 <kill_parent@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@.str = [11 x i8] c"LD_PRELOAD\00"
@.str.1 = [25 x i8] c"--called-as-nocheckpoint\00"
@.str.2 = [46 x i8] c"--called-as-nocheckpoint-with-user-LD_PRELOAD\00"
@.str.3 = [12 x i8] c"USER_STRING\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [5 x i8] c"%2d \00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.timespec
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %6
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %30
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i8*, i8** %6
%25 = call i64 @strlen(i8* %24)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (...) @kill_parent()
br label %29
29: ; preds = %27, %23, %20
store i32 0, i32* %3
br label %82
30: ; preds = %14, %2
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %51
35: ; preds = %30
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %35
%42 = load i8*, i8** %6
%43 = icmp eq i8* %42, null
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = load i8*, i8** %6
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44, %41
%49 = call i32 (...) @kill_parent()
br label %50
50: ; preds = %48, %44
store i32 0, i32* %3
br label %82
51: ; preds = %35, %30
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
store i64 0, i64* %52
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
store i32 100000000, i32* %53
store i32 0, i32* %8
br label %54
54: ; preds = %51, %81
%55 = call i32 @unsetenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 0
%58 = load i8*, i8** %57
%59 = call i32 @call_self_nocheckpoint(i8* %58)
%60 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 0
%63 = load i8*, i8** %62
%64 = call i32 @call_self_nocheckpoint(i8* %63)
%65 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 1)
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 0
%68 = load i8*, i8** %67
%69 = call i32 @call_self_nocheckpoint(i8* %68)
%70 = call i32 @nanosleep(%struct.timespec* %7, i32* null)
%71 = load i32, i32* %8
%72 = add nsw i32 %71, 1
store i32 %72, i32* %8
%73 = srem i32 %72, 20
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %81
75: ; preds = %54
%76 = load i32, i32* %8
%77 = sdiv i32 %76, 20
%78 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %77)
%79 = load i32, i32* @stdout
%80 = call i32 @fflush(i32 %79)
br label %81
81: ; preds = %75, %54
br label %54
82: ; preds = %50, %29
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@.str = [11 x i8] c"LD_PRELOAD\00"
@.str.1 = [25 x i8] c"--called-as-nocheckpoint\00"
@.str.2 = [46 x i8] c"--called-as-nocheckpoint-with-user-LD_PRELOAD\00"
@.str.3 = [12 x i8] c"USER_STRING\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [5 x i8] c"%2d \00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %27, label %8
8: ; preds = %2
%9 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = icmp eq i8* %4, null
br i1 %12, label %54, label %13
13: ; preds = %11
%14 = tail call i64 @strlen(i8* %4)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %52, label %54
16: ; preds = %8
%17 = load i8*, i8** %5
%18 = icmp eq i8* %17, null
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = icmp eq i8* %4, null
br i1 %23, label %52, label %24
24: ; preds = %22
%25 = tail call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %54, label %52
27: ; preds = %2, %19, %16
%28 = bitcast %struct.timespec* %3 to i8*
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 100000000, i32* %30
br label %31
31: ; preds = %51, %27
%32 = phi i32 [ 0, %27 ], [ %43, %51 ]
%33 = call i32 @unsetenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%34 = load i8*, i8** %1
%35 = call i32 @call_self_nocheckpoint(i8* %34)
%36 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
%37 = load i8*, i8** %1
%38 = call i32 @call_self_nocheckpoint(i8* %37)
%39 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 1)
%40 = load i8*, i8** %1
%41 = call i32 @call_self_nocheckpoint(i8* %40)
%42 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%43 = add nuw nsw i32 %32, 1
%44 = urem i32 %43, 20
%45 = udiv i32 %43, 20
%46 = icmp eq i32 %44, 0
br i1 %46, label %47, label %51
47: ; preds = %31
%48 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %45)
%49 = load i32, i32* @stdout
%50 = call i32 @fflush(i32 %49)
br label %51
51: ; preds = %47, %31
br label %31
52: ; preds = %22, %24, %13
%53 = tail call i32 (...) @kill_parent()
br label %54
54: ; preds = %52, %24, %11, %13
ret i32 0
}