#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*/ SYS_getpid;
int X86_EFLAGS_NT;
int get_eflags();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ set_eflags(int);
int /*<<< orphan*/ syscall(int /*<<< orphan*/);
int main() {
  printf("[RUN]\tSet NT and issue a syscall\n");
  set_eflags(get_eflags() | X86_EFLAGS_NT);
  syscall(SYS_getpid);
  if (get_eflags() & X86_EFLAGS_NT) {
    printf("[OK]\tThe syscall worked and NT is still set\n");
    return 0;
  } else {
    printf("[FAIL]\tThe syscall worked but NT was cleared\n");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <get_eflags@plt>
mov    %eax,%edi
mov    0x2e75(%rip),%rax
or     (%rax),%edi
call   1030 <set_eflags@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
mov    $0x0,%al
call   1040 <get_eflags@plt>
mov    0x2e52(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b1 <main+0x71>
lea    0xe82(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c4 <main+0x84>
lea    0xe97(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <get_eflags@plt>
mov    0x2e82(%rip),%r14
or     (%r14),%eax
mov    %eax,%edi
call   1030 <set_eflags@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
xor    %eax,%eax
call   1050 <get_eflags@plt>
xor    %ebx,%ebx
and    (%r14),%eax
sete   %bl
lea    0xe95(%rip),%rax
lea    0xebb(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [34 x i8] c"[RUN]\09Set NT and issue a syscall\0A\00"
@X86_EFLAGS_NT = global i32 0
@SYS_getpid = global i32 0
@.str.1 = [45 x i8] c"[OK]\09The syscall worked and NT is still set\0A\00"
@.str.2 = [46 x i8] c"[FAIL]\09The syscall worked but NT was cleared\0A\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%3 = call i32 (...) @get_eflags()
%4 = load i32, i32* @X86_EFLAGS_NT
%5 = or i32 %3, %4
%6 = call i32 @set_eflags(i32 %5)
%7 = load i32, i32* @SYS_getpid
%8 = call i32 @syscall(i32 %7)
%9 = call i32 (...) @get_eflags()
%10 = load i32, i32* @X86_EFLAGS_NT
%11 = and i32 %9, %10
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %17
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %17
17: ; preds = %15, %13
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
@X86_EFLAGS_NT = global i32 0
@SYS_getpid = global i32 0
@str = [33 x i8] c"[RUN]\09Set NT and issue a syscall\00"
@str.3 = [45 x i8] c"[FAIL]\09The syscall worked but NT was cleared\00"
@str.4 = [44 x i8] c"[OK]\09The syscall worked and NT is still set\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @get_eflags()
%3 = load i32, i32* @X86_EFLAGS_NT
%4 = or i32 %3, %2
%5 = tail call i32 @set_eflags(i32 %4)
%6 = load i32, i32* @SYS_getpid
%7 = tail call i32 @syscall(i32 %6)
%8 = tail call i32 (...) @get_eflags()
%9 = load i32, i32* @X86_EFLAGS_NT
%10 = and i32 %9, %8
%11 = icmp eq i32 %10, 0
%12 = select i1 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @str.3, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @str.4, i64 0, i64 0)
%13 = zext i1 %11 to i32
%14 = tail call i32 @puts(i8* dereferenceable(1) %12)
ret i32 %13
}