#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*/ uint8_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int _write(int32_t file, uint8_t *ptr, int32_t len) {
  int32_t 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    116b <_write+0x5b>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1166 <_write+0x56>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1030 <fputc@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1133 <_write+0x23>
jmp    11b9 <_write+0xa9>
cmpl   $0x2,-0x4(%rbp)
jne    11b4 <_write+0xa4>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11af <_write+0x9f>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1030 <fputc@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117c <_write+0x6c>
jmp    11b4 <_write+0xa4>
jmp    11b9 <_write+0xa9>
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     1157 <_write+0x47>
cmp    $0x2,%edi
jne    1185 <_write+0x75>
test   %r14d,%r14d
jle    1185 <_write+0x75>
mov    %r14d,%r15d
xor    %ebp,%ebp
mov    0x2ea7(%rip),%r12
nopl   0x0(%rax)
mov    (%rbx,%rbp,4),%edi
mov    (%r12),%esi
call   1030 <fputc@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1140 <_write+0x30>
jmp    1185 <_write+0x75>
test   %r14d,%r14d
jle    1185 <_write+0x75>
mov    %r14d,%r15d
xor    %ebp,%ebp
mov    0x2e70(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,4),%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, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %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 i32*, i32** %5
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
%21 = load i32, i32* @stdout
%22 = call i32 @fputc(i32 %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 i32*, i32** %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = load i32, i32* @stderr
%42 = call i32 @fputc(i32 %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, i32* 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 i32, i32* %1, i64 %13
%15 = load i32, i32* %14
%16 = load i32, i32* @stdout
%17 = tail call i32 @fputc(i32 %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 i32, i32* %1, i64 %21
%23 = load i32, i32* %22
%24 = load i32, i32* @stderr
%25 = tail call i32 @fputc(i32 %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
}