#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 */
struct afile {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ O_RDONLY;
struct afile *af_fdopen(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char const *, int /*<<< orphan*/);
struct afile *af_open(const char *file) {
  struct afile *af;
  int fd;
  if (file == NULL)
    do {
      errno = (EINVAL);
      return (NULL);
    } while (0);
  if ((fd = open(file, O_RDONLY)) < 0)
    return (NULL);
  if ((af = af_fdopen(fd)) == NULL)
    (void)close(fd);
  return (af);
}


x86 assembly (clang, -O0):
<af_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116b <af_open+0x3b>
jmp    114c <af_open+0x1c>
mov    0x2e7d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7c(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11bc <af_open+0x8c>
mov    -0x10(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1196 <af_open+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11bc <af_open+0x8c>
mov    -0x1c(%rbp),%edi
call   1040 <af_fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b4 <af_open+0x84>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<af_open>:
push   %rbx
test   %rdi,%rdi
je     1158 <af_open+0x28>
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1173 <af_open+0x43>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <af_fdopen@plt>
test   %rax,%rax
je     116c <af_open+0x3c>
pop    %rbx
ret
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    0x2e70(%rip),%rcx
mov    %eax,(%rcx)
jmp    1173 <af_open+0x43>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.afile = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@O_RDONLY = global i32 0
define %struct.afile* @af_open(i8* %0) {
%2 = alloca %struct.afile*
%3 = alloca i8*
%4 = alloca %struct.afile*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
br label %9
9: ; preds = %8
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
store %struct.afile* null, %struct.afile** %2
br label %27
11: ; No predecessors!
br label %12
12: ; preds = %11, %1
%13 = load i8*, i8** %3
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store %struct.afile* null, %struct.afile** %2
br label %27
18: ; preds = %12
%19 = load i32, i32* %5
%20 = call %struct.afile* @af_fdopen(i32 %19)
store %struct.afile* %20, %struct.afile** %4
%21 = icmp eq %struct.afile* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %5
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22, %18
%26 = load %struct.afile*, %struct.afile** %4
store %struct.afile* %26, %struct.afile** %2
br label %27
27: ; preds = %25, %17, %9
%28 = load %struct.afile*, %struct.afile** %2
ret %struct.afile* %28
}

LLVM IR (-O2):
%struct.afile = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@O_RDONLY = global i32 0
define %struct.afile* @af_open(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i32, i32* @EINVAL
store i32 %4, i32* @errno
br label %14
5: ; preds = %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call %struct.afile* @af_fdopen(i32 %7)
%11 = icmp eq %struct.afile* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @close(i32 %7)
br label %14
14: ; preds = %9, %12, %5, %3
%15 = phi %struct.afile* [ null, %3 ], [ null, %5 ], [ null, %12 ], [ %10, %9 ]
ret %struct.afile* %15
}