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

/* Variables and functions */
int /*<<< orphan*/ abort();
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void mfd_fail_open(int fd, int flags, mode_t mode) {
  char buf[512];
  int r;
  sprintf(buf, "/proc/self/fd/%d", fd);
  r = open(buf, flags, mode);
  if (r >= 0) {
    printf("open(%s) didn't fail as expected\n");
    abort();
  }
}


x86 assembly (clang, -O0):
<mfd_fail_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe9b(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jl     11a2 <mfd_fail_open+0x62>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_fail_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%edx
lea    0xea8(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1060 <sprintf@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1050 <open@plt>
test   %eax,%eax
jns    117f <mfd_fail_open+0x3f>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe8b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [34 x i8] c"open(%s) didn't fail as expected\0A\00"
define void @mfd_fail_open(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = call i32 @open(i8* %12, i32 %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 (...) @abort()
unreachable
21: ; preds = %3
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [34 x i8] c"open(%s) didn't fail as expected\0A\00"
define void @mfd_fail_open(i32 %0, i32 %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32 @open(i8* %5, i32 %1, i32 %2)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %3
ret void
}