#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 protocol_state {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ write_bytes(struct protocol_state *, char const *,
                       int /*<<< orphan*/);
scalar_t__ write_delim(struct protocol_state *);
int write_string(struct protocol_state *ps, const char *str) {
  assert(ps != NULL);
  assert(str != NULL);
  if (write_bytes(ps, str, strlen(str))) {
    return -1;
  }
  if (write_delim(ps)) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1060 <write_bytes@plt>
cmp    $0x0,%rax
je     11b2 <write_string+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <write_string+0x98>
mov    -0x10(%rbp),%rdi
call   1030 <write_delim@plt>
cmp    $0x0,%rax
je     11d1 <write_string+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <write_string+0x98>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_string>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <write_bytes@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1195 <write_string+0x55>
mov    %r14,%rdi
call   1030 <write_delim@plt>
neg    %rax
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.protocol_state = type { i32 }
define i32 @write_string(%struct.protocol_state* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.protocol_state*
%5 = alloca i8*
store %struct.protocol_state* %0, %struct.protocol_state** %4
store i8* %1, i8** %5
%6 = load %struct.protocol_state*, %struct.protocol_state** %4
%7 = icmp ne %struct.protocol_state* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load %struct.protocol_state*, %struct.protocol_state** %4
%15 = load i8*, i8** %5
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
%18 = call i64 @write_bytes(%struct.protocol_state* %14, i8* %15, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 -1, i32* %3
br label %27
21: ; preds = %2
%22 = load %struct.protocol_state*, %struct.protocol_state** %4
%23 = call i64 @write_delim(%struct.protocol_state* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %3
br label %27
26: ; preds = %21
store i32 0, i32* %3
br label %27
27: ; preds = %26, %25, %20
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.protocol_state = type { i32 }
define i32 @write_string(%struct.protocol_state* %0, i8* %1) {
%3 = icmp ne %struct.protocol_state* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i8* %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @strlen(i8* %1)
%10 = tail call i64 @write_bytes(%struct.protocol_state* %0, i8* %1, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = tail call i64 @write_delim(%struct.protocol_state* %0)
%14 = icmp ne i64 %13, 0
%15 = sext i1 %14 to i32
br label %16
16: ; preds = %12, %2
%17 = phi i32 [ -1, %2 ], [ %15, %12 ]
ret i32 %17
}