#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*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
long strlen(char *);
long write(int, char *, long);
int puttext2channel(char *bufftext, long size, char *realfilename, char *json,
                    int channelfd) {
  long totalbytewrite, bytewrite;
  bytewrite = 0;
  totalbytewrite = 0;
  char tempbuff[strlen(realfilename) > 20 ? strlen(realfilename) + 1 : 20];
  if (channelfd < 0) {
    printf("*** ZVM error write text to file. Wrong file descriptor\n");
    return -1;
  }
  sprintf(tempbuff, "%ld ", size);
  bytewrite = write(channelfd, tempbuff, strlen(tempbuff));
  if (bytewrite == 0) {
    printf("*** ZVM error write data to file\n");
    return -1;
  }
  sprintf(tempbuff, "%s //", realfilename);
  totalbytewrite += bytewrite;
  bytewrite = write(channelfd, tempbuff, strlen(tempbuff));
  if (bytewrite == 0) {
    printf("*** ZVM error write data to file\n");
    return -1;
  }
  totalbytewrite += bytewrite;
  bytewrite = write(channelfd, json, strlen(json));
  if (bytewrite == 0) {
    printf("*** ZVM error write json data to file\n");
    return -1;
  }
  totalbytewrite += bytewrite;
  bytewrite = write(channelfd, "~~", 2);
  if (bytewrite == 0) {
    printf("*** ZVM error write data to file\n");
    return -1;
  }
  totalbytewrite += bytewrite;
  bytewrite = write(channelfd, bufftext, size);
  if (bytewrite == 0) {
    printf("*** ZVM error write data to file\n");
    return -1;
  }
  totalbytewrite += bytewrite;
  return totalbytewrite;
}


x86 assembly (clang, -O0):
<puttext2channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x14,%rax
jle    1198 <puttext2channel+0x58>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
jmp    11a6 <puttext2channel+0x66>
mov    $0x14,%eax
mov    %rax,-0x60(%rbp)
jmp    11a6 <puttext2channel+0x66>
mov    -0x60(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11f6 <puttext2channel+0xb6>
lea    0xe22(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13d8 <puttext2channel+0x298>
mov    -0x68(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x68(%rbp),%rdi
mov    -0x2c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1258 <puttext2channel+0x118>
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13d8 <puttext2channel+0x298>
mov    -0x68(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x68(%rbp),%rdi
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x70(%rbp)
call   1040 <strlen@plt>
mov    -0x70(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12c6 <puttext2channel+0x186>
lea    0xd90(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13d8 <puttext2channel+0x298>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1326 <puttext2channel+0x1e6>
lea    0xd58(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13d8 <puttext2channel+0x298>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
lea    0xd51(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1374 <puttext2channel+0x234>
lea    0xce2(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13d8 <puttext2channel+0x298>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    13be <puttext2channel+0x27e>
lea    0xc98(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13d8 <puttext2channel+0x298>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<puttext2channel>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebx
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,-0x38(%rbp)
mov    %rdx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rcx
cmp    $0x15,%rax
mov    $0x14,%eax
cmovge %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
test   %ebx,%ebx
js     125d <puttext2channel+0x11d>
lea    0xe66(%rip),%rsi
mov    %r13,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
test   %rax,%rax
je     1254 <puttext2channel+0x114>
mov    %rax,-0x30(%rbp)
lea    0xe35(%rip),%rsi
mov    %r13,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
test   %rax,%rax
je     1254 <puttext2channel+0x114>
mov    %rax,%r15
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
test   %rax,%rax
je     127d <puttext2channel+0x13d>
mov    %rax,%r12
lea    0xded(%rip),%rsi
mov    $0x2,%edx
mov    %ebx,%edi
call   1040 <write@plt>
test   %rax,%rax
je     1254 <puttext2channel+0x114>
mov    %rax,%r13
mov    %ebx,%edi
mov    -0x38(%rbp),%rsi
mov    %r14,%rdx
call   1040 <write@plt>
test   %rax,%rax
je     1254 <puttext2channel+0x114>
add    -0x30(%rbp),%r15d
add    %r15d,%r12d
add    %r12d,%r13d
add    %r13d,%eax
jmp    126e <puttext2channel+0x12e>
lea    0xdd9(%rip),%rdi
jmp    1264 <puttext2channel+0x124>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8a(%rip),%rdi
jmp    1264 <puttext2channel+0x124>

LLVM IR (-O0):
@.str = [57 x i8] c"*** ZVM error write text to file. Wrong file descriptor\0A\00"
@.str.1 = [5 x i8] c"%ld \00"
@.str.2 = [34 x i8] c"*** ZVM error write data to file\0A\00"
@.str.3 = [6 x i8] c"%s //\00"
@.str.4 = [39 x i8] c"*** ZVM error write json data to file\0A\00"
@.str.5 = [3 x i8] c"~~\00"
define i32 @puttext2channel(i8* %0, i64 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i64 0, i64* %13
store i64 0, i64* %12
%17 = load i8*, i8** %9
%18 = call i64 @strlen(i8* %17)
%19 = icmp sgt i64 %18, 20
br i1 %19, label %20, label %24
20: ; preds = %5
%21 = load i8*, i8** %9
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 %22, 1
br label %25
24: ; preds = %5
br label %25
25: ; preds = %24, %20
%26 = phi i64 [ %23, %20 ], [ 20, %24 ]
%27 = call i8* @llvm.stacksave()
store i8* %27, i8** %14
%28 = alloca i8, i64 %26
store i64 %26, i64* %15
%29 = load i32, i32* %11
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %16
br label %97
33: ; preds = %25
%34 = load i64, i64* %8
%35 = call i32 (i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %34)
%36 = load i32, i32* %11
%37 = call i64 @strlen(i8* %28)
%38 = call i64 @write(i32 %36, i8* %28, i64 %37)
store i64 %38, i64* %13
%39 = load i64, i64* %13
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %16
br label %97
43: ; preds = %33
%44 = load i8*, i8** %9
%45 = call i32 (i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = load i64, i64* %13
%47 = load i64, i64* %12
%48 = add nsw i64 %47, %46
store i64 %48, i64* %12
%49 = load i32, i32* %11
%50 = call i64 @strlen(i8* %28)
%51 = call i64 @write(i32 %49, i8* %28, i64 %50)
store i64 %51, i64* %13
%52 = load i64, i64* %13
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %43
%55 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %16
br label %97
56: ; preds = %43
%57 = load i64, i64* %13
%58 = load i64, i64* %12
%59 = add nsw i64 %58, %57
store i64 %59, i64* %12
%60 = load i32, i32* %11
%61 = load i8*, i8** %10
%62 = load i8*, i8** %10
%63 = call i64 @strlen(i8* %62)
%64 = call i64 @write(i32 %60, i8* %61, i64 %63)
store i64 %64, i64* %13
%65 = load i64, i64* %13
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %56
%68 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %16
br label %97
69: ; preds = %56
%70 = load i64, i64* %13
%71 = load i64, i64* %12
%72 = add nsw i64 %71, %70
store i64 %72, i64* %12
%73 = load i32, i32* %11
%74 = call i64 @write(i32 %73, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 2)
store i64 %74, i64* %13
%75 = load i64, i64* %13
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %69
%78 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %16
br label %97
79: ; preds = %69
%80 = load i64, i64* %13
%81 = load i64, i64* %12
%82 = add nsw i64 %81, %80
store i64 %82, i64* %12
%83 = load i32, i32* %11
%84 = load i8*, i8** %7
%85 = load i64, i64* %8
%86 = call i64 @write(i32 %83, i8* %84, i64 %85)
store i64 %86, i64* %13
%87 = load i64, i64* %13
%88 = icmp eq i64 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %79
%90 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %16
br label %97
91: ; preds = %79
%92 = load i64, i64* %13
%93 = load i64, i64* %12
%94 = add nsw i64 %93, %92
store i64 %94, i64* %12
%95 = load i64, i64* %12
%96 = trunc i64 %95 to i32
store i32 %96, i32* %6
store i32 1, i32* %16
br label %97
97: ; preds = %91, %89, %77, %67, %54, %41, %31
%98 = load i8*, i8** %14
%99 = load i32, i32* %6
ret i32 %99
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%ld \00"
@.str.3 = [6 x i8] c"%s //\00"
@.str.5 = [3 x i8] c"~~\00"
@str.7 = [38 x i8] c"*** ZVM error write json data to file\00"
@str.9 = [33 x i8] c"*** ZVM error write data to file\00"
@str.10 = [56 x i8] c"*** ZVM error write text to file. Wrong file descriptor\00"
define i32 @puttext2channel(i8* nocapture %0, i64 %1, i8* %2, i8* nocapture %3, i32 %4) {
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = icmp sgt i64 %6, 20
%8 = add nsw i64 %6, 1
%9 = select i1 %7, i64 %8, i64 20
%10 = alloca i8, i64 %9
%11 = icmp slt i32 %4, 0
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.10, i64 0, i64 0))
br label %50
14: ; preds = %5
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%16 = call i64 @strlen(i8* %10)
%17 = call i64 @write(i32 %4, i8* %10, i64 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
br label %50
21: ; preds = %14
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%23 = call i64 @strlen(i8* %10)
%24 = call i64 @write(i32 %4, i8* %10, i64 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
br label %50
28: ; preds = %21
%29 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%30 = tail call i64 @write(i32 %4, i8* %3, i64 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0))
br label %50
34: ; preds = %28
%35 = tail call i64 @write(i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
br label %50
39: ; preds = %34
%40 = tail call i64 @write(i32 %4, i8* %0, i64 %1)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
br label %50
44: ; preds = %39
%45 = add nsw i64 %24, %17
%46 = add nsw i64 %45, %30
%47 = add nsw i64 %46, %35
%48 = add nsw i64 %47, %40
%49 = trunc i64 %48 to i32
br label %50
50: ; preds = %44, %42, %37, %32, %26, %19, %12
%51 = phi i32 [ -1, %12 ], [ -1, %19 ], [ -1, %26 ], [ -1, %32 ], [ -1, %37 ], [ -1, %42 ], [ %49, %44 ]
ret i32 %51
}