#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 */

/* Variables and functions */
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int creat(char const *, int);
int /*<<< orphan*/ perror(char const *);
scalar_t__ sg_set_binary_mode(int);
int strlen(char const *);
int write(int, unsigned char *, int);
int write_dataout(const char *filename, unsigned char *buf, int len) {
  int ret = 99;
  int fd;
  if ((filename == NULL) || ((1 == strlen(filename)) && ('-' == filename[0])))
    fd = STDOUT_FILENO;
  else {
    fd = creat(filename, 0666);
    if (fd < 0) {
      perror(filename);
      goto bail;
    }
  }
  if (sg_set_binary_mode(fd) < 0) {
    perror("sg_set_binary_mode");
    goto bail;
  }
  if (write(fd, buf, len) != len) {
    perror(filename ? filename : "stdout");
    goto bail;
  }
  ret = 0;
bail:
  if (fd >= 0 && fd != STDOUT_FILENO)
    close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<write_dataout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x63,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11b1 <write_dataout+0x51>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    11c2 <write_dataout+0x62>
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x2d,%eax
cmp    %ecx,%eax
jne    11c2 <write_dataout+0x62>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11f0 <write_dataout+0x90>
mov    -0x8(%rbp),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11eb <write_dataout+0x8b>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
jmp    1268 <write_dataout+0x108>
jmp    11f0 <write_dataout+0x90>
mov    -0x1c(%rbp),%edi
call   1030 <sg_set_binary_mode@plt>
cmp    $0x0,%rax
jge    1213 <write_dataout+0xb3>
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
jmp    1268 <write_dataout+0x108>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     1261 <write_dataout+0x101>
cmpq   $0x0,-0x8(%rbp)
je     1243 <write_dataout+0xe3>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1253 <write_dataout+0xf3>
lea    0xdc9(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1253 <write_dataout+0xf3>
mov    -0x28(%rbp),%rdi
call   1070 <perror@plt>
jmp    1268 <write_dataout+0x108>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     128c <write_dataout+0x12c>
mov    -0x1c(%rbp),%eax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%eax
je     128c <write_dataout+0x12c>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_dataout>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
test   %rdi,%rdi
je     1187 <write_dataout+0x27>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jne    1192 <write_dataout+0x32>
cmpb   $0x2d,(%rbx)
jne    1192 <write_dataout+0x32>
mov    0x2e62(%rip),%rax
mov    (%rax),%ebp
jmp    11a5 <write_dataout+0x45>
mov    %rbx,%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1212 <write_dataout+0xb2>
mov    %ebp,%edi
call   1030 <sg_set_binary_mode@plt>
test   %rax,%rax
js     11ea <write_dataout+0x8a>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
xor    %r15d,%r15d
cmp    %r14d,%eax
jne    11f3 <write_dataout+0x93>
test   %ebp,%ebp
js     11dc <write_dataout+0x7c>
mov    0x2e1f(%rip),%rax
cmp    (%rax),%ebp
je     11dc <write_dataout+0x7c>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0f(%rip),%rdi
jmp    1201 <write_dataout+0xa1>
test   %rbx,%rbx
lea    0xe16(%rip),%rdi
cmovne %rbx,%rdi
call   1070 <perror@plt>
mov    $0x63,%r15d
test   %ebp,%ebp
jns    11ca <write_dataout+0x6a>
jmp    11dc <write_dataout+0x7c>
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    $0x63,%r15d
jmp    11dc <write_dataout+0x7c>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [19 x i8] c"sg_set_binary_mode\00"
@.str.1 = [7 x i8] c"stdout\00"
define i32 @write_dataout(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 99, i32* %7
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %21, label %11
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = icmp eq i32 1, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 45, %19
br i1 %20, label %21, label %23
21: ; preds = %15, %3
%22 = load i32, i32* @STDOUT_FILENO
store i32 %22, i32* %8
br label %32
23: ; preds = %15, %11
%24 = load i8*, i8** %4
%25 = call i32 @creat(i8* %24, i32 438)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = call i32 @perror(i8* %29)
br label %55
31: ; preds = %23
br label %32
32: ; preds = %31, %21
%33 = load i32, i32* %8
%34 = call i64 @sg_set_binary_mode(i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %55
38: ; preds = %32
%39 = load i32, i32* %8
%40 = load i8*, i8** %5
%41 = load i32, i32* %6
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
%43 = load i32, i32* %6
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %54
45: ; preds = %38
%46 = load i8*, i8** %4
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = load i8*, i8** %4
br label %51
50: ; preds = %45
br label %51
51: ; preds = %50, %48
%52 = phi i8* [ %49, %48 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %50 ]
%53 = call i32 @perror(i8* %52)
br label %55
54: ; preds = %38
store i32 0, i32* %7
br label %55
55: ; preds = %54, %51, %36, %28
%56 = load i32, i32* %8
%57 = icmp sge i32 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %55
%59 = load i32, i32* %8
%60 = load i32, i32* @STDOUT_FILENO
%61 = icmp ne i32 %59, %60
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %58, %55
%66 = load i32, i32* %7
ret i32 %66
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [19 x i8] c"sg_set_binary_mode\00"
@.str.1 = [7 x i8] c"stdout\00"
define i32 @write_dataout(i8* %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 45
br i1 %10, label %11, label %13
11: ; preds = %8, %3
%12 = load i32, i32* @STDOUT_FILENO
br label %18
13: ; preds = %8, %5
%14 = tail call i32 @creat(i8* %0, i32 438)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @perror(i8* %0)
br label %38
18: ; preds = %13, %11
%19 = phi i32 [ %12, %11 ], [ %14, %13 ]
%20 = tail call i64 @sg_set_binary_mode(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %18
%23 = tail call i32 @write(i32 %19, i8* %1, i32 %2)
%24 = icmp eq i32 %23, %2
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = select i1 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
br label %27
27: ; preds = %18, %25
%28 = phi i8* [ %26, %25 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %18 ]
%29 = tail call i32 @perror(i8* %28)
br label %30
30: ; preds = %27, %22
%31 = phi i32 [ 0, %22 ], [ 99, %27 ]
%32 = icmp slt i32 %19, 0
%33 = load i32, i32* @STDOUT_FILENO
%34 = icmp eq i32 %19, %33
%35 = select i1 %32, i1 true, i1 %34
br i1 %35, label %38, label %36
36: ; preds = %30
%37 = tail call i32 @close(i32 %19)
br label %38
38: ; preds = %16, %36, %30
%39 = phi i32 [ 99, %16 ], [ %31, %36 ], [ %31, %30 ]
ret i32 %39
}