#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 ssize_t;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int show_state() {
  int fd = open("state", O_RDONLY);
  if (fd == -1) {
    if (errno == ENOENT) {
      printf("online");
      return 0;
    }
    perror("state");
    return 1;
  }
  char buffer[1024];
  ssize_t len;
  while ((len = read(fd, buffer, 1023)) > 0) {
    write(1, buffer, len);
  }
  if (len == -1) {
    perror("write");
    close(fd);
    return 1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<show_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d7 <show_state+0x77>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <show_state+0x5f>
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1257 <show_state+0xf7>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <show_state+0xf7>
jmp    11dc <show_state+0x7c>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    121b <show_state+0xbb>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11dc <show_state+0x7c>
cmpl   $0xffffffff,-0x414(%rbp)
jne    1248 <show_state+0xe8>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <show_state+0xf7>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<show_state>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d2 <show_state+0x72>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11c2 <show_state+0x62>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11a0 <show_state+0x40>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     1202 <show_state+0xa2>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11f6 <show_state+0x96>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    1215 <show_state+0xb5>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    11c9 <show_state+0x69>
lea    0xde4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    11f6 <show_state+0x96>

LLVM IR (-O0):
@.str = [6 x i8] c"state\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [7 x i8] c"online\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @show_state() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %17
9: ; preds = %0
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %37
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %37
17: ; preds = %0
br label %18
18: ; preds = %23, %17
%19 = load i32, i32* %2
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 1023)
store i32 %21, i32* %4
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* %4
%26 = call i32 @write(i32 1, i8* %24, i32 %25)
br label %18
27: ; preds = %18
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
store i32 1, i32* %1
br label %37
34: ; preds = %27
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
store i32 0, i32* %1
br label %37
37: ; preds = %34, %30, %15, %13
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [6 x i8] c"state\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [7 x i8] c"online\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @show_state() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %13
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %30
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %30
13: ; preds = %0
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%15 = call i32 @read(i32 %3, i8* %14, i32 1023)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13, %17
%18 = phi i32 [ %20, %17 ], [ %15, %13 ]
%19 = call i32 @write(i32 1, i8* %14, i32 %18)
%20 = call i32 @read(i32 %3, i8* %14, i32 1023)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %17, label %22
22: ; preds = %17, %13
%23 = phi i32 [ %15, %13 ], [ %20, %17 ]
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %22, %25
%28 = phi i32 [ 1, %25 ], [ 0, %22 ]
%29 = tail call i32 @close(i32 %3)
br label %30
30: ; preds = %27, %11, %9
%31 = phi i32 [ 0, %9 ], [ 1, %11 ], [ %28, %27 ]
ret i32 %31
}