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

/* Variables and functions */
int /*<<< orphan*/ MSG_WAITALL;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
char *__make_command(char const *, char const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
size_t recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sendall(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
size_t writeall(int, char const *, size_t);
int __process(int fd, const char *tag, const char *ftemp, const char *data,
              size_t len) {
  char *p;
  int f;
  ssize_t n;
  char buf[8];
  f = open(ftemp, O_WRONLY | O_APPEND | O_CREAT, 0644);
  if (f == -1) {
    perror("comlogger: open");
    exit(-1);
  }
  n = writeall(f, data, len);
  if (n != len) {
    perror("comlogger: write");
    exit(-1);
  }
  close(f);
  if (fd != -1) {
    p = __make_command(tag, ftemp, 1);
    if (!p) {
      perror("comlogger: __make_command");
      exit(-1);
    }
    sendall(fd, p, strlen(p), 0);
    free(p);
    n = recv(fd, buf, 4, MSG_WAITALL);
    if (n <= 0) {
      perror("comlogger: recv");
      exit(-1);
    }
    buf[n] = '\0';
    return strcmp(buf, "OK\r\n");
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<__process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1217 <__process+0x67>
lea    0xdf8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <writeall@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     124f <__process+0x9f>
lea    0xdd0(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     1328 <__process+0x178>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1090 <__make_command@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1298 <__process+0xe8>
lea    0xd98(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   10a0 <sendall@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    0x2cfa(%rip),%rax
mov    (%rax),%ecx
mov    $0x4,%edx
call   1040 <recv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
ja     1307 <__process+0x157>
lea    0xd43(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,-0x48(%rbp,%rax,1)
lea    -0x48(%rbp),%rdi
lea    0xd30(%rip),%rsi
call   1080 <strcmp@plt>
mov    %eax,-0x4(%rbp)
jmp    132f <__process+0x17f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e06(%rip),%rbp
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %r8,%rbp
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e14(%rip),%rcx
or     (%rcx),%eax
mov    %rdx,%rdi
mov    %eax,%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     129e <__process+0xee>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <writeall@plt>
cmp    %rbp,%rax
jne    12a7 <__process+0xf7>
mov    %ebx,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%r15d
je     128d <__process+0xdd>
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
call   1090 <__make_command@plt>
test   %rax,%rax
je     12b0 <__process+0x100>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   10a0 <sendall@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1040 <recv@plt>
test   %rax,%rax
je     12b9 <__process+0x109>
movb   $0x0,(%rsp,%rax,1)
lea    0xdc8(%rip),%rsi
mov    %rsp,%rdi
call   1080 <strcmp@plt>
jmp    128f <__process+0xdf>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5b(%rip),%rdi
jmp    12c0 <__process+0x110>
lea    0xd62(%rip),%rdi
jmp    12c0 <__process+0x110>
lea    0xd6a(%rip),%rdi
jmp    12c0 <__process+0x110>
lea    0xd7b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@.str = [16 x i8] c"comlogger: open\00"
@.str.1 = [17 x i8] c"comlogger: write\00"
@.str.2 = [26 x i8] c"comlogger: __make_command\00"
@MSG_WAITALL = global i32 0
@.str.3 = [16 x i8] c"comlogger: recv\00"
@.str.4 = [5 x i8] c"OK\0D\0A\00"
define i32 @__process(i32 %0, i8* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i64
%15 = alloca [8 x i8]
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%16 = load i8*, i8** %9
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_APPEND
%19 = or i32 %17, %18
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21, i32 420)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %5
%26 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %5
%29 = load i32, i32* %13
%30 = load i8*, i8** %10
%31 = load i64, i64* %11
%32 = call i64 @writeall(i32 %29, i8* %30, i64 %31)
store i64 %32, i64* %14
%33 = load i64, i64* %14
%34 = load i64, i64* %11
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %28
%40 = load i32, i32* %13
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %7
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %75
44: ; preds = %39
%45 = load i8*, i8** %8
%46 = load i8*, i8** %9
%47 = call i8* @__make_command(i8* %45, i8* %46, i32 1)
store i8* %47, i8** %12
%48 = load i8*, i8** %12
%49 = icmp ne i8* %48, null
br i1 %49, label %53, label %50
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %44
%54 = load i32, i32* %7
%55 = load i8*, i8** %12
%56 = load i8*, i8** %12
%57 = call i32 @strlen(i8* %56)
%58 = call i32 @sendall(i32 %54, i8* %55, i32 %57, i32 0)
%59 = load i8*, i8** %12
%60 = call i32 @free(i8* %59)
%61 = load i32, i32* %7
%62 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 0
%63 = load i32, i32* @MSG_WAITALL
%64 = call i64 @recv(i32 %61, i8* %62, i32 4, i32 %63)
store i64 %64, i64* %14
%65 = load i64, i64* %14
%66 = icmp ule i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %53
%68 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%69 = call i32 @exit(i32 -1)
unreachable
70: ; preds = %53
%71 = load i64, i64* %14
%72 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 %71
store i8 0, i8* %72
%73 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 0
%74 = call i32 @strcmp(i8* %73, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 %74, i32* %6
br label %76
75: ; preds = %39
store i32 0, i32* %6
br label %76
76: ; preds = %75, %70
%77 = load i32, i32* %6
ret i32 %77
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@.str = [16 x i8] c"comlogger: open\00"
@.str.1 = [17 x i8] c"comlogger: write\00"
@.str.2 = [26 x i8] c"comlogger: __make_command\00"
@MSG_WAITALL = global i32 0
@.str.3 = [16 x i8] c"comlogger: recv\00"
@.str.4 = [5 x i8] c"OK\0D\0A\00"
define i32 @__process(i32 %0, i8* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca [8 x i8]
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_APPEND
%10 = or i32 %9, %8
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* %2, i32 %12, i32 420)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %5
%19 = tail call i64 @writeall(i32 %13, i8* %3, i64 %4)
%20 = icmp eq i64 %19, %4
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %18
%25 = tail call i32 @close(i32 %13)
%26 = icmp eq i32 %0, -1
br i1 %26, label %46, label %27
27: ; preds = %24
%28 = tail call i8* @__make_command(i8* %1, i8* %2, i32 1)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 -1)
unreachable
33: ; preds = %27
%34 = tail call i32 @strlen(i8* %28)
%35 = tail call i32 @sendall(i32 %0, i8* %28, i32 %34, i32 0)
%36 = tail call i32 @free(i8* %28)
%37 = load i32, i32* @MSG_WAITALL
%38 = call i64 @recv(i32 %0, i8* %7, i32 4, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %33
%44 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 %38
store i8 0, i8* %44
%45 = call i32 @strcmp(i8* %7, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %24, %43
%47 = phi i32 [ %45, %43 ], [ 0, %24 ]
ret i32 %47
}