#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
size_t write(int, unsigned char *, size_t);
int send_to_camd(unsigned char *buf, size_t len) {
  struct sockaddr_un servaddr;
  int clilen;
  int sock;
  memset(&servaddr, 0, sizeof(struct sockaddr_un));
  servaddr.sun_family = AF_UNIX;
  strcpy(servaddr.sun_path, "/tmp/camd.socket");
  clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
  connect(sock, (struct sockaddr *)&servaddr, clilen);
  if (write(sock, buf, len) == len)
    return EXIT_SUCCESS;
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<send_to_camd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    0xe6a(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x24(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <connect@plt>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x18(%rbp),%rax
jne    1202 <send_to_camd+0xa2>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <send_to_camd+0xae>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_to_camd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe62(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%edi
call   1050 <strlen@plt>
lea    0x4(%rax),%ebp
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1070 <connect@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <write@plt>
cmp    %r12,%rax
jne    11f1 <send_to_camd+0x91>
mov    0x2dd1(%rip),%rax
jmp    11f8 <send_to_camd+0x98>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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
@.str = [17 x i8] c"/tmp/camd.socket\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @send_to_camd(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @strcpy(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strlen(i32 %16)
%18 = sext i32 %17 to i64
%19 = add i64 4, %18
%20 = trunc i64 %19 to i32
store i32 %20, i32* %7
%21 = load i32, i32* @PF_UNIX
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%26 = load i32, i32* %7
%27 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 %26)
%28 = load i32, i32* %8
%29 = load i8*, i8** %4
%30 = load i64, i64* %5
%31 = call i64 @write(i32 %28, i8* %29, i64 %30)
%32 = load i64, i64* %5
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %2
%35 = load i32, i32* @EXIT_SUCCESS
store i32 %35, i32* %3
br label %38
36: ; preds = %2
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %34
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str = [17 x i8] c"/tmp/camd.socket\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @send_to_camd(i8* nocapture %0, i64 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @strcpy(i32 %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %8
%12 = call i32 @strlen(i32 %11)
%13 = add i32 %12, 4
%14 = load i32, i32* @PF_UNIX
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%18 = call i32 @connect(i32 %16, %struct.sockaddr* %17, i32 %13)
%19 = call i64 @write(i32 %16, i8* %0, i64 %1)
%20 = icmp eq i64 %19, %1
%21 = load i32, i32* @EXIT_SUCCESS
%22 = load i32, i32* @EXIT_FAILURE
%23 = select i1 %20, i32 %21, i32 %22
ret i32 %23
}