#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*/ assert(int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
int write_string(int fd, char *arr, int n) {
  int status, written;
  assert(fd > 1 && "Invalid file descriptor");
  if (n < 0) {
    n = strlen(arr);
  }
  written = 0;
  while (written < n) {
    status = write(fd, &arr[written], n - written);
    assert(status >= 0 && "Write failed");
    written += status;
  }
  do {
    status = write(fd, "\n", 1);
    assert(status >= 0 && "Write failed");
  } while (status == 0);
  return 0;
}


x86 assembly (clang, -O0):
<write_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x4(%rbp)
mov    %al,-0x1d(%rbp)
jle    115b <write_string+0x2b>
mov    $0x1,%al
mov    %al,-0x1d(%rbp)
jmp    115b <write_string+0x2b>
mov    -0x1d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0x14(%rbp)
jge    117e <write_string+0x4e>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e1 <write_string+0xb1>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x1e(%rbp)
jl     11c6 <write_string+0x96>
mov    $0x1,%al
mov    %al,-0x1e(%rbp)
jmp    11c6 <write_string+0x96>
mov    -0x1e(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1185 <write_string+0x55>
jmp    11e6 <write_string+0xb6>
mov    -0x4(%rbp),%edi
lea    0xe35(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x1f(%rbp)
jl     1216 <write_string+0xe6>
mov    $0x1,%al
mov    %al,-0x1f(%rbp)
jmp    1216 <write_string+0xe6>
mov    -0x1f(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0x18(%rbp)
je     11e6 <write_string+0xb6>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
xor    %edi,%edi
cmp    $0x2,%r15d
setge  %dil
call   1050 <assert@plt>
test   %r12d,%r12d
js     115c <write_string+0x2c>
test   %r12d,%r12d
jg     116c <write_string+0x3c>
jmp    1199 <write_string+0x69>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
test   %r12d,%r12d
jle    1199 <write_string+0x69>
xor    %ebx,%ebx
xchg   %ax,%ax
movslq %ebx,%rbp
lea    (%r14,%rbp,1),%rsi
mov    %r12d,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1030 <write@plt>
mov    %eax,%ebx
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
add    %ebp,%ebx
cmp    %ebx,%r12d
jg     1170 <write_string+0x40>
lea    0xe60(%rip),%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
test   %ebx,%ebx
je     11a0 <write_string+0x70>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Invalid file descriptor\00"
@.str.1 = [13 x i8] c"Write failed\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @write_string(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %3
br label %12
12: ; preds = %11, %3
%13 = phi i1 [ false, %3 ], [ true, %11 ]
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %18, %12
store i32 0, i32* %8
br label %22
22: ; preds = %39, %21
%23 = load i32, i32* %8
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %46
26: ; preds = %22
%27 = load i32, i32* %4
%28 = load i8*, i8** %5
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i32, i32* %6
%33 = load i32, i32* %8
%34 = sub nsw i32 %32, %33
%35 = call i32 @write(i32 %27, i8* %31, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %26
br label %39
39: ; preds = %38, %26
%40 = phi i1 [ false, %26 ], [ true, %38 ]
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %22
46: ; preds = %22
br label %47
47: ; preds = %57, %46
%48 = load i32, i32* %4
%49 = call i32 @write(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp sge i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
br label %53
53: ; preds = %52, %47
%54 = phi i1 [ false, %47 ], [ true, %52 ]
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
br label %57
57: ; preds = %53
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 0
br i1 %59, label %47, label %60
60: ; preds = %57
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [2 x i8] c"\0A\00"
define i32 @write_string(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %0, 1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp slt i32 %2, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @strlen(i8* %1)
br label %10
10: ; preds = %8, %3
%11 = phi i32 [ %9, %8 ], [ %2, %3 ]
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %10, %13
%14 = phi i32 [ %22, %13 ], [ 0, %10 ]
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = sub nsw i32 %11, %14
%18 = tail call i32 @write(i32 %0, i8* %16, i32 %17)
%19 = xor i32 %18, -1
%20 = lshr i32 %19, 31
%21 = tail call i32 @assert(i32 %20)
%22 = add nsw i32 %18, %14
%23 = icmp sgt i32 %11, %22
br i1 %23, label %13, label %24
24: ; preds = %13, %10
br label %25
25: ; preds = %24, %25
%26 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%27 = xor i32 %26, -1
%28 = lshr i32 %27, 31
%29 = tail call i32 @assert(i32 %28)
%30 = icmp eq i32 %26, 0
br i1 %30, label %25, label %31
31: ; preds = %25
ret i32 0
}