#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned char fwrite(char *, int, int, int /*<<< orphan*/ *);
unsigned char strlen(char *);
scalar_t__ write_byte(int /*<<< orphan*/ *, unsigned char);
int write_counted_string(FILE *file, char *string) {
  if (string) {
    unsigned char count = strlen(string);
    if (write_byte(file, count) == 0)
      return 0;
    if (fwrite(string, (int)sizeof(char), (int)count, file) != count)
      return 0;
  } else {
    if (write_byte(file, 0) == 0)
      return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<write_counted_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b0 <write_counted_string+0x80>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rdi
movzbl -0x19(%rbp),%esi
call   1050 <write_byte@plt>
cmp    $0x0,%rax
jne    117a <write_counted_string+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    11dd <write_counted_string+0xad>
mov    -0x18(%rbp),%rdi
movzbl -0x19(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
movzbl %al,%eax
movzbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
je     11ab <write_counted_string+0x7b>
movl   $0x0,-0x4(%rbp)
jmp    11dd <write_counted_string+0xad>
jmp    11d6 <write_counted_string+0xa6>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <write_byte@plt>
cmp    $0x0,%rax
jne    11d1 <write_counted_string+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    11dd <write_counted_string+0xad>
jmp    11d6 <write_counted_string+0xa6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_counted_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
test   %rsi,%rsi
je     1177 <write_counted_string+0x47>
mov    %rsi,%rbp
mov    %rsi,%rdi
call   1030 <strlen@plt>
movzbl %al,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1050 <write_byte@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     118f <write_counted_string+0x5f>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1040 <fwrite@plt>
cmp    %bl,%al
je     1189 <write_counted_string+0x59>
jmp    118f <write_counted_string+0x5f>
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <write_byte@plt>
test   %rax,%rax
je     118f <write_counted_string+0x5f>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_counted_string(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8
store i32* %0, i32** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %29
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = call zeroext i8 @strlen(i8* %10)
store i8 %11, i8* %6
%12 = load i32*, i32** %4
%13 = load i8, i8* %6
%14 = call i64 @write_byte(i32* %12, i8 zeroext %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %9
store i32 0, i32* %3
br label %36
17: ; preds = %9
%18 = load i8*, i8** %5
%19 = load i8, i8* %6
%20 = zext i8 %19 to i32
%21 = load i32*, i32** %4
%22 = call zeroext i8 @fwrite(i8* %18, i32 1, i32 %20, i32* %21)
%23 = zext i8 %22 to i32
%24 = load i8, i8* %6
%25 = zext i8 %24 to i32
%26 = icmp ne i32 %23, %25
br i1 %26, label %27, label %28
27: ; preds = %17
store i32 0, i32* %3
br label %36
28: ; preds = %17
br label %35
29: ; preds = %2
%30 = load i32*, i32** %4
%31 = call i64 @write_byte(i32* %30, i8 zeroext 0)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 0, i32* %3
br label %36
34: ; preds = %29
br label %35
35: ; preds = %34, %28
store i32 1, i32* %3
br label %36
36: ; preds = %35, %33, %27, %16
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
define i32 @write_counted_string(i32* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = tail call zeroext i8 @strlen(i8* %1)
%6 = tail call i64 @write_byte(i32* %0, i8 zeroext %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = zext i8 %5 to i32
%10 = tail call zeroext i8 @fwrite(i8* %1, i32 1, i32 %9, i32* %0)
%11 = icmp eq i8 %10, %5
br i1 %11, label %15, label %16
12: ; preds = %2
%13 = tail call i64 @write_byte(i32* %0, i8 zeroext 0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %8, %12
br label %16
16: ; preds = %8, %4, %12, %15
%17 = phi i32 [ 1, %15 ], [ 0, %12 ], [ 0, %4 ], [ 0, %8 ]
ret i32 %17
}