#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
scalar_t__ ioperm(int, int, int);
int /*<<< orphan*/ outb(int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ setuid(int /*<<< orphan*/);
int main(void) {
  if (setuid(0) < 0) {
    printf("Program musi byt spusten rootem\n");
    return (0);
  }
  if (ioperm(0x378, 3, 1)) {
    printf("Neni pristup na datovy port\n");
    return (0);
  }
  char str[999];
  FILE *file;
  int nezajem;
  file = fopen("state.rele", "r");
  if (file) {
    nezajem = fscanf(file, "%s", str);
    fclose(file);
  }
  int stavRele;
  stavRele = (*str == '1' ? 1 : 0);
  if (stavRele == 1)
    outb(1, 0x378);
  else
    outb(0, 0x378);
  ioperm(0x378, 3, 0);
  printf("\n");
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
jge    11ab <main+0x3b>
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    $0x378,%edi
mov    $0x3,%esi
mov    $0x1,%edx
call   1070 <ioperm@plt>
cmp    $0x0,%rax
je     11e1 <main+0x71>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a0 <main+0x130>
lea    0xe56(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x3f8(%rbp)
cmpq   $0x0,-0x3f8(%rbp)
je     1235 <main+0xc5>
mov    -0x3f8(%rbp),%rdi
lea    -0x3f0(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1060 <fscanf@plt>
mov    %eax,-0x3fc(%rbp)
mov    -0x3f8(%rbp),%rdi
call   1030 <fclose@plt>
movsbl -0x3f0(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x31,%edx
cmove  %ecx,%eax
mov    %eax,-0x400(%rbp)
cmpl   $0x1,-0x400(%rbp)
jne    1270 <main+0x100>
mov    $0x1,%edi
mov    $0x378,%esi
call   1050 <outb@plt>
jmp    127c <main+0x10c>
xor    %edi,%edi
mov    $0x378,%esi
call   1050 <outb@plt>
mov    $0x378,%edi
mov    $0x3,%esi
xor    %edx,%edx
call   1070 <ioperm@plt>
lea    0xd8b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
xor    %edi,%edi
call   10a0 <setuid@plt>
test   %rax,%rax
js     11c2 <main+0x42>
mov    $0x1,%ebx
mov    $0x378,%edi
mov    $0x3,%esi
mov    $0x1,%edx
call   1080 <ioperm@plt>
test   %rax,%rax
je     11d0 <main+0x50>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
jmp    1235 <main+0xb5>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
jmp    1235 <main+0xb5>
lea    0xe29(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120e <main+0x8e>
mov    %rax,%r14
lea    0xe1b(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1070 <fscanf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
cmpb   $0x31,(%rsp)
sete   %bl
mov    %ebx,%edi
mov    $0x378,%esi
call   1060 <outb@plt>
mov    $0x378,%edi
mov    $0x3,%esi
xor    %edx,%edx
call   1080 <ioperm@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Program musi byt spusten rootem\0A\00"
@.str.1 = [29 x i8] c"Neni pristup na datovy port\0A\00"
@.str.2 = [11 x i8] c"state.rele\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [999 x i8]
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i64 @setuid(i32 0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %41
10: ; preds = %0
%11 = call i64 @ioperm(i32 888, i32 3, i32 1)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %41
15: ; preds = %10
%16 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %16, i32** %3
%17 = load i32*, i32** %3
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i32*, i32** %3
%21 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 0
%22 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %21)
store i32 %22, i32* %4
%23 = load i32*, i32** %3
%24 = call i32 @fclose(i32* %23)
br label %25
25: ; preds = %19, %15
%26 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 49
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 1, i32 0
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @outb(i32 1, i32 888)
br label %38
36: ; preds = %25
%37 = call i32 @outb(i32 0, i32 888)
br label %38
38: ; preds = %36, %34
%39 = call i64 @ioperm(i32 888, i32 3, i32 0)
%40 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %41
41: ; preds = %38, %13, %8
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str.2 = [11 x i8] c"state.rele\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%s\00"
@str = [28 x i8] c"Neni pristup na datovy port\00"
@str.6 = [32 x i8] c"Program musi byt spusten rootem\00"
define i32 @main() {
%1 = alloca [999 x i8]
%2 = tail call i64 @setuid(i32 0)
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
br label %26
6: ; preds = %0
%7 = tail call i64 @ioperm(i32 888, i32 3, i32 1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %26
11: ; preds = %6
%12 = getelementptr inbounds [999 x i8], [999 x i8]* %1, i64 0, i64 0
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %21, label %15
15: ; preds = %11
%16 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%17 = call i32 @fclose(i32* %13)
%18 = load i8, i8* %12
%19 = icmp eq i8 %18, 49
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %15, %11
%22 = phi i32 [ 1, %11 ], [ %20, %15 ]
%23 = call i32 @outb(i32 %22, i32 888)
%24 = call i64 @ioperm(i32 888, i32 3, i32 0)
%25 = call i32 @putchar(i32 10)
br label %26
26: ; preds = %21, %9, %4
ret i32 0
}