#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 /*<<< orphan*/ *fdopen(int, char *);
int fwrite_log(int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ perror(char *);
int strlen(char const *);
int write(int, char const *, int);
int dwrite_log(int fd, const char *msg) {
  FILE *file;
  int length;
  length = strlen(msg);
  if (write(fd, msg, length) < length) {
    perror("dwrite_log|write()");
    return 1;
  }
  file = fdopen(fd, "a");
  if (file == NULL) {
    return 1;
  }
  return fwrite_log(file, msg);
}


x86 assembly (clang, -O0):
<dwrite_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
jge    119b <dwrite_log+0x4b>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d5 <dwrite_log+0x85>
mov    -0x8(%rbp),%edi
lea    0xe6e(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <dwrite_log+0x75>
movl   $0x1,-0x4(%rbp)
jmp    11d5 <dwrite_log+0x85>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <fwrite_log@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dwrite_log>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jl     1195 <dwrite_log+0x45>
lea    0xe99(%rip),%rsi
mov    %ebp,%edi
call   1050 <fdopen@plt>
test   %rax,%rax
je     11a1 <dwrite_log+0x51>
mov    %rax,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <fwrite_log@plt>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"dwrite_log|write()\00"
@.str.1 = [2 x i8] c"a\00"
define i32 @dwrite_log(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i32, i32* %7
%13 = call i32 @write(i32 %10, i8* %11, i32 %12)
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %28
18: ; preds = %2
%19 = load i32, i32* %4
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 1, i32* %3
br label %28
24: ; preds = %18
%25 = load i32*, i32** %6
%26 = load i8*, i8** %5
%27 = call i32 @fwrite_log(i32* %25, i8* %26)
store i32 %27, i32* %3
br label %28
28: ; preds = %24, %23, %16
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [19 x i8] c"dwrite_log|write()\00"
@.str.1 = [2 x i8] c"a\00"
define i32 @dwrite_log(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i32 @write(i32 %0, i8* %1, i32 %3)
%5 = icmp slt i32 %4, %3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %13
8: ; preds = %2
%9 = tail call i32* @fdopen(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @fwrite_log(i32* %9, i8* %1)
br label %13
13: ; preds = %8, %11, %6
%14 = phi i32 [ 1, %6 ], [ %12, %11 ], [ 1, %8 ]
ret i32 %14
}