#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int check_fail(int rc, int expected_errno, const char *errno_str) {
  if (rc > 0) {
    printf("[FAIL] (unexpected success from open operation)\n");
    close(rc);
    return 1;
  }
  if (errno != expected_errno) {
    printf("[FAIL] (expected errno %d (%s) not %d (%s)\n", expected_errno,
           strerror(expected_errno), errno, strerror(errno));
    return 1;
  }
  printf("[OK]\n");
  return 0;
}


x86 assembly (clang, -O0):
<check_fail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    116e <check_fail+0x3e>
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e8 <check_fail+0xb8>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
cmp    -0xc(%rbp),%eax
je     11d3 <check_fail+0xa3>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <strerror@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    %rax,%r8
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e8 <check_fail+0xb8>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_fail>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
jle    115f <check_fail+0x1f>
mov    %edi,%ebp
lea    0xee0(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11a9 <check_fail+0x69>
mov    %esi,%ebx
mov    0x2e78(%rip),%rbp
cmp    %esi,0x0(%rbp)
jne    117d <check_fail+0x3d>
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11ae <check_fail+0x6e>
mov    %ebx,%edi
call   1060 <strerror@plt>
mov    %rax,%r14
mov    0x0(%rbp),%ebp
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
mov    %ebp,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"[FAIL] (unexpected success from open operation)\0A\00"
@errno = global i32 0
@.str.1 = [44 x i8] c"[FAIL] (expected errno %d (%s) not %d (%s)\0A\00"
@.str.2 = [6 x i8] c"[OK]\0A\00"
define i32 @check_fail(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%8 = load i32, i32* %5
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %5
%13 = call i32 @close(i32 %12)
store i32 1, i32* %4
br label %28
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = load i32, i32* %6
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i32, i32* %6
%21 = call i8* @strerror(i32 %20)
%22 = load i32, i32* @errno
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %21, i32 %22, i8* %24)
store i32 1, i32* %4
br label %28
26: ; preds = %14
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %28
28: ; preds = %26, %18, %10
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@errno = global i32 0
@.str.1 = [44 x i8] c"[FAIL] (expected errno %d (%s) not %d (%s)\0A\00"
@str = [5 x i8] c"[OK]\00"
@str.3 = [48 x i8] c"[FAIL] (unexpected success from open operation)\00"
define i32 @check_fail(i32 %0, i32 %1, i8* nocapture readnone %2) {
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.3, i64 0, i64 0))
%7 = tail call i32 @close(i32 %0)
br label %18
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = icmp eq i32 %9, %1
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = tail call i8* @strerror(i32 %1)
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* %12, i32 %13, i8* %14)
br label %18
16: ; preds = %8
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %16, %11, %5
%19 = phi i32 [ 1, %5 ], [ 1, %11 ], [ 0, %16 ]
ret i32 %19
}