#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*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char **sys_errlist;
int main(void) {
  FILE *fp;
  printf("errno = %d\n", errno);
  fp = fopen("test.txt", "r");
  if (fp == NULL) {
    printf("errno = %d\n", errno);
    printf("sys_errlist[errno] = %s\n", sys_errlist[errno]);
    perror("fopen error!");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeaf(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cd <main+0x9d>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <main+0xa4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%r14
mov    (%r14),%esi
lea    0xebb(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xeb7(%rip),%rdi
lea    0xeb9(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1170 <main+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%r14),%esi
lea    0xe86(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
movslq (%r14),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe89(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1166 <main+0x36>

LLVM IR (-O0):
@.str = [12 x i8] c"errno = %d\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"test.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [25 x i8] c"sys_errlist[errno] = %s\0A\00"
@sys_errlist = global i8** null
@.str.4 = [13 x i8] c"fopen error!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = load i32, i32* @errno
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i8**, i8*** @sys_errlist
%12 = load i32, i32* @errno
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8*, i8** %11, i64 %13
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %19
18: ; preds = %0
store i32 0, i32* %1
br label %19
19: ; preds = %18, %8
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
@.str = [12 x i8] c"errno = %d\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"test.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [25 x i8] c"sys_errlist[errno] = %s\0A\00"
@sys_errlist = global i8** null
@.str.4 = [13 x i8] c"fopen error!\00"
define i32 @main() {
%1 = load i32, i32* @errno
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %15
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i8**, i8*** @sys_errlist
%9 = load i32, i32* @errno
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8*, i8** %8, i64 %10
%12 = load i8*, i8** %11
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%14 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %15
15: ; preds = %0, %5
%16 = phi i32 [ -1, %5 ], [ 0, %0 ]
ret i32 %16
}