#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*/ fputc(unsigned int, int /*<<< orphan*/);
int fputsn(unsigned int channel, char *str, unsigned int n) {
  int written = 0;
  while (*str && n--) {
    fputc(channel, *str++);
    n--;
    written++;
  }
  return written;
}


x86 assembly (clang, -O0):
<fputsn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     1152 <fputsn+0x42>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1162 <fputsn+0x52>
jmp    1193 <fputsn+0x83>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%esi
call   1030 <fputc@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <fputsn+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fputsn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rsi),%al
xor    %ebx,%ebx
test   %al,%al
je     1164 <fputsn+0x54>
mov    %edx,%r15d
test   %edx,%edx
je     1164 <fputsn+0x54>
mov    %rsi,%r14
mov    %edi,%ebp
add    $0xfffffffe,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl %al,%esi
mov    %ebp,%edi
call   1030 <fputc@plt>
movzbl 0x1(%r14,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
je     1164 <fputsn+0x54>
lea    -0x2(%r15),%ecx
test   %r15d,%r15d
mov    %ecx,%r15d
jne    1140 <fputsn+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fputsn(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %19, %3
%9 = load i8*, i8** %5
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = load i32, i32* %6
%15 = add i32 %14, -1
store i32 %15, i32* %6
%16 = icmp ne i32 %14, 0
br label %17
17: ; preds = %13, %8
%18 = phi i1 [ false, %8 ], [ %16, %13 ]
br i1 %18, label %19, label %30
19: ; preds = %17
%20 = load i32, i32* %4
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
%23 = load i8, i8* %21
%24 = sext i8 %23 to i32
%25 = call i32 @fputc(i32 %20, i32 %24)
%26 = load i32, i32* %6
%27 = add i32 %26, -1
store i32 %27, i32* %6
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %8
30: ; preds = %17
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
define i32 @fputsn(i32 %0, i8* nocapture %1, i32 %2) {
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
%6 = icmp eq i32 %2, 0
%7 = or i1 %5, %6
br i1 %7, label %22, label %8
8: ; preds = %3, %8
%9 = phi i8 [ %18, %8 ], [ %4, %3 ]
%10 = phi i32 [ %17, %8 ], [ 0, %3 ]
%11 = phi i32 [ %16, %8 ], [ %2, %3 ]
%12 = phi i8* [ %14, %8 ], [ %1, %3 ]
%13 = sext i8 %9 to i32
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = tail call i32 @fputc(i32 %0, i32 %13)
%16 = add i32 %11, -2
%17 = add nuw nsw i32 %10, 1
%18 = load i8, i8* %14
%19 = icmp eq i8 %18, 0
%20 = icmp eq i32 %16, 0
%21 = select i1 %19, i1 true, i1 %20
br i1 %21, label %22, label %8
22: ; preds = %8, %3
%23 = phi i32 [ 0, %3 ], [ %17, %8 ]
ret i32 %23
}