#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
void send_tag(int value) {
  unsigned char buf[2];
  int res, left;
  buf[0] = 1U;
  buf[1] = (unsigned char)value;
  left = 2;
  while (left > 0) {
    if ((res = write(1, buf + left - 2, left)) <= 0) {
      perror("Error writing to pipe");
      exit(1);
    } else {
      left -= res;
    }
  }
}


x86 assembly (clang, -O0):
<send_tag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movb   $0x1,-0x6(%rbp)
mov    -0x4(%rbp),%eax
mov    %al,-0x5(%rbp)
movl   $0x2,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11a4 <send_tag+0x74>
lea    -0x6(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
add    $0xfffffffffffffffe,%rsi
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     1194 <send_tag+0x64>
lea    0xe7b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    114c <send_tag+0x1c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_tag>:
push   %rbx
sub    $0x10,%rsp
movb   $0x1,0xe(%rsp)
mov    %dil,0xf(%rsp)
mov    $0x2,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%eax
lea    (%rsp,%rax,1),%rsi
add    $0xc,%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    1176 <send_tag+0x46>
sub    %eax,%ebx
test   %ebx,%ebx
jg     1150 <send_tag+0x20>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe83(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Error writing to pipe\00"
define void @send_tag(i32 %0) {
%2 = alloca i32
%3 = alloca [2 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 1, i8* %6
%7 = load i32, i32* %2
%8 = trunc i32 %7 to i8
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 %8, i8* %9
store i32 2, i32* %5
br label %10
10: ; preds = %29, %1
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 -2
%19 = load i32, i32* %5
%20 = call i32 @write(i32 1, i8* %18, i32 %19)
store i32 %20, i32* %4
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %13
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = sub nsw i32 %27, %26
store i32 %28, i32* %5
br label %29
29: ; preds = %25
br label %10
30: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"Error writing to pipe\00"
define void @send_tag(i32 %0) {
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
store i8 1, i8* %3
%4 = trunc i32 %0 to i8
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 %4, i8* %5
br label %8
6: ; preds = %8
%7 = icmp sgt i32 %15, 0
br i1 %7, label %8, label %19
8: ; preds = %1, %6
%9 = phi i32 [ 2, %1 ], [ %15, %6 ]
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 %10
%12 = getelementptr inbounds i8, i8* %11, i64 -2
%13 = call i32 @write(i32 1, i8* %12, i32 %9)
%14 = icmp slt i32 %13, 1
%15 = sub nsw i32 %9, %13
br i1 %14, label %16, label %6
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %6
ret void
}