#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 O_APPEND;
int O_NOFOLLOW;
int O_NONBLOCK;
int O_WRONLY;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
FILE *open_logfile(const char *filename) {
  int logfd;
  FILE *log;
  logfd = open(filename, O_WRONLY | O_APPEND | O_NOFOLLOW | O_NONBLOCK);
  if (-1 == logfd) {
    fprintf(stderr, "logfile: %s. ", filename);
    perror("opening logfile");
    return NULL;
  }
  log = fdopen(logfd, "a");
  if (NULL == log) {
    perror("fdopen logfile");
    return NULL;
  }
  return log;
}


x86 assembly (clang, -O0):
<open_logfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
mov    0x2e6e(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11bc <open_logfile+0x7c>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe62(%rip),%rsi
call   1030 <fprintf@plt>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fc <open_logfile+0xbc>
mov    -0x14(%rbp),%edi
lea    0xe58(%rip),%rsi
call   1040 <fdopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11f4 <open_logfile+0xb4>
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fc <open_logfile+0xbc>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_logfile>:
push   %rbx
mov    0x2e78(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e9e(%rip),%rax
or     (%rax),%esi
mov    %rdi,%rbx
mov    0x2e7a(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1187 <open_logfile+0x47>
lea    0xea5(%rip),%rsi
mov    %eax,%edi
call   1040 <fdopen@plt>
test   %rax,%rax
je     11a8 <open_logfile+0x68>
pop    %rbx
ret
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
lea    0xe68(%rip),%rdi
jmp    11af <open_logfile+0x6f>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_NOFOLLOW = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"logfile: %s. \00"
@.str.1 = [16 x i8] c"opening logfile\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [15 x i8] c"fdopen logfile\00"
define i32* @open_logfile(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = load i32, i32* @O_NOFOLLOW
%11 = or i32 %9, %10
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %6, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %22
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %3
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %2
br label %31
22: ; preds = %1
%23 = load i32, i32* %4
%24 = call i32* @fdopen(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %5
%25 = load i32*, i32** %5
%26 = icmp eq i32* null, %25
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32* null, i32** %2
br label %31
29: ; preds = %22
%30 = load i32*, i32** %5
store i32* %30, i32** %2
br label %31
31: ; preds = %29, %27, %17
%32 = load i32*, i32** %2
ret i32* %32
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_NOFOLLOW = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"logfile: %s. \00"
@.str.1 = [16 x i8] c"opening logfile\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [15 x i8] c"fdopen logfile\00"
define noalias i32* @open_logfile(i8* %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_APPEND
%4 = or i32 %3, %2
%5 = load i32, i32* @O_NOFOLLOW
%6 = or i32 %4, %5
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
14: ; preds = %1
%15 = tail call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14, %11
%18 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), %14 ]
%19 = tail call i32 @perror(i8* %18)
br label %20
20: ; preds = %17, %14
%21 = phi i32* [ %15, %14 ], [ null, %17 ]
ret i32* %21
}