#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main() {
  FILE *pf;
  int errnum;
  pf = fopen("unexist.txt", "rb");
  if (pf == NULL) {
    errnum = errno;
    fprintf(stderr, "Value of errno: %d\n", errno);
    perror("Error printed by perror");
    fprintf(stderr, "Error opening file: %s\n", strerror(errnum));
  } else {
    fclose(pf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e6 <main+0x96>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11ef <main+0x9f>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117c <main+0x2c>
mov    %rax,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e55(%rip),%rax
mov    (%rax),%ebx
mov    0x2e54(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe79(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
mov    0x0(%rbp),%ebp
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe7f(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1173 <main+0x23>

LLVM IR (-O0):
@.str = [12 x i8] c"unexist.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"Value of errno: %d\0A\00"
@.str.3 = [24 x i8] c"Error printed by perror\00"
@.str.4 = [24 x i8] c"Error opening file: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %17
7: ; preds = %0
%8 = load i32, i32* @errno
store i32 %8, i32* %3
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = load i32, i32* %3
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %15)
br label %20
17: ; preds = %0
%18 = load i32*, i32** %2
%19 = call i32 @fclose(i32* %18)
br label %20
20: ; preds = %17, %7
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"unexist.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"Value of errno: %d\0A\00"
@.str.3 = [24 x i8] c"Error printed by perror\00"
@.str.4 = [24 x i8] c"Error opening file: %s\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %11
3: ; preds = %0
%4 = load i32, i32* @errno
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%7 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%8 = load i32, i32* @stderr
%9 = tail call i8* @strerror(i32 %4)
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %9)
br label %13
11: ; preds = %0
%12 = tail call i32 @fclose(i32* %1)
br label %13
13: ; preds = %11, %3
ret i32 0
}