#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int sample_fopen_function() {
  FILE *p = NULL;
  FILE *q = NULL;
  p = fopen("/dev/null", "w");
  if (NULL == p) {
    perror("fopen()");
    return 1;
  }
  q = fopen("/dev/null", "w");
  if (NULL == q) {
    perror("fopen()");
    fclose(p);
    return 2;
  }
  fclose(p);
  fclose(q);
  return 0;
}


x86 assembly (clang, -O0):
<sample_fopen_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xeb1(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1183 <sample_fopen_function+0x53>
lea    0xe9a(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <sample_fopen_function+0xb0>
lea    0xe76(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11c7 <sample_fopen_function+0x97>
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x2,-0x4(%rbp)
jmp    11e0 <sample_fopen_function+0xb0>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sample_fopen_function>:
push   %r14
push   %rbx
push   %rax
lea    0xec5(%rip),%rdi
lea    0xec8(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1184 <sample_fopen_function+0x54>
mov    %rax,%r14
lea    0xeaa(%rip),%rdi
lea    0xead(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1197 <sample_fopen_function+0x67>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    117c <sample_fopen_function+0x4c>
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0x2,%eax
jmp    117c <sample_fopen_function+0x4c>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"fopen()\00"
define i32 @sample_fopen_function() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
store i32* null, i32** %2
store i32* null, i32** %3
%4 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* null, %5
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %22
9: ; preds = %0
%10 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp eq i32* null, %11
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32*, i32** %2
%16 = call i32 @fclose(i32* %15)
store i32 2, i32* %1
br label %22
17: ; preds = %9
%18 = load i32*, i32** %2
%19 = call i32 @fclose(i32* %18)
%20 = load i32*, i32** %3
%21 = call i32 @fclose(i32* %20)
store i32 0, i32* %1
br label %22
22: ; preds = %17, %13, %7
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"fopen()\00"
define i32 @sample_fopen_function() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %14
5: ; preds = %0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @fclose(i32* %1)
br label %14
11: ; preds = %5
%12 = tail call i32 @fclose(i32* %1)
%13 = tail call i32 @fclose(i32* %6)
br label %14
14: ; preds = %11, %8, %3
%15 = phi i32 [ 1, %3 ], [ 2, %8 ], [ 0, %11 ]
ret i32 %15
}