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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_socket(struct sockaddr_un addr) {
  char *sp;
  char sock_path[256];
  int sock;
  sp = getenv("HOWM_SOCK");
  if (sp != NULL)
    snprintf(sock_path, sizeof(sock_path), "%s", sp);
  else
    snprintf(sock_path, sizeof(sock_path), "%s", "/tmp/howm");
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", sock_path);
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1) {
    perror("Socket error");
    exit(EXIT_FAILURE);
  }
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    perror("Connection error");
    exit(EXIT_FAILURE);
  }
  return sock;
}


x86 assembly (clang, -O0):
<setup_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11be <setup_socket+0x4e>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x100,%esi
lea    0xe56(%rip),%rdx
call   1040 <snprintf@plt>
jmp    11dd <setup_socket+0x6d>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
lea    0xe39(%rip),%rdx
lea    0xe35(%rip),%rcx
call   1040 <snprintf@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    $0x8,%esi
lea    0xdfa(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    125b <setup_socket+0xeb>
lea    0xdcf(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x124(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1292 <setup_socket+0x122>
lea    0xda5(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x124(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_socket>:
push   %rbx
sub    $0x110,%rsp
mov    %rdi,(%rsp)
mov    %esi,0x8(%rsp)
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe77(%rip),%rdx
test   %rax,%rax
je     11a7 <setup_socket+0x37>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rcx
jmp    11b8 <setup_socket+0x48>
lea    0xe5f(%rip),%rcx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e15(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rdi
lea    0xe26(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    $0x8,%esi
call   1040 <snprintf@plt>
mov    (%rbx),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     122b <setup_socket+0xbb>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1234 <setup_socket+0xc4>
mov    %ebx,%eax
add    $0x110,%rsp
pop    %rbx
ret
lea    0xde5(%rip),%rdi
jmp    123b <setup_socket+0xcb>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"HOWM_SOCK\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [10 x i8] c"/tmp/howm\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [13 x i8] c"Socket error\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [17 x i8] c"Connection error\00"
define i32 @setup_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = bitcast %struct.sockaddr_un* %3 to { i8*, i32 }*
%8 = getelementptr inbounds { i8*, i32 }, { i8*, i32 }* %7, i32 0, i32 0
store i8* %0, i8** %8
%9 = getelementptr inbounds { i8*, i32 }, { i8*, i32 }* %7, i32 0, i32 1
store i32 %1, i32* %9
%10 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 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 %13, label %17
13: ; preds = %2
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = call i32 @snprintf(i8* %14, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %20
17: ; preds = %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = call i32 @snprintf(i8* %18, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %17, %13
%21 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%22 = load i32, i32* @AF_UNIX
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%27 = call i32 @snprintf(i8* %25, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%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* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %20
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%40 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 16)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %37
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"HOWM_SOCK\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [10 x i8] c"/tmp/howm\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [13 x i8] c"Socket error\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [17 x i8] c"Connection error\00"
define i32 @setup_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [256 x i8]
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
store i8* %0, i8** %5
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %1, i32* %6
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %14
12: ; preds = %2
%13 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%16 = load i32, i32* @AF_UNIX
store i32 %16, i32* %6
%17 = load i8*, i8** %5
%18 = call i32 @snprintf(i8* %17, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%19 = load i32, i32* @AF_UNIX
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %14
%28 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%29 = call i32 @connect(i32 %21, %struct.sockaddr* %28, i32 16)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %27
ret i32 %21
}