#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 ENOENT;
int /*<<< orphan*/ O_WRONLY;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  int fd;
  FILE *fp;
  fd = open("./data.in", O_WRONLY);
  printf("%d\n", errno);
  perror("error:");
  fp = fopen("/xxx", "r+");
  printf("%d\n", errno);
  perror("error:");
  fclose(fp);
  perror("error:");
  errno = ENOENT;
  perror("argv[0]");
  return 0;
}


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)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
lea    0xe74(%rip),%rdi
call   1070 <perror@plt>
lea    0xe6f(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    0x2de4(%rip),%rax
mov    %ecx,(%rax)
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
mov    0x2e6c(%rip),%r12
mov    (%r12),%esi
lea    0xe8b(%rip),%r14
mov    %r14,%rdi
call   1040 <printf@plt>
lea    0xe80(%rip),%r15
mov    %r15,%rdi
call   1070 <perror@plt>
lea    0xe78(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    (%r12),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r12)
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"./data.in\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"error:\00"
@.str.3 = [5 x i8] c"/xxx\00"
@.str.4 = [3 x i8] c"r+\00"
@ENOENT = global i32 0
@.str.5 = [8 x i8] c"argv[0]\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* @errno
%11 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32, i32* @errno
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32*, i32** %7
%18 = call i32 @fclose(i32* %17)
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @ENOENT
store i32 %20, i32* @errno
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"./data.in\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"error:\00"
@.str.3 = [5 x i8] c"/xxx\00"
@.str.4 = [3 x i8] c"r+\00"
@ENOENT = global i32 0
@.str.5 = [8 x i8] c"argv[0]\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @errno
%6 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @fclose(i32* %8)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @ENOENT
store i32 %14, i32* @errno
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}