#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 stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ EPERM;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  struct stat buf;
  if (0 > stat("/dev/sydbox/off", &buf)) {
    fprintf(stderr, "%s: failed to set sydbox off\n", argv[0]);
    return EXIT_FAILURE;
  }
  if (0 > stat("/dev/sydbox/on", &buf)) {
    fprintf(stderr, "%s: failed to set sydbox on\n", argv[0]);
    return EXIT_FAILURE;
  }
  fd = open("arnold.layne", O_WRONLY);
  if (0 > fd)
    return (EPERM == errno) ? EXIT_SUCCESS : EXIT_FAILURE;
  else {
    if (0 > write(fd, "hello arnold layne!", 20))
      return (EPERM == errno) ? EXIT_SUCCESS : EXIT_FAILURE;
    close(fd);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11b1 <main+0x61>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe75(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d9 <main+0x189>
lea    0xe76(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11fc <main+0xac>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d9 <main+0x189>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jle    1261 <main+0x111>
mov    0x2d8a(%rip),%rax
mov    (%rax),%rax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    124a <main+0xfa>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1256 <main+0x106>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d9 <main+0x189>
mov    -0x14(%rbp),%edi
lea    0xdfc(%rip),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    12c5 <main+0x175>
mov    0x2d26(%rip),%rax
mov    (%rax),%rax
mov    0x2d44(%rip),%rcx
cmp    (%rcx),%rax
jne    12ae <main+0x15e>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    12ba <main+0x16a>
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d9 <main+0x189>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0xea1(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11c0 <main+0x70>
lea    0xeb9(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11d5 <main+0x85>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0xec6(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f6 <main+0xa6>
mov    %eax,%ebx
lea    0xec1(%rip),%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1030 <write@plt>
test   %rax,%rax
js     11f6 <main+0xa6>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    120c <main+0xbc>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe3d(%rip),%rsi
jmp    11e8 <main+0x98>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2ddc(%rip),%rax
jmp    1213 <main+0xc3>
mov    0x2db3(%rip),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <main+0x9d>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"/dev/sydbox/off\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"%s: failed to set sydbox off\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [15 x i8] c"/dev/sydbox/on\00"
@.str.3 = [29 x i8] c"%s: failed to set sydbox on\0A\00"
@.str.4 = [13 x i8] c"arnold.layne\00"
@O_WRONLY = global i32 0
@EPERM = global i64 0
@errno = global i64 0
@EXIT_SUCCESS = global i32 0
@.str.5 = [20 x i8] c"hello arnold layne!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %struct.stat* %7)
%9 = icmp sgt i64 0, %8
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %3
br label %61
17: ; preds = %2
%18 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %7)
%19 = icmp sgt i64 0, %18
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %3
br label %61
27: ; preds = %17
%28 = load i32, i32* @O_WRONLY
%29 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp sgt i32 0, %30
br i1 %31, label %32, label %42
32: ; preds = %27
%33 = load i64, i64* @EPERM
%34 = load i64, i64* @errno
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32, i32* @EXIT_SUCCESS
br label %40
38: ; preds = %32
%39 = load i32, i32* @EXIT_FAILURE
br label %40
40: ; preds = %38, %36
%41 = phi i32 [ %37, %36 ], [ %39, %38 ]
store i32 %41, i32* %3
br label %61
42: ; preds = %27
%43 = load i32, i32* %6
%44 = call i64 @write(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 20)
%45 = icmp sgt i64 0, %44
br i1 %45, label %46, label %56
46: ; preds = %42
%47 = load i64, i64* @EPERM
%48 = load i64, i64* @errno
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i32, i32* @EXIT_SUCCESS
br label %54
52: ; preds = %46
%53 = load i32, i32* @EXIT_FAILURE
br label %54
54: ; preds = %52, %50
%55 = phi i32 [ %51, %50 ], [ %53, %52 ]
store i32 %55, i32* %3
br label %61
56: ; preds = %42
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56
%60 = load i32, i32* @EXIT_SUCCESS
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %54, %40, %20, %10
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [16 x i8] c"/dev/sydbox/off\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"%s: failed to set sydbox off\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [15 x i8] c"/dev/sydbox/on\00"
@.str.3 = [29 x i8] c"%s: failed to set sydbox on\0A\00"
@.str.4 = [13 x i8] c"arnold.layne\00"
@O_WRONLY = global i32 0
@EPERM = global i64 0
@errno = global i64 0
@EXIT_SUCCESS = global i32 0
@.str.5 = [20 x i8] c"hello arnold layne!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
br label %44
12: ; preds = %2
%13 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %1
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
br label %44
20: ; preds = %12
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = load i64, i64* @EPERM
%26 = load i64, i64* @errno
%27 = icmp eq i64 %25, %26
%28 = load i32, i32* @EXIT_SUCCESS
%29 = load i32, i32* @EXIT_FAILURE
%30 = select i1 %27, i32 %28, i32 %29
br label %44
31: ; preds = %20
%32 = call i64 @write(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 20)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i64, i64* @EPERM
%36 = load i64, i64* @errno
%37 = icmp eq i64 %35, %36
%38 = load i32, i32* @EXIT_SUCCESS
%39 = load i32, i32* @EXIT_FAILURE
%40 = select i1 %37, i32 %38, i32 %39
br label %44
41: ; preds = %31
%42 = call i32 @close(i32 %22)
%43 = load i32, i32* @EXIT_SUCCESS
br label %44
44: ; preds = %41, %34, %24, %15, %7
%45 = phi i32 [ %11, %7 ], [ %19, %15 ], [ %30, %24 ], [ %40, %34 ], [ %43, %41 ]
ret i32 %45
}