#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*/ perror(char *);
int strlen(char *);
int write(int, char *, int);
int post(int fd, char *msg) {
  int len = strlen(msg);
  while (len > 0) {
    int writed = write(fd, msg, len);
    if (writed == -1) {
      perror("test write");
      return -1;
    } else {
      msg += writed;
      len -= writed;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<post>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11aa <post+0x7a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1189 <post+0x59>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <post+0x81>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    114b <post+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<post>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    116c <post+0x3c>
mov    %eax,%ebp
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     117a <post+0x4a>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1150 <post+0x20>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%r14d
jmp    116c <post+0x3c>

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

LLVM IR (-O2):
@.str = [11 x i8] c"test write\00"
define i32 @post(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %2, %12
%6 = phi i32 [ %15, %12 ], [ %3, %2 ]
%7 = phi i8* [ %14, %12 ], [ %1, %2 ]
%8 = tail call i32 @write(i32 %0, i8* %7, i32 %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %5
%13 = sext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %7, i64 %13
%15 = sub nsw i32 %6, %8
%16 = icmp sgt i32 %15, 0
br i1 %16, label %5, label %17
17: ; preds = %12, %2, %10
%18 = phi i32 [ -1, %10 ], [ 0, %2 ], [ 0, %12 ]
ret i32 %18
}