#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 /*<<< orphan*/ STDERR_FILENO;
int strlen(char const *);
int write(int /*<<< orphan*/, char const *, int);
void signal_write(const char *str) {
  int n = strlen(str);
  int written = 0;
  while (written < n) {
    int c = write(STDERR_FILENO, str + written, n - written);
    if (c > 0) {
      written += c;
    } else {
      return;
    }
  }
}


x86 assembly (clang, -O0):
<signal_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118f <signal_write+0x6f>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1185 <signal_write+0x65>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    118a <signal_write+0x6a>
jmp    118f <signal_write+0x6f>
jmp    113f <signal_write+0x1f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    0x2ea6(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edx
sub    %ebx,%edx
jle    115a <signal_write+0x3a>
mov    (%r15),%edi
movslq %ebx,%rsi
add    %r14,%rsi
call   1030 <write@plt>
add    %eax,%ebx
test   %eax,%eax
jg     1140 <signal_write+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
define void @signal_write(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %3
store i32 0, i32* %4
br label %8
8: ; preds = %29, %1
%9 = load i32, i32* %4
%10 = load i32, i32* %3
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %30
12: ; preds = %8
%13 = load i32, i32* @STDERR_FILENO
%14 = load i8*, i8** %2
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = sub nsw i32 %18, %19
%21 = call i32 @write(i32 %13, i8* %17, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %12
%25 = load i32, i32* %5
%26 = load i32, i32* %4
%27 = add nsw i32 %26, %25
store i32 %27, i32* %4
br label %29
28: ; preds = %12
br label %30
29: ; preds = %24
br label %8
30: ; preds = %28, %8
ret void
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
define void @signal_write(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
br label %3
3: ; preds = %6, %1
%4 = phi i32 [ 0, %1 ], [ %13, %6 ]
%5 = icmp sgt i32 %2, %4
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = load i32, i32* @STDERR_FILENO
%8 = sext i32 %4 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = sub nsw i32 %2, %4
%11 = tail call i32 @write(i32 %7, i8* %9, i32 %10)
%12 = icmp sgt i32 %11, 0
%13 = add nsw i32 %11, %4
br i1 %12, label %3, label %14
14: ; preds = %3, %6
ret void
}