#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ get_log_fd();
scalar_t__ strlen(char const *);
scalar_t__ write(int /*<<< orphan*/, char const *, scalar_t__);
void write_string(const char *s) {
  ssize_t written, rem = strlen(s);
  while (rem) {
    written = write(get_log_fd(), s, rem);
    assert(written >= 0 && "write() failed");
    rem -= written;
    s += written;
  }
}


x86 assembly (clang, -O0):
<write_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c5 <write_string+0x85>
mov    $0x0,%al
call   1060 <get_log_fd@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x19(%rbp)
jl     1198 <write_string+0x58>
mov    $0x1,%al
mov    %al,-0x19(%rbp)
jmp    1198 <write_string+0x58>
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1159 <write_string+0x19>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_string>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %rax,%rax
je     118e <write_string+0x4e>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1060 <get_log_fd@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %rax,%r15
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1050 <assert@plt>
add    %r15,%r14
sub    %r15,%rbx
jne    1160 <write_string+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"write() failed\00"
define void @write_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
store i64 %6, i64* %4
br label %7
7: ; preds = %18, %1
%8 = load i64, i64* %4
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = call i32 (...) @get_log_fd()
%12 = load i8*, i8** %2
%13 = load i64, i64* %4
%14 = call i64 @write(i32 %11, i8* %12, i64 %13)
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = icmp sge i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %10
br label %18
18: ; preds = %17, %10
%19 = phi i1 [ false, %10 ], [ true, %17 ]
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i64, i64* %3
%23 = load i64, i64* %4
%24 = sub nsw i64 %23, %22
store i64 %24, i64* %4
%25 = load i64, i64* %3
%26 = load i8*, i8** %2
%27 = getelementptr inbounds i8, i8* %26, i64 %25
store i8* %27, i8** %2
br label %7
28: ; preds = %7
ret void
}

LLVM IR (-O2):
define void @write_string(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %16, label %4
4: ; preds = %1, %4
%5 = phi i64 [ %13, %4 ], [ %2, %1 ]
%6 = phi i8* [ %14, %4 ], [ %0, %1 ]
%7 = tail call i32 (...) @get_log_fd()
%8 = tail call i64 @write(i32 %7, i8* %6, i64 %5)
%9 = lshr i64 %8, 63
%10 = trunc i64 %9 to i32
%11 = xor i32 %10, 1
%12 = tail call i32 @assert(i32 %11)
%13 = sub nsw i64 %5, %8
%14 = getelementptr inbounds i8, i8* %6, i64 %8
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %4
16: ; preds = %4, %1
ret void
}