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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr_un *, int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int snprintf(int /*<<< orphan*/, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
FILE *connect_to_dropboxd(void) {
  struct sockaddr_un addr = {
      .sun_family = AF_UNIX,
  };
  FILE *sock;
  char *home;
  int sockfd;
  int ret;
  home = getenv("HOME");
  if (!home) {
    fprintf(stderr, "HOME is not set\n");
    return NULL;
  }
  ret = snprintf(addr.sun_path, sizeof(addr.sun_path),
                 "%s/.dropbox/command_socket", home);
  if (ret >= sizeof(addr.sun_path)) {
    fprintf(stderr, "path to command socket is too long\n");
    return NULL;
  }
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket");
    return NULL;
  }
  ret = connect(sockfd, &addr, sizeof(addr));
  if (ret == -1) {
    close(sockfd);
    return NULL;
  }
  sock = fdopen(sockfd, "rb");
  if (!sock) {
    perror("fdopen");
    close(sockfd);
    return NULL;
  }
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_dropboxd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x10(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe5e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <connect_to_dropboxd+0x58>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <connect_to_dropboxd+0x15c>
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%rcx
mov    $0x4,%esi
lea    0xe2b(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    $0x4,%rax
jb     1223 <connect_to_dropboxd+0xa3>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <connect_to_dropboxd+0x15c>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1262 <connect_to_dropboxd+0xe2>
lea    0xdda(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <connect_to_dropboxd+0x15c>
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1295 <connect_to_dropboxd+0x115>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <connect_to_dropboxd+0x15c>
mov    -0x24(%rbp),%edi
lea    0xdb6(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12d4 <connect_to_dropboxd+0x154>
lea    0xd9e(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <connect_to_dropboxd+0x15c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_dropboxd>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x8(%rsp)
mov    0x2e54(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe5f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11dd <connect_to_dropboxd+0x5d>
lea    0xe64(%rip),%rdx
xor    %edi,%edi
mov    $0x4,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
cmp    $0x4,%eax
jb     11f4 <connect_to_dropboxd+0x74>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1249 <connect_to_dropboxd+0xc9>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1249 <connect_to_dropboxd+0xc9>
mov    (%rbx),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1251 <connect_to_dropboxd+0xd1>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1242 <connect_to_dropboxd+0xc2>
lea    0xe2b(%rip),%rsi
mov    %ebx,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
jne    124b <connect_to_dropboxd+0xcb>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
jmp    1249 <connect_to_dropboxd+0xc9>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"HOME is not set\0A\00"
@.str.2 = [27 x i8] c"%s/.dropbox/command_socket\00"
@.str.3 = [36 x i8] c"path to command socket is too long\0A\00"
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.6 = [7 x i8] c"fdopen\00"
define i32* @connect_to_dropboxd() {
%1 = alloca i32*
%2 = alloca %struct.sockaddr_un
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
%9 = load i32, i32* @AF_UNIX
store i32 %9, i32* %8
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %1
br label %54
16: ; preds = %0
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %4
%20 = call i32 @snprintf(i32 %18, i32 4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = icmp uge i64 %22, 4
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32* null, i32** %1
br label %54
27: ; preds = %16
%28 = load i32, i32* @AF_UNIX
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32* null, i32** %1
br label %54
35: ; preds = %27
%36 = load i32, i32* %5
%37 = call i32 @connect(i32 %36, %struct.sockaddr_un* %2, i32 8)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
store i32* null, i32** %1
br label %54
43: ; preds = %35
%44 = load i32, i32* %5
%45 = call i32* @fdopen(i32 %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %45, i32** %3
%46 = load i32*, i32** %3
%47 = icmp ne i32* %46, null
br i1 %47, label %52, label %48
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%50 = load i32, i32* %5
%51 = call i32 @close(i32 %50)
store i32* null, i32** %1
br label %54
52: ; preds = %43
%53 = load i32*, i32** %3
store i32* %53, i32** %1
br label %54
54: ; preds = %52, %48, %40, %33, %24, %13
%55 = load i32*, i32** %1
ret i32* %55
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"HOME is not set\0A\00"
@.str.2 = [27 x i8] c"%s/.dropbox/command_socket\00"
@.str.3 = [36 x i8] c"path to command socket is too long\0A\00"
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.6 = [7 x i8] c"fdopen\00"
define noalias i32* @connect_to_dropboxd() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
store i32 0, i32* %3
%4 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
%5 = load i32, i32* @AF_UNIX
store i32 %5, i32* %4
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %35
11: ; preds = %0
%12 = tail call i32 @snprintf(i32 0, i32 4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%13 = icmp ugt i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %35
17: ; preds = %11
%18 = load i32, i32* @AF_UNIX
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %35
24: ; preds = %17
%25 = call i32 @connect(i32 %20, %struct.sockaddr_un* %1, i32 8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @close(i32 %20)
br label %35
29: ; preds = %24
%30 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%34 = call i32 @close(i32 %20)
br label %35
35: ; preds = %29, %32, %27, %22, %14, %8
%36 = phi i32* [ null, %14 ], [ null, %22 ], [ null, %27 ], [ null, %32 ], [ null, %8 ], [ %30, %29 ]
ret i32* %36
}