#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 */
scalar_t__ EPERM;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main() {
  assert(errno == 0);
  errno = EPERM;
  assert(errno == EPERM);
  FILE *f = fopen("non_existent", "r");
  if (f == NULL) {
    perror("open()");
  } else {
    fclose(f);
  }
  printf("All Ok!\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
cmpq   $0x0,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%rcx
mov    0x2e58(%rip),%rax
mov    %rcx,(%rax)
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0xe4d(%rip),%rdi
lea    0xe53(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11df <main+0x8f>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
jmp    11e8 <main+0x98>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e88(%rip),%rbx
xor    %edi,%edi
cmpq   $0x0,(%rbx)
sete   %dil
call   1050 <assert@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rbx)
mov    $0x1,%edi
call   1050 <assert@plt>
lea    0xe7b(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ae <main+0x5e>
mov    %rax,%rdi
call   1040 <fclose@plt>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
jmp    119e <main+0x4e>

LLVM IR (-O0):
@errno = global i64 0
@EPERM = global i64 0
@.str = [13 x i8] c"non_existent\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"open()\00"
@.str.3 = [9 x i8] c"All Ok!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = load i64, i64* @errno
%4 = icmp eq i64 %3, 0
%5 = zext i1 %4 to i32
%6 = call i32 @assert(i32 %5)
%7 = load i64, i64* @EPERM
store i64 %7, i64* @errno
%8 = load i64, i64* @errno
%9 = load i64, i64* @EPERM
%10 = icmp eq i64 %8, %9
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %21
18: ; preds = %0
%19 = load i32*, i32** %2
%20 = call i32 @fclose(i32* %19)
br label %21
21: ; preds = %18, %16
%22 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@errno = global i64 0
@EPERM = global i64 0
@.str = [13 x i8] c"non_existent\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"open()\00"
@str = [8 x i8] c"All Ok!\00"
define i32 @main() {
%1 = load i64, i64* @errno
%2 = icmp eq i64 %1, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = load i64, i64* @EPERM
store i64 %5, i64* @errno
%6 = tail call i32 @assert(i32 1)
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %13
11: ; preds = %0
%12 = tail call i32 @fclose(i32* %7)
br label %13
13: ; preds = %11, %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
ret i32 0
}