#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 write(int, unsigned char *, int);
void write_buf(int fd) {
  unsigned char terminal[] = "init \n";
  int num_written = 0;
  int space = 0;
  do {
    num_written = write(fd, &terminal[space], 1);
    space += num_written;
  } while ((terminal[space - 1] != '\r') && (num_written > 0));
}


x86 assembly (clang, -O0):
<write_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0xedf(%rip),%eax
mov    %eax,-0xb(%rbp)
mov    0xed9(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xed1(%rip),%al
mov    %al,-0x5(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
movslq -0x14(%rbp),%rax
lea    -0xb(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
movzbl -0xb(%rbp,%rax,1),%ecx
xor    %eax,%eax
cmp    $0xd,%ecx
mov    %al,-0x15(%rbp)
je     118f <write_buf+0x7f>
cmpl   $0x0,-0x10(%rbp)
setg   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1146 <write_buf+0x36>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0xa2074,0xb(%rsp)
movl   $0x74696e69,0x8(%rsp)
74
xor    %ebp,%ebp
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x8,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    1168 <write_buf+0x58>
add    %eax,%ebp
movslq %ebp,%rax
cmpb   $0xd,-0x1(%rax,%r14,1)
jne    1140 <write_buf+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.write_buf.terminal = [7 x i8] c"init \0A\00"
define void @write_buf(i32 %0) {
%2 = alloca i32
%3 = alloca [7 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = bitcast [7 x i8]* %3 to i8*
store i32 0, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %27, %1
%8 = load i32, i32* %2
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 %10
%12 = call i32 @write(i32 %8, i8* %11, i32 1)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* %5
%15 = add nsw i32 %14, %13
store i32 %15, i32* %5
br label %16
16: ; preds = %7
%17 = load i32, i32* %5
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp ne i32 %22, 13
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 0
br label %27
27: ; preds = %24, %16
%28 = phi i1 [ false, %16 ], [ %26, %24 ]
br i1 %28, label %7, label %29
29: ; preds = %27
ret void
}

LLVM IR (-O2):
@__const.write_buf.terminal = [7 x i8] c"init \0A\00"
define void @write_buf(i32 %0) {
%2 = alloca [7 x i8]
%3 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %4, %1
%5 = phi i32 [ 0, %1 ], [ %9, %4 ]
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 %6
%8 = call i32 @write(i32 %0, i8* %7, i32 1)
%9 = add nsw i32 %8, %5
%10 = add nsw i32 %9, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 13
%15 = icmp sgt i32 %8, 0
%16 = and i1 %15, %14
br i1 %16, label %4, label %17
17: ; preds = %4
ret void
}