#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 */
struct string_buffer {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINTR;
char *buffer_data(struct string_buffer *);
int data_length(struct string_buffer *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ log_debug(int, char *, int, char const *, int, char const *);
int /*<<< orphan*/ log_error(char *, int);
int write(int, char const *, int);
void write_buffer(struct string_buffer *s, int fd) {
  const char *data = buffer_data(s);
  const char *end = data + data_length(s);
  int result;
  log_debug(7, "writing to pipe %d, data %p, size %d, text '%s'", fd, data,
            (int)(end - data), data);
  while (data < end) {
    result = write(fd, data, end - data);
    if (result < 0) {
      if (errno != EINTR) {
        log_error("can't write to pipe %d", fd);
        exit(1);
      }
    } else {
      data += result;
    }
  }
}


x86 assembly (clang, -O0):
<write_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <buffer_data@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <data_length@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rsi
sub    %rsi,%rax
mov    %eax,%r8d
mov    -0x18(%rbp),%r9
mov    $0x7,%edi
lea    0xe3e(%rip),%rsi
call   1060 <log_debug@plt>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1249 <write_buffer+0xe9>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1233 <write_buffer+0xd3>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
je     122e <write_buffer+0xce>
mov    -0xc(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1050 <log_error@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1244 <write_buffer+0xe4>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11c7 <write_buffer+0x67>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
call   1030 <buffer_data@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <data_length@plt>
mov    %eax,%r14d
lea    0xe78(%rip),%rsi
mov    $0x7,%edi
mov    %r15d,%edx
mov    %rbx,%rcx
mov    %eax,%r8d
mov    %rbx,%r9
call   1060 <log_debug@plt>
test   %r14d,%r14d
jle    11ff <write_buffer+0x9f>
movslq %r14d,%rbp
add    %rbx,%rbp
mov    0x2e30(%rip),%r14
mov    0x2e21(%rip),%r12
jmp    11ca <write_buffer+0x6a>
nopl   0x0(%rax)
mov    %eax,%eax
add    %rax,%rbx
cmp    %rbp,%rbx
jae    11ff <write_buffer+0x9f>
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
test   %eax,%eax
jns    11c0 <write_buffer+0x60>
mov    (%r14),%rax
cmp    (%r12),%rax
je     11c5 <write_buffer+0x65>
lea    0xe43(%rip),%rdi
mov    %r15d,%esi
call   1050 <log_error@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.string_buffer = type { i32 }
@.str = [48 x i8] c"writing to pipe %d, data %p, size %d, text '%s'\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [23 x i8] c"can't write to pipe %d\00"
define void @write_buffer(%struct.string_buffer* %0, i32 %1) {
%3 = alloca %struct.string_buffer*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store %struct.string_buffer* %0, %struct.string_buffer** %3
store i32 %1, i32* %4
%8 = load %struct.string_buffer*, %struct.string_buffer** %3
%9 = call i8* @buffer_data(%struct.string_buffer* %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = load %struct.string_buffer*, %struct.string_buffer** %3
%12 = call i32 @data_length(%struct.string_buffer* %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8* %14, i8** %6
%15 = load i32, i32* %4
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = load i8*, i8** %5
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = load i8*, i8** %5
%24 = call i32 @log_debug(i32 7, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %16, i32 %22, i8* %23)
br label %25
25: ; preds = %55, %2
%26 = load i8*, i8** %5
%27 = load i8*, i8** %6
%28 = icmp ult i8* %26, %27
br i1 %28, label %29, label %56
29: ; preds = %25
%30 = load i32, i32* %4
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = load i8*, i8** %5
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
%38 = call i32 @write(i32 %30, i8* %31, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %29
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* %4
%47 = call i32 @log_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
br label %55
50: ; preds = %29
%51 = load i32, i32* %7
%52 = load i8*, i8** %5
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %5
br label %55
55: ; preds = %50, %49
br label %25
56: ; preds = %25
ret void
}

LLVM IR (-O2):
%struct.string_buffer = type { i32 }
@.str = [48 x i8] c"writing to pipe %d, data %p, size %d, text '%s'\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [23 x i8] c"can't write to pipe %d\00"
define void @write_buffer(%struct.string_buffer* %0, i32 %1) {
%3 = tail call i8* @buffer_data(%struct.string_buffer* %0)
%4 = tail call i32 @data_length(%struct.string_buffer* %0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %3, i64 %5
%7 = ptrtoint i8* %6 to i64
%8 = tail call i32 @log_debug(i32 7, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %1, i8* %3, i32 %4, i8* %3)
%9 = icmp sgt i32 %4, 0
br i1 %9, label %10, label %30
10: ; preds = %2, %27
%11 = phi i8* [ %28, %27 ], [ %3, %2 ]
%12 = ptrtoint i8* %11 to i64
%13 = sub i64 %7, %12
%14 = trunc i64 %13 to i32
%15 = tail call i32 @write(i32 %1, i8* %11, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = tail call i32 @log_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %10
%25 = zext i32 %15 to i64
%26 = getelementptr inbounds i8, i8* %11, i64 %25
br label %27
27: ; preds = %17, %24
%28 = phi i8* [ %11, %17 ], [ %26, %24 ]
%29 = icmp ult i8* %28, %6
br i1 %29, label %10, label %30
30: ; preds = %27, %2
ret void
}