#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 */
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void communicate_two_signals_with_server(char *sig1, char *sig2, int *res,
                                         char *buffer) {
  int sockfd, len;
  struct sockaddr_un addr;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, "/tmp/kbpm.sock");
  len = sizeof(addr);
  *res = connect(sockfd, (struct sockaddr *)&addr, len);
  if (*res == -1) {
    return;
  }
  write(sockfd, sig1, strlen(sig1));
  read(sockfd, buffer, 40 * 1024);
  if (strcmp(buffer, "pong") != 0) {
    *res = -1;
    return;
  }
  write(sockfd, sig2, strlen(sig2));
  read(sockfd, buffer, 40 * 1024);
  close(sockfd);
  *res = 0;
}


x86 assembly (clang, -O0):
<communicate_two_signals_with_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
lea    0xe36(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x8,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1090 <connect@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    11ff <communicate_two_signals_with_server+0x7f>
jmp    12a6 <communicate_two_signals_with_server+0x126>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0xa000,%edx
call   1070 <read@plt>
mov    -0x20(%rbp),%rdi
lea    0xdcf(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     125e <communicate_two_signals_with_server+0xde>
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    12a6 <communicate_two_signals_with_server+0x126>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0xa000,%edx
call   1070 <read@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<communicate_two_signals_with_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e4a(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
lea    0xe41(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <connect@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
je     1246 <communicate_two_signals_with_server+0xc6>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xa000,%edx
call   1070 <read@plt>
lea    0xe0a(%rip),%rsi
mov    %r12,%rdi
call   1080 <strcmp@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
jne    1243 <communicate_two_signals_with_server+0xc3>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xa000,%edx
call   1070 <read@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ecx,%ecx
mov    %ecx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"/tmp/kbpm.sock\00"
@.str.1 = [5 x i8] c"pong\00"
define void @communicate_two_signals_with_server(i8* %0, i8* %1, i32* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_un
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i8* %3, i8** %8
%12 = load i32, i32* @AF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %9
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strcpy(i32 %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 8, i32* %10
%20 = load i32, i32* %9
%21 = bitcast %struct.sockaddr_un* %11 to %struct.sockaddr*
%22 = load i32, i32* %10
%23 = call i32 @connect(i32 %20, %struct.sockaddr* %21, i32 %22)
%24 = load i32*, i32** %7
store i32 %23, i32* %24
%25 = load i32*, i32** %7
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %4
br label %55
29: ; preds = %4
%30 = load i32, i32* %9
%31 = load i8*, i8** %5
%32 = load i8*, i8** %5
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write(i32 %30, i8* %31, i32 %33)
%35 = load i32, i32* %9
%36 = load i8*, i8** %8
%37 = call i32 @read(i32 %35, i8* %36, i32 40960)
%38 = load i8*, i8** %8
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %29
%42 = load i32*, i32** %7
store i32 -1, i32* %42
br label %55
43: ; preds = %29
%44 = load i32, i32* %9
%45 = load i8*, i8** %6
%46 = load i8*, i8** %6
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @write(i32 %44, i8* %45, i32 %47)
%49 = load i32, i32* %9
%50 = load i8*, i8** %8
%51 = call i32 @read(i32 %49, i8* %50, i32 40960)
%52 = load i32, i32* %9
%53 = call i32 @close(i32 %52)
%54 = load i32*, i32** %7
store i32 0, i32* %54
br label %55
55: ; preds = %43, %41, %28
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"/tmp/kbpm.sock\00"
@.str.1 = [5 x i8] c"pong\00"
define void @communicate_two_signals_with_server(i8* %0, i8* %1, i32* nocapture writeonly %2, i8* %3) {
%5 = alloca %struct.sockaddr_un
%6 = bitcast %struct.sockaddr_un* %5 to i8*
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%14 = call i32 @connect(i32 %9, %struct.sockaddr* %13, i32 8)
store i32 %14, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %29, label %16
16: ; preds = %4
%17 = call i32 @strlen(i8* %0)
%18 = call i32 @write(i32 %9, i8* %0, i32 %17)
%19 = call i32 @read(i32 %9, i8* %3, i32 40960)
%20 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = call i32 @strlen(i8* %1)
%24 = call i32 @write(i32 %9, i8* %1, i32 %23)
%25 = call i32 @read(i32 %9, i8* %3, i32 40960)
%26 = call i32 @close(i32 %9)
br label %27
27: ; preds = %16, %22
%28 = phi i32 [ 0, %22 ], [ -1, %16 ]
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %4
ret void
}