#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ NAME_MAX;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ *fdopen(int, char *);
int open(char *, int);
int snprintf(char *, int, char const *const, char const *const);
FILE *hdopen(const char *const format, const char *const name) {
  char buf[NAME_MAX + 1];
  FILE *fp = (FILE *)-1;
  int fd, ret;
  ret = snprintf(buf, sizeof(buf), format, name);
  if ((ret >= (int)sizeof(buf)) || (ret < 0))
    goto error;
  fd = open(buf, O_RDONLY | O_NOCTTY);
  if (fd < 0) {
    if (errno != ENOENT)
      goto error;
    fp = (FILE *)0;
    goto error;
  }
  fp = fdopen(fd, "r");
  if (fp == (FILE *)0)
    close(fd);
error:
  return fp;
}


x86 assembly (clang, -O0):
<hdopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1030 <snprintf@plt>
mov    -0x38(%rbp),%rcx
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jge    11b9 <hdopen+0x79>
cmpl   $0x0,-0x30(%rbp)
jge    11be <hdopen+0x7e>
jmp    123d <hdopen+0xfd>
mov    -0x40(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1212 <hdopen+0xd2>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
je     1205 <hdopen+0xc5>
jmp    123d <hdopen+0xfd>
movq   $0x0,-0x28(%rbp)
jmp    123d <hdopen+0xfd>
mov    -0x2c(%rbp),%edi
lea    0xde4(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1238 <hdopen+0xf8>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
jmp    123d <hdopen+0xfd>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hdopen>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rsi,%rcx
mov    %rdi,%rdx
mov    0x2e64(%rip),%rax
mov    (%rax),%r14d
lea    0x1(%r14),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1030 <snprintf@plt>
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
cmp    %r14d,%ecx
jg     11e0 <hdopen+0xa0>
test   %ecx,%ecx
js     11e0 <hdopen+0xa0>
mov    0x2e5b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c4 <hdopen+0x84>
mov    %eax,%ebx
lea    0xe53(%rip),%rsi
mov    %eax,%edi
call   1050 <fdopen@plt>
test   %rax,%rax
jne    11e0 <hdopen+0xa0>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    11e0 <hdopen+0xa0>
mov    0x2e05(%rip),%rax
mov    (%rax),%rcx
mov    0x2deb(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %rax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@NAME_MAX = global i32 0
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [2 x i8] c"r\00"
define i32* @hdopen(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i32, i32* @NAME_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
store i32* inttoptr (i64 -1 to i32*), i32** %7
%15 = trunc i64 %12 to i32
%16 = load i8*, i8** %3
%17 = load i8*, i8** %4
%18 = call i32 @snprintf(i8* %14, i32 %15, i8* %16, i8* %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = trunc i64 %12 to i32
%21 = icmp sge i32 %19, %20
br i1 %21, label %25, label %22
22: ; preds = %2
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22, %2
br label %48
26: ; preds = %22
%27 = load i32, i32* @O_RDONLY
%28 = load i32, i32* @O_NOCTTY
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %14, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %26
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %48
38: ; preds = %33
store i32* null, i32** %7
br label %48
39: ; preds = %26
%40 = load i32, i32* %8
%41 = call i32* @fdopen(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %41, i32** %7
%42 = load i32*, i32** %7
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44, %39
br label %48
48: ; preds = %47, %38, %37, %25
%49 = load i32*, i32** %7
%50 = load i8*, i8** %5
ret i32* %49
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [2 x i8] c"r\00"
define i32* @hdopen(i8* %0, i8* %1) {
%3 = load i32, i32* @NAME_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @snprintf(i8* %6, i32 %4, i8* %0, i8* %1)
%8 = icmp sgt i32 %7, %3
%9 = icmp slt i32 %7, 0
%10 = or i1 %8, %9
br i1 %10, label %27, label %11
11: ; preds = %2
%12 = load i32, i32* @O_RDONLY
%13 = load i32, i32* @O_NOCTTY
%14 = or i32 %13, %12
%15 = call i32 @open(i8* %6, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
%21 = select i1 %20, i32* null, i32* inttoptr (i64 -1 to i32*)
br label %27
22: ; preds = %11
%23 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @close(i32 %15)
br label %27
27: ; preds = %17, %22, %25, %2
%28 = phi i32* [ inttoptr (i64 -1 to i32*), %2 ], [ null, %25 ], [ %23, %22 ], [ %21, %17 ]
ret i32* %28
}