#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*/ a;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(int /*<<< orphan*/);
scalar_t__ connect(int, struct sockaddr *, size_t);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fail(int, char *, ...);
int getopt(int, char **, char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ optarg;
int optind;
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int strnlen(char *, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int s;
  int fdi;
  int fdo;
  int opt;
  size_t n;
  struct sockaddr_un a;
  fdi = 0;
  fdo = 1;
  while ((opt = getopt(argc, argv, "+r:w:")) != -1) {
    switch (opt) {
    case 'f':
      fdi = fdo = atoi(optarg);
      break;
    default:
      usage(argv[0]);
    }
  }
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    fail(111, "socket");
  }
  if (argc - optind < 2) {
    usage(argv[0]);
  }
  memset(&a, 0, sizeof(a));
  a.sun_family = AF_UNIX;
  strncpy(a.sun_path, argv[optind], sizeof(a.sun_path) - 1);
  n = strnlen(a.sun_path, sizeof(a.sun_path) - 1) + sizeof(a.sun_family);
  if (connect(s, (struct sockaddr *)&a, n) < 0) {
    fail(111, "connect '%s'", a.sun_path);
  }
  if (dup2(s, fdi) < 0) {
    fail(111, "dup2 fd%d", fdi);
  }
  if (dup2(s, fdo) < 0) {
    fail(111, "dup2 fd%d", fdo);
  }
  execvp(argv[optind + 1], &argv[optind + 1]);
  fail(111, "exec %s", argv[optind + 1]);
  return 111;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe1e(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     122e <main+0x7e>
mov    -0x20(%rbp),%eax
sub    $0x66,%eax
jne    121d <main+0x6d>
jmp    1204 <main+0x54>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10b0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,-0x18(%rbp)
jmp    1229 <main+0x79>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
jmp    11d4 <main+0x24>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1266 <main+0xb6>
mov    $0x6f,%edi
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x8(%rbp),%eax
mov    0x2d68(%rip),%rcx
sub    (%rcx),%eax
cmp    $0x2,%eax
jge    1287 <main+0xd7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x2d26(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rdi
mov    $0x7,%esi
call   1070 <strnlen@plt>
cltq
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    130c <main+0x15c>
mov    -0x38(%rbp),%rdx
mov    $0x6f,%edi
lea    0xd08(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1337 <main+0x187>
mov    -0x18(%rbp),%edx
mov    $0x6f,%edi
lea    0xcea(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1362 <main+0x1b2>
mov    -0x1c(%rbp),%edx
mov    $0x6f,%edi
lea    0xcbf(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x10(%rbp),%rax
mov    0x2c6b(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2c54(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
call   10a0 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    0x2c36(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
mov    $0x6f,%edi
lea    0xc6a(%rip),%rsi
mov    $0x0,%al
call   1040 <fail@plt>
mov    $0x6f,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r14d,%r14d
mov    $0x1,%r15d
lea    0xe2d(%rip),%r12
mov    0x2e1e(%rip),%r13
jmp    11e8 <main+0x38>
nopl   0x0(%rax)
mov    (%rbx),%rdi
call   1080 <usage@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1090 <getopt@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0x60>
cmp    $0x66,%eax
jne    11e0 <main+0x30>
mov    0x0(%r13),%edi
call   10b0 <atoi@plt>
mov    %eax,%r15d
mov    %eax,%r14d
jmp    11e8 <main+0x38>
mov    %r14d,0x4(%rsp)
mov    0x2dc4(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    124a <main+0x9a>
lea    0xdc8(%rip),%rsi
mov    $0x6f,%edi
xor    %eax,%eax
call   1040 <fail@plt>
mov    0x2d87(%rip),%r14
sub    (%r14),%ebp
cmp    $0x1,%ebp
jg     1261 <main+0xb1>
mov    (%rbx),%rdi
call   1080 <usage@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
movslq (%r14),%rax
mov    (%rbx,%rax,8),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    0x8(%rsp),%rdi
mov    $0x7,%esi
call   1070 <strnlen@plt>
movslq %eax,%rdx
add    $0x4,%rdx
mov    %r12d,%edi
mov    %rbp,%rsi
call   10c0 <connect@plt>
test   %rax,%rax
jns    12d1 <main+0x121>
mov    0x8(%rsp),%rdx
lea    0xd48(%rip),%rsi
mov    $0x6f,%edi
xor    %eax,%eax
call   1040 <fail@plt>
mov    %r12d,%edi
mov    0x4(%rsp),%ebp
mov    %ebp,%esi
call   1050 <dup2@plt>
test   %rax,%rax
jns    12f9 <main+0x149>
lea    0xd2f(%rip),%rsi
mov    $0x6f,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fail@plt>
mov    %r12d,%edi
mov    %r15d,%esi
call   1050 <dup2@plt>
test   %rax,%rax
jns    131f <main+0x16f>
lea    0xd0a(%rip),%rsi
mov    $0x6f,%edi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <fail@plt>
movslq (%r14),%rax
lea    (%rbx,%rax,8),%rsi
add    $0x8,%rsi
mov    0x8(%rbx,%rax,8),%rdi
call   10a0 <execvp@plt>
movslq (%r14),%rax
mov    0x8(%rbx,%rax,8),%rdx
lea    0xce1(%rip),%rsi
mov    $0x6f,%edi
xor    %eax,%eax
call   1040 <fail@plt>
mov    $0x6f,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c"+r:w:\00"
@optarg = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@optind = global i32 0
@.str.2 = [13 x i8] c"connect '%s'\00"
@.str.3 = [10 x i8] c"dup2 fd%d\00"
@.str.4 = [8 x i8] c"exec %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca %struct.sockaddr_un
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 1, i32* %8
br label %12
12: ; preds = %27, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %9
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i32, i32* %9
switch i32 %18, label %22 [
i32 102, label %19
]
19: ; preds = %17
%20 = load i32, i32* @optarg
%21 = call i32 @atoi(i32 %20)
store i32 %21, i32* %8
store i32 %21, i32* %7
br label %27
22: ; preds = %17
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 @usage(i8* %25)
br label %27
27: ; preds = %22, %19
br label %12
28: ; preds = %12
%29 = load i32, i32* @AF_UNIX
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %4
%37 = load i32, i32* @optind
%38 = sub nsw i32 %36, %37
%39 = icmp slt i32 %38, 2
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 0
%43 = load i8*, i8** %42
%44 = call i32 @usage(i8* %43)
br label %45
45: ; preds = %40, %35
%46 = call i32 @memset(%struct.sockaddr_un* %11, i32 0, i32 16)
%47 = load i32, i32* @AF_UNIX
%48 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 1
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = load i8**, i8*** %5
%52 = load i32, i32* @optind
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %51, i64 %53
%55 = load i8*, i8** %54
%56 = call i32 @strncpy(i8* %50, i8* %55, i32 7)
%57 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @strnlen(i8* %58, i32 7)
%60 = sext i32 %59 to i64
%61 = add i64 %60, 4
store i64 %61, i64* %10
%62 = load i32, i32* %6
%63 = bitcast %struct.sockaddr_un* %11 to %struct.sockaddr*
%64 = load i64, i64* %10
%65 = call i64 @connect(i32 %62, %struct.sockaddr* %63, i64 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %45
%68 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %67, %45
%72 = load i32, i32* %6
%73 = load i32, i32* %7
%74 = call i64 @dup2(i32 %72, i32 %73)
%75 = icmp slt i64 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %71
%77 = load i32, i32* %7
%78 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %76, %71
%80 = load i32, i32* %6
%81 = load i32, i32* %8
%82 = call i64 @dup2(i32 %80, i32 %81)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %87
84: ; preds = %79
%85 = load i32, i32* %8
%86 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %85)
br label %87
87: ; preds = %84, %79
%88 = load i8**, i8*** %5
%89 = load i32, i32* @optind
%90 = add nsw i32 %89, 1
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8*, i8** %88, i64 %91
%93 = load i8*, i8** %92
%94 = load i8**, i8*** %5
%95 = load i32, i32* @optind
%96 = add nsw i32 %95, 1
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8*, i8** %94, i64 %97
%99 = call i32 @execvp(i8* %93, i8** %98)
%100 = load i8**, i8*** %5
%101 = load i32, i32* @optind
%102 = add nsw i32 %101, 1
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i8*, i8** %100, i64 %103
%105 = load i8*, i8** %104
%106 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %105)
ret i32 111
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c"+r:w:\00"
@optarg = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@optind = global i32 0
@.str.2 = [13 x i8] c"connect '%s'\00"
@.str.3 = [10 x i8] c"dup2 fd%d\00"
@.str.4 = [8 x i8] c"exec %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
br label %5
5: ; preds = %10, %2
%6 = phi i32 [ %12, %10 ], [ 1, %2 ]
%7 = phi i32 [ %12, %10 ], [ 0, %2 ]
br label %8
8: ; preds = %5, %13
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %9, label %13 [
i32 -1, label %16
i32 102, label %10
]
10: ; preds = %8
%11 = load i32, i32* @optarg
%12 = tail call i32 @atoi(i32 %11)
br label %5
13: ; preds = %8
%14 = load i8*, i8** %1
%15 = tail call i32 @usage(i8* %14)
br label %8
16: ; preds = %8
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* @optind
%25 = sub nsw i32 %0, %24
%26 = icmp slt i32 %25, 2
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %1
%29 = tail call i32 @usage(i8* %28)
br label %30
30: ; preds = %27, %23
%31 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%32 = load i32, i32* @AF_UNIX
%33 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = load i32, i32* @optind
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %1, i64 %37
%39 = load i8*, i8** %38
%40 = call i32 @strncpy(i8* %35, i8* %39, i32 7)
%41 = load i8*, i8** %34
%42 = call i32 @strnlen(i8* %41, i32 7)
%43 = sext i32 %42 to i64
%44 = add nsw i64 %43, 4
%45 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%46 = call i64 @connect(i32 %19, %struct.sockaddr* %45, i64 %44)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %30
%49 = load i8*, i8** %34
%50 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %48, %30
%52 = call i64 @dup2(i32 %19, i32 %7)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %7)
br label %56
56: ; preds = %54, %51
%57 = call i64 @dup2(i32 %19, i32 %6)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %6)
br label %61
61: ; preds = %59, %56
%62 = load i32, i32* @optind
%63 = add nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %1, i64 %64
%66 = load i8*, i8** %65
%67 = call i32 @execvp(i8* %66, i8** %65)
%68 = load i32, i32* @optind
%69 = add nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8*, i8** %1, i64 %70
%72 = load i8*, i8** %71
%73 = call i32 (i32, i8*, ...) @fail(i32 111, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %72)
ret i32 111
}