#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 scalar_t__ ssize_t;

/* Variables and functions */
int strlen(char const *);
scalar_t__ write(int, char const *, size_t);
int write_command(int fd, const char *cmd) {
  size_t len = strlen(cmd) + 1;
  size_t off = 0;
  while (off < len) {
    ssize_t bytes = write(fd, cmd + off, len - off);
    if (bytes <= 0) {
      return -1;
    }
    off += bytes;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119b <write_command+0x7b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jg     118a <write_command+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <write_command+0x82>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1149 <write_command+0x29>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_command>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbp
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %rbp,%rbx
jae    1175 <write_command+0x55>
lea    (%r12,%rbx,1),%rsi
mov    %rbp,%rdx
sub    %rbx,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
add    %rax,%rbx
test   %rax,%rax
jg     1150 <write_command+0x30>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_command(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %6
store i64 0, i64* %7
br label %13
13: ; preds = %29, %2
%14 = load i64, i64* %7
%15 = load i64, i64* %6
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %33
17: ; preds = %13
%18 = load i32, i32* %4
%19 = load i8*, i8** %5
%20 = load i64, i64* %7
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i64, i64* %6
%23 = load i64, i64* %7
%24 = sub i64 %22, %23
%25 = call i64 @write(i32 %18, i8* %21, i64 %24)
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %17
store i32 -1, i32* %3
br label %34
29: ; preds = %17
%30 = load i64, i64* %8
%31 = load i64, i64* %7
%32 = add i64 %31, %30
store i64 %32, i64* %7
br label %13
33: ; preds = %13
store i32 0, i32* %3
br label %34
34: ; preds = %33, %28
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @write_command(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = add nsw i32 %3, 1
%5 = sext i32 %4 to i64
br label %6
6: ; preds = %9, %2
%7 = phi i64 [ 0, %2 ], [ %14, %9 ]
%8 = icmp ult i64 %7, %5
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = getelementptr inbounds i8, i8* %1, i64 %7
%11 = sub i64 %5, %7
%12 = tail call i64 @write(i32 %0, i8* %10, i64 %11)
%13 = icmp slt i64 %12, 1
%14 = add i64 %12, %7
br i1 %13, label %15, label %6
15: ; preds = %6, %9
%16 = phi i32 [ -1, %9 ], [ 0, %6 ]
ret i32 %16
}