#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*/ msg;
typedef int /*<<< orphan*/ addr;
typedef int /*<<< orphan*/ SYSTEM_SOCKET_PATH;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
scalar_t__ send(int, char *, int, int /*<<< orphan*/);
int snprintf(char *, int, char *, char const *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int property_set(const char *key, const char *value) {
  static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
  struct sockaddr_un addr;
  char msg[256];
  int fd, len;
  fd = socket(PF_LOCAL, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(fd);
    return 0;
  }
  len = snprintf(msg, sizeof(msg), "%s=%s", key, value);
  if (send(fd, msg, len + 1, 0) < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<property_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    11bb <property_set+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <property_set+0x126>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    0xe1f(%rip),%rsi
mov    $0x10,%edx
call   1070 <memcpy@plt>
mov    -0x124(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1220 <property_set+0xb0>
mov    -0x124(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1296 <property_set+0x126>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x100,%esi
lea    0xdd5(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x128(%rbp)
mov    -0x124(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%edx
add    $0x1,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0x0,%rax
jge    1284 <property_set+0x114>
mov    -0x124(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <property_set+0x126>
mov    -0x124(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<property_set>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1232 <property_set+0xc2>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe2a(%rip),%rsi
mov    $0x10,%edx
call   1070 <memcpy@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1239 <property_set+0xc9>
lea    0xe15(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1040 <snprintf@plt>
lea    0x1(%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
sar    $0x3f,%rbx
jmp    1240 <property_set+0xd0>
mov    $0xffffffff,%ebx
jmp    1240 <property_set+0xd0>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x110,%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 }
@property_set.SYSTEM_SOCKET_PATH = internal constant [16 x i8] c"\00android_system\00"
@PF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@AF_UNIX = global i32 0
@.str = [6 x i8] c"%s=%s\00"
define i32 @property_set(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.sockaddr_un
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i32, i32* @PF_LOCAL
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %47
16: ; preds = %2
%17 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @property_set.SYSTEM_SOCKET_PATH, i64 0, i64 0), i32 16)
%23 = load i32, i32* %8
%24 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
store i32 0, i32* %3
br label %47
30: ; preds = %16
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %4
%33 = load i8*, i8** %5
%34 = call i32 @snprintf(i8* %31, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %32, i8* %33)
store i32 %34, i32* %9
%35 = load i32, i32* %8
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
%39 = call i64 @send(i32 %35, i8* %36, i32 %38, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %3
br label %47
44: ; preds = %30
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %47
47: ; preds = %44, %41, %27, %15
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@property_set.SYSTEM_SOCKET_PATH = internal constant [16 x i8] c"\00android_system\00"
@PF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@AF_UNIX = global i32 0
@.str = [6 x i8] c"%s=%s\00"
define i32 @property_set(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @PF_LOCAL
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %30, label %11
11: ; preds = %2
%12 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @memcpy(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @property_set.SYSTEM_SOCKET_PATH, i64 0, i64 0), i32 16)
%18 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%19 = call i64 @connect(i32 %9, %struct.sockaddr* %18, i32 8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @close(i32 %9)
br label %30
23: ; preds = %11
%24 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%25 = add nsw i32 %24, 1
%26 = call i64 @send(i32 %9, i8* %6, i32 %25, i32 0)
%27 = call i32 @close(i32 %9)
%28 = ashr i64 %26, 63
%29 = trunc i64 %28 to i32
br label %30
30: ; preds = %23, %2, %21
%31 = phi i32 [ 0, %21 ], [ -1, %2 ], [ %29, %23 ]
ret i32 %31
}