#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_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ create_socket_path(char **);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int, int);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
FILE *open_error_file() {
  int fd;
  FILE *out;
  char *path;
  int new_size;
  char *new_path;
  create_socket_path(&path);
  new_size = strlen(path) + 10;
  new_path = malloc(new_size);
  strncpy(new_path, path, new_size);
  strncat(new_path, ".error", (new_size - strlen(path)) - 1);
  free(path);
  fd = open(new_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
  if (fd == -1) {
    free(new_path);
    return 0;
  }
  out = fdopen(fd, "a");
  if (out == NULL) {
    close(fd);
    free(new_path);
    return 0;
  }
  free(new_path);
  return out;
}


x86 assembly (clang, -O0):
<open_error_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x20(%rbp),%rdi
call   10b0 <create_socket_path@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
add    $0xa,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rdi
sub    %eax,%edx
sub    $0x1,%edx
lea    0xe0a(%rip),%rsi
call   1060 <strncat@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
or     (%rax),%esi
mov    0x2daf(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1250 <open_error_file+0xc0>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <open_error_file+0x10d>
mov    -0xc(%rbp),%edi
lea    0xdad(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128c <open_error_file+0xfc>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <open_error_file+0x10d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_error_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   10b0 <create_socket_path@plt>
mov    0x8(%rsp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0xa(%rbx),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1040 <strncpy@plt>
mov    0x8(%rsp),%rdi
call   1050 <strlen@plt>
not    %eax
lea    (%rbx,%rax,1),%edx
add    $0xa,%edx
lea    0xe1d(%rip),%rsi
mov    %r14,%rdi
call   1060 <strncat@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x2dfc(%rip),%rax
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1241 <open_error_file+0xb1>
mov    %eax,%ebp
lea    0xddc(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1243 <open_error_file+0xb3>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c".error\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [2 x i8] c"a\00"
define i32* @open_error_file() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = call i32 @create_socket_path(i8** %4)
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 10
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = call i32 @strncpy(i8* %13, i8* %14, i32 %15)
%17 = load i8*, i8** %6
%18 = load i32, i32* %5
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %18, %20
%22 = sub nsw i32 %21, 1
%23 = call i32 @strncat(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %4
%25 = call i32 @free(i8* %24)
%26 = load i8*, i8** %6
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_APPEND
%29 = or i32 %27, %28
%30 = load i32, i32* @O_WRONLY
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %26, i32 %31, i32 384)
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %0
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
store i32* null, i32** %1
br label %52
38: ; preds = %0
%39 = load i32, i32* %2
%40 = call i32* @fdopen(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %3
%41 = load i32*, i32** %3
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load i32, i32* %2
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
store i32* null, i32** %1
br label %52
48: ; preds = %38
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
%51 = load i32*, i32** %3
store i32* %51, i32** %1
br label %52
52: ; preds = %48, %43, %35
%53 = load i32*, i32** %1
ret i32* %53
}

LLVM IR (-O2):
@.str = [7 x i8] c".error\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [2 x i8] c"a\00"
define noalias i32* @open_error_file() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = call i32 @create_socket_path(i8** %1)
%4 = load i8*, i8** %1
%5 = call i32 @strlen(i8* %4)
%6 = add nsw i32 %5, 10
%7 = call i8* @malloc(i32 %6)
%8 = load i8*, i8** %1
%9 = call i32 @strncpy(i8* %7, i8* %8, i32 %6)
%10 = load i8*, i8** %1
%11 = call i32 @strlen(i8* %10)
%12 = xor i32 %11, -1
%13 = add i32 %6, %12
%14 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i8*, i8** %1
%16 = call i32 @free(i8* %15)
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_APPEND
%19 = or i32 %18, %17
%20 = load i32, i32* @O_WRONLY
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %7, i32 %21, i32 384)
%23 = icmp eq i32 %22, -1
br i1 %23, label %29, label %24
24: ; preds = %0
%25 = call i32* @fdopen(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @close(i32 %22)
br label %29
29: ; preds = %24, %0, %27
%30 = phi i32* [ null, %27 ], [ null, %0 ], [ %25, %24 ]
%31 = call i32 @free(i8* %7)
ret i32* %30
}