#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(char, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int _write(int file, char *ptr, int len) {
  int i;
  if (file == 1) {
    for (i = 0; i < len; i++)
      fputc(ptr[i], stdout);
  } else if (file == 2) {
    for (i = 0; i < len; i++)
      fputc(ptr[i], stderr);
  }
  return len;
}


x86 assembly (clang, -O0):
<_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    116e <_write+0x5e>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1169 <_write+0x59>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1030 <fputc@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1133 <_write+0x23>
jmp    11bf <_write+0xaf>
cmpl   $0x2,-0x4(%rbp)
jne    11ba <_write+0xaa>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b5 <_write+0xa5>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1030 <fputc@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117f <_write+0x6f>
jmp    11ba <_write+0xaa>
jmp    11bf <_write+0xaf>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
cmp    $0x1,%edi
je     1158 <_write+0x48>
cmp    $0x2,%edi
jne    1186 <_write+0x76>
test   %r14d,%r14d
jle    1186 <_write+0x76>
mov    %r14d,%r15d
xor    %ebp,%ebp
mov    0x2ea7(%rip),%r12
nopl   0x0(%rax)
movsbl (%rbx,%rbp,1),%edi
mov    (%r12),%esi
call   1030 <fputc@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1140 <_write+0x30>
jmp    1186 <_write+0x76>
test   %r14d,%r14d
jle    1186 <_write+0x76>
mov    %r14d,%r15d
xor    %ebp,%ebp
mov    0x2e6f(%rip),%r12
nopl   0x0(%rax)
movsbl (%rbx,%rbp,1),%edi
mov    (%r12),%esi
call   1030 <fputc@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1170 <_write+0x60>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@stderr = global i32 0
define i32 @_write(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
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %27
10: ; preds = %3
store i32 0, i32* %7
br label %11
11: ; preds = %23, %10
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = load i32, i32* @stdout
%22 = call i32 @fputc(i8 signext %20, i32 %21)
br label %23
23: ; preds = %15
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
br label %11
26: ; preds = %11
br label %48
27: ; preds = %3
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 2
br i1 %29, label %30, label %47
30: ; preds = %27
store i32 0, i32* %7
br label %31
31: ; preds = %43, %30
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %46
35: ; preds = %31
%36 = load i8*, i8** %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = load i32, i32* @stderr
%42 = call i32 @fputc(i8 signext %40, i32 %41)
br label %43
43: ; preds = %35
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %31
46: ; preds = %31
br label %47
47: ; preds = %46, %27
br label %48
48: ; preds = %47, %26
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
@stdout = global i32 0
@stderr = global i32 0
define i32 @_write(i32 %0, i8* nocapture %1, i32 returned %2) {
switch i32 %0, label %28 [
i32 1, label %8
i32 2, label %4
]
4: ; preds = %3
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %28
6: ; preds = %4
%7 = zext i32 %2 to i64
br label %20
8: ; preds = %3
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %28
10: ; preds = %8
%11 = zext i32 %2 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %18, %12 ]
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = load i8, i8* %14
%16 = load i32, i32* @stdout
%17 = tail call i32 @fputc(i8 signext %15, i32 %16)
%18 = add nuw nsw i64 %13, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %28, label %12
20: ; preds = %6, %20
%21 = phi i64 [ 0, %6 ], [ %26, %20 ]
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = load i8, i8* %22
%24 = load i32, i32* @stderr
%25 = tail call i32 @fputc(i8 signext %23, i32 %24)
%26 = add nuw nsw i64 %21, 1
%27 = icmp eq i64 %26, %7
br i1 %27, label %28, label %20
28: ; preds = %20, %12, %4, %8, %3
ret i32 %2
}