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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ checked_send(int, ...);
int /*<<< orphan*/ checked_sendfd(int, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
char *getenv(char *);
int /*<<< orphan*/ handle_error(char *);
int /*<<< orphan*/ pack_int(unsigned char *, int);
int recv(int, unsigned char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int unpack_int(unsigned char *);
int main(int argc, char **argv) {
  char *sock_path = getenv("POSEIDON_SOCK");
  if (!sock_path)
    sock_path = "/tmp/poseidon.sock";
  int s;
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    handle_error("Could not create socket");
  }
  struct sockaddr_un remote;
  remote.sun_family = AF_UNIX;
  strncpy(remote.sun_path, sock_path, sizeof(remote.sun_path) - 1);
  if (connect(s, (struct sockaddr *)&remote, sizeof(remote)) < 0) {
    handle_error("Could not connect to UNIX socket for master process");
  }
  checked_sendfd(s, 0);
  checked_sendfd(s, 1);
  checked_sendfd(s, 2);
  unsigned char bytes[4];
  pack_int(bytes, argc - 1);
  checked_send(s, bytes, 4);
  for (int i = 1; i < argc; i++) {
    checked_send(s, argv[i], strlen(argv[i]) + 1);
  }
  int t, total = 0;
  unsigned char exitstatus[4];
  while (total < 4) {
    if ((t = recv(s, exitstatus + total, 4 - total, 0)) < 0) {
      handle_error("Could not receive exitstatus from master");
    } else if (t == 0) {
      handle_error("Master hung up connection");
    }
    total += t;
  }
  close(s);
  return unpack_int(exitstatus);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe23(%rip),%rdi
call   1040 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fc <main+0x3c>
lea    0xe16(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    122d <main+0x6d>
lea    0xdf9(%rip),%rdi
call   10d0 <handle_error@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1060 <strncpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1271 <main+0xb1>
lea    0xdcd(%rip),%rdi
call   10d0 <handle_error@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1030 <checked_sendfd@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1030 <checked_sendfd@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1030 <checked_sendfd@plt>
lea    -0x2c(%rbp),%rdi
mov    -0x8(%rbp),%esi
sub    $0x1,%esi
call   10c0 <pack_int@plt>
mov    -0x1c(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   10a0 <checked_send@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1312 <main+0x152>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
mov    $0x0,%al
call   10a0 <checked_send@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12be <main+0xfe>
movl   $0x0,-0x38(%rbp)
cmpl   $0x4,-0x38(%rbp)
jge    1386 <main+0x1c6>
mov    -0x1c(%rbp),%edi
lea    -0x3c(%rbp),%rsi
movslq -0x38(%rbp),%rax
add    %rax,%rsi
mov    $0x4,%edx
sub    -0x38(%rbp),%edx
xor    %ecx,%ecx
call   1050 <recv@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    135d <main+0x19d>
lea    0xd1a(%rip),%rdi
call   10d0 <handle_error@plt>
jmp    1378 <main+0x1b8>
cmpl   $0x0,-0x34(%rbp)
jne    1373 <main+0x1b3>
lea    0xd28(%rip),%rdi
call   10d0 <handle_error@plt>
jmp    1378 <main+0x1b8>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1319 <main+0x159>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
lea    -0x3c(%rbp),%rdi
call   1090 <unpack_int@plt>
add    $0x50,%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,%r14
mov    %edi,%r15d
lea    0xe25(%rip),%rdi
call   1040 <getenv@plt>
test   %rax,%rax
lea    0xe24(%rip),%r12
cmovne %rax,%r12
mov    0x2df3(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    121a <main+0x5a>
lea    0xe0c(%rip),%rdi
call   10d0 <handle_error@plt>
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    %r12,%rsi
mov    $0x3,%edx
call   1060 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0x8,%edx
call   10b0 <connect@plt>
test   %rax,%rax
jns    1250 <main+0x90>
lea    0xdee(%rip),%rdi
call   10d0 <handle_error@plt>
xor    %ebx,%ebx
mov    %r13d,%edi
xor    %esi,%esi
call   1030 <checked_sendfd@plt>
mov    %r13d,%edi
mov    $0x1,%esi
call   1030 <checked_sendfd@plt>
mov    %r13d,%edi
mov    $0x2,%esi
call   1030 <checked_sendfd@plt>
lea    -0x1(%r15),%esi
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   10c0 <pack_int@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   10a0 <checked_send@plt>
cmp    $0x2,%r15d
jl     12d5 <main+0x115>
mov    %r15d,%r15d
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbp,8),%r12
mov    %r12,%rdi
call   1070 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r13d,%edi
mov    %r12,%rsi
xor    %eax,%eax
call   10a0 <checked_send@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    12b0 <main+0xf0>
lea    0xd91(%rip),%r14
lea    0xdb3(%rip),%r15
jmp    12fc <main+0x13c>
cs nopw 0x0(%rax,%rax,1)
nop
call   10d0 <handle_error@plt>
add    %ebp,%ebx
cmp    $0x4,%ebx
jge    1328 <main+0x168>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x4,%rsi
mov    $0x4,%edx
sub    %eax,%edx
mov    %r13d,%edi
xor    %ecx,%ecx
call   1050 <recv@plt>
mov    %eax,%ebp
mov    %r14,%rdi
test   %eax,%eax
js     12f0 <main+0x130>
mov    %r15,%rdi
jne    12f5 <main+0x135>
jmp    12f0 <main+0x130>
mov    %r13d,%edi
call   1080 <close@plt>
lea    0x4(%rsp),%rdi
call   1090 <unpack_int@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"POSEIDON_SOCK\00"
@.str.1 = [19 x i8] c"/tmp/poseidon.sock\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [24 x i8] c"Could not create socket\00"
@.str.3 = [52 x i8] c"Could not connect to UNIX socket for master process\00"
@.str.4 = [41 x i8] c"Could not receive exitstatus from master\00"
@.str.5 = [26 x i8] c"Master hung up connection\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
%9 = alloca [4 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %18
18: ; preds = %17, %2
%19 = load i32, i32* @AF_UNIX
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @handle_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = load i32, i32* @AF_UNIX
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i8*, i8** %6
%31 = call i32 @strncpy(i32 %29, i8* %30, i32 3)
%32 = load i32, i32* %7
%33 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%34 = call i64 @connect(i32 %32, %struct.sockaddr* %33, i32 8)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @handle_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %25
%39 = load i32, i32* %7
%40 = call i32 @checked_sendfd(i32 %39, i32 0)
%41 = load i32, i32* %7
%42 = call i32 @checked_sendfd(i32 %41, i32 1)
%43 = load i32, i32* %7
%44 = call i32 @checked_sendfd(i32 %43, i32 2)
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* %4
%47 = sub nsw i32 %46, 1
%48 = call i32 @pack_int(i8* %45, i32 %47)
%49 = load i32, i32* %7
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%51 = call i32 (i32, ...) @checked_send(i32 %49, i8* %50, i32 4)
store i32 1, i32* %10
br label %52
52: ; preds = %71, %38
%53 = load i32, i32* %10
%54 = load i32, i32* %4
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %74
56: ; preds = %52
%57 = load i32, i32* %7
%58 = load i8**, i8*** %5
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = load i8**, i8*** %5
%64 = load i32, i32* %10
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8*, i8** %63, i64 %65
%67 = load i8*, i8** %66
%68 = call i32 @strlen(i8* %67)
%69 = add nsw i32 %68, 1
%70 = call i32 (i32, ...) @checked_send(i32 %57, i8* %62, i32 %69)
br label %71
71: ; preds = %56
%72 = load i32, i32* %10
%73 = add nsw i32 %72, 1
store i32 %73, i32* %10
br label %52
74: ; preds = %52
store i32 0, i32* %12
br label %75
75: ; preds = %96, %74
%76 = load i32, i32* %12
%77 = icmp slt i32 %76, 4
br i1 %77, label %78, label %100
78: ; preds = %75
%79 = load i32, i32* %7
%80 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 0
%81 = load i32, i32* %12
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = load i32, i32* %12
%85 = sub nsw i32 4, %84
%86 = call i32 @recv(i32 %79, i8* %83, i32 %85, i32 0)
store i32 %86, i32* %11
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %78
%89 = call i32 @handle_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %96
90: ; preds = %78
%91 = load i32, i32* %11
%92 = icmp eq i32 %91, 0
br i1 %92, label %93, label %95
93: ; preds = %90
%94 = call i32 @handle_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %95
95: ; preds = %93, %90
br label %96
96: ; preds = %95, %88
%97 = load i32, i32* %11
%98 = load i32, i32* %12
%99 = add nsw i32 %98, %97
store i32 %99, i32* %12
br label %75
100: ; preds = %75
%101 = load i32, i32* %7
%102 = call i32 @close(i32 %101)
%103 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 0
%104 = call i32 @unpack_int(i8* %103)
ret i32 %104
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"POSEIDON_SOCK\00"
@.str.1 = [19 x i8] c"/tmp/poseidon.sock\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [24 x i8] c"Could not create socket\00"
@.str.3 = [52 x i8] c"Could not connect to UNIX socket for master process\00"
@.str.4 = [41 x i8] c"Could not receive exitstatus from master\00"
@.str.5 = [26 x i8] c"Master hung up connection\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [4 x i8]
%5 = alloca [4 x i8]
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %6
%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 slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @handle_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = bitcast %struct.sockaddr_un* %3 to i8*
%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 @strncpy(i32 undef, i8* %8, i32 3)
%20 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%21 = call i64 @connect(i32 %11, %struct.sockaddr* %20, i32 8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @handle_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = call i32 @checked_sendfd(i32 %11, i32 0)
%27 = call i32 @checked_sendfd(i32 %11, i32 1)
%28 = call i32 @checked_sendfd(i32 %11, i32 2)
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%30 = add nsw i32 %0, -1
%31 = call i32 @pack_int(i8* %29, i32 %30)
%32 = call i32 (i32, ...) @checked_send(i32 %11, i8* %29, i32 4)
%33 = icmp sgt i32 %0, 1
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = zext i32 %0 to i64
br label %38
36: ; preds = %38, %25
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
br label %47
38: ; preds = %34, %38
%39 = phi i64 [ 1, %34 ], [ %45, %38 ]
%40 = getelementptr inbounds i8*, i8** %1, i64 %39
%41 = load i8*, i8** %40
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %42, 1
%44 = call i32 (i32, ...) @checked_send(i32 %11, i8* %41, i32 %43)
%45 = add nuw nsw i64 %39, 1
%46 = icmp eq i64 %45, %35
br i1 %46, label %36, label %38
47: ; preds = %36, %59
%48 = phi i32 [ 0, %36 ], [ %60, %59 ]
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %49
%51 = sub nsw i32 4, %48
%52 = call i32 @recv(i32 %11, i8* %50, i32 %51, i32 0)
%53 = icmp slt i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %47
%55 = icmp eq i32 %52, 0
br i1 %55, label %56, label %59
56: ; preds = %54, %47
%57 = phi i8* [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), %47 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), %54 ]
%58 = call i32 @handle_error(i8* %57)
br label %59
59: ; preds = %56, %54
%60 = add nsw i32 %52, %48
%61 = icmp slt i32 %60, 4
br i1 %61, label %47, label %62
62: ; preds = %59
%63 = call i32 @close(i32 %11)
%64 = call i32 @unpack_int(i8* %37)
ret i32 %64
}