#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)
    return 0;
  out = fdopen(fd, "a");
  if (out == NULL) {
    close(fd);
    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    1247 <open_error_file+0xb7>
movq   $0x0,-0x8(%rbp)
jmp    128b <open_error_file+0xfb>
mov    -0xc(%rbp),%edi
lea    0xdb6(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    127a <open_error_file+0xea>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    128b <open_error_file+0xfb>
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     124b <open_error_file+0xbb>
mov    %eax,%ebp
lea    0xddc(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1244 <open_error_file+0xb4>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    124d <open_error_file+0xbd>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %ebx,%ebx
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 %36
35: ; preds = %0
store i32* null, i32** %1
br label %48
36: ; preds = %0
%37 = load i32, i32* %2
%38 = call i32* @fdopen(i32 %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %38, i32** %3
%39 = load i32*, i32** %3
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* %2
%43 = call i32 @close(i32 %42)
store i32* null, i32** %1
br label %48
44: ; preds = %36
%45 = load i8*, i8** %6
%46 = call i32 @free(i8* %45)
%47 = load i32*, i32** %3
store i32* %47, i32** %1
br label %48
48: ; preds = %44, %41, %35
%49 = load i32*, i32** %1
ret i32* %49
}

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 %31, 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 %31
29: ; preds = %24
%30 = call i32 @free(i8* %7)
br label %31
31: ; preds = %0, %29, %27
%32 = phi i32* [ null, %27 ], [ %25, %29 ], [ null, %0 ]
ret i32* %32
}