#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 /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int connect(int, struct sockaddr *, int);
int dup(int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int s;
  struct sockaddr_un remote;
  char *sock_path = 0;
  if (argc != 2)
    return 1;
  sock_path = argv[1];
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  remote.sun_family = AF_UNIX;
  strcpy(remote.sun_path, sock_path);
  int len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (connect(s, (struct sockaddr *)&remote, len) == -1) {
    perror("connect");
    exit(1);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  if (dup(s) != 0)
    exit(100);
  if (dup(s) != 1)
    exit(101);
  execlp("wish", "wish", 0);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c4 <main+0x34>
movl   $0x1,-0x4(%rbp)
jmp    12cc <main+0x13c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    120b <main+0x7b>
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1262 <main+0xd2>
lea    0xdb4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
cmp    $0x0,%eax
je     1299 <main+0x109>
mov    $0x64,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
cmp    $0x1,%eax
je     12b4 <main+0x124>
mov    $0x65,%edi
call   1080 <exit@plt>
lea    0xd54(%rip),%rsi
xor    %edx,%edx
mov    %rsi,%rdi
call   10a0 <execlp@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1233 <main+0xa3>
mov    0x8(%rsi),%r14
mov    0x2e33(%rip),%r15
mov    (%r15),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xb2>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14,%rsi
call   1030 <strcpy@plt>
call   1040 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xbb>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1050 <dup@plt>
test   %eax,%eax
jne    1261 <main+0xd1>
mov    %ebx,%edi
call   1050 <dup@plt>
cmp    $0x1,%eax
jne    126b <main+0xdb>
lea    0xde6(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10a0 <execlp@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdb7(%rip),%rdi
jmp    1252 <main+0xc2>
lea    0xdb5(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x64,%edi
call   1080 <exit@plt>
mov    $0x65,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [5 x i8] c"wish\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 1, i32* %3
br label %63
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %8
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = load i32, i32* @AF_UNIX
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i8*, i8** %8
%30 = call i32 @strcpy(i32 %28, i8* %29)
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @strlen(i32 %32)
%34 = sext i32 %33 to i64
%35 = add i64 %34, 4
%36 = trunc i64 %35 to i32
store i32 %36, i32* %9
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%39 = load i32, i32* %9
%40 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %24
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %24
%46 = load i32, i32* @STDIN_FILENO
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %6
%51 = call i32 @dup(i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = call i32 @exit(i32 100)
unreachable
55: ; preds = %45
%56 = load i32, i32* %6
%57 = call i32 @dup(i32 %56)
%58 = icmp ne i32 %57, 1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @exit(i32 101)
unreachable
61: ; preds = %55
%62 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i32 1, i32* %3
br label %63
63: ; preds = %61, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [5 x i8] c"wish\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %44
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %6
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = tail call i32 @strcpy(i32 undef, i8* %8)
%20 = tail call i32 @strlen(i32 undef)
%21 = add i32 %20, 4
%22 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%23 = call i32 @connect(i32 %11, %struct.sockaddr* %22, i32 %21)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @close(i32 %31)
%33 = call i32 @dup(i32 %11)
%34 = icmp eq i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %28
%36 = call i32 @exit(i32 100)
unreachable
37: ; preds = %28
%38 = call i32 @dup(i32 %11)
%39 = icmp eq i32 %38, 1
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @exit(i32 101)
unreachable
42: ; preds = %37
%43 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %44
44: ; preds = %2, %42
ret i32 1
}