#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*/ qubesd_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int strlen(char *);
char *strrchr(char *, char);
int /*<<< orphan*/ write_wrapper(int, char *, int);
int main(int argc, char **argv) {
  char *source_domain = getenv("QREXEC_REMOTE_DOMAIN");
  char *target_domain = getenv("QREXEC_REQUESTED_TARGET");
  char *service_name = strrchr(argv[0], '/');
  int fd;
  char buf[4096];
  int read_ret;
  struct sockaddr_un qubesd_addr = {
      .sun_family = AF_UNIX,
      .sun_path = "/var/run/qubesd.sock",
  };
  if (service_name)
    service_name++;
  if (!source_domain || !target_domain || !service_name || argc > 2) {
    fprintf(stderr, "Usage: %s [service-argument]\n", argv[0]);
    fprintf(stderr, "\n");
    fprintf(stderr, "Expected environment variables:\n");
    fprintf(stderr, " - QREXEC_REMOTE_DOMAIN - source domain for the call\n");
    fprintf(stderr,
            " - QREXEC_REQUESTED_TARGET - target domain for the call\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Additionally, this program assumes being called with "
                    "desired service name as argv[0] (use symlink)\n");
    return 1;
  }
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd == -1) {
    perror("socket");
    return 1;
  }
  if (connect(fd, (struct sockaddr *)&qubesd_addr, sizeof(qubesd_addr)) == -1) {
    perror("connect to qubesd");
    return 1;
  }
  write_wrapper(fd, source_domain, strlen(source_domain) + 1);
  write_wrapper(fd, service_name, strlen(service_name) + 1);
  write_wrapper(fd, target_domain, strlen(target_domain) + 1);
  if (argc == 2)
    write_wrapper(fd, argv[1], strlen(argv[1]) + 1);
  else
    write_wrapper(fd, "\0", 1);
  while ((read_ret = read(0, buf, sizeof(buf))) > 0)
    write_wrapper(fd, buf, read_ret);
  if (read_ret == -1) {
    perror("read from stdin");
    return 1;
  }
  shutdown(fd, SHUT_WR);
  while ((read_ret = read(fd, buf, sizeof(buf))) > 0)
    write_wrapper(1, buf, read_ret);
  if (read_ret == -1) {
    perror("read from qubesd");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1090,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
lea    0xe45(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x28(%rbp)
lea    0xe38(%rip),%rax
mov    %rax,-0x1048(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1040(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1222 <main+0x82>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     124d <main+0xad>
cmpq   $0x0,-0x20(%rbp)
je     124d <main+0xad>
cmpq   $0x0,-0x28(%rbp)
je     124d <main+0xad>
cmpl   $0x2,-0x8(%rbp)
jle    1301 <main+0x161>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xed7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1551 <main+0x3b1>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    133f <main+0x19f>
lea    0xe26(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1551 <main+0x3b1>
mov    -0x2c(%rbp),%edi
lea    -0x1048(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1374 <main+0x1d4>
lea    0xdf8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1551 <main+0x3b1>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1074(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x1070(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x1074(%rbp),%edi
mov    -0x1070(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1040 <write_wrapper@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1064(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x1060(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x1064(%rbp),%edi
mov    -0x1060(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1040 <write_wrapper@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1054(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x1054(%rbp),%edi
mov    -0x1050(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1040 <write_wrapper@plt>
cmpl   $0x2,-0x8(%rbp)
jne    145b <main+0x2bb>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1084(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x1080(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    -0x1084(%rbp),%edi
mov    -0x1080(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1040 <write_wrapper@plt>
jmp    146f <main+0x2cf>
mov    -0x2c(%rbp),%edi
lea    0xd29(%rip),%rsi
mov    $0x1,%edx
call   1040 <write_wrapper@plt>
jmp    1474 <main+0x2d4>
lea    -0x1030(%rbp),%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x0,%eax
jle    14b0 <main+0x310>
mov    -0x2c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
call   1040 <write_wrapper@plt>
jmp    1474 <main+0x2d4>
cmpl   $0xffffffff,-0x1034(%rbp)
jne    14d5 <main+0x335>
lea    0xca9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1551 <main+0x3b1>
mov    -0x2c(%rbp),%edi
mov    0x2b09(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x2c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x0,%eax
jle    1525 <main+0x385>
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
mov    $0x1,%edi
call   1040 <write_wrapper@plt>
jmp    14e6 <main+0x346>
cmpl   $0xffffffff,-0x1034(%rbp)
jne    154a <main+0x3aa>
lea    0xc44(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1551 <main+0x3b1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1090,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r13
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
mov    (%r14),%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0xe41(%rip),%rcx
mov    %rcx,(%rsp)
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edi
mov    %edi,0x8(%rsp)
lea    0x1(%rax),%rbp
test   %rax,%rax
cmove  %rax,%rbp
cmp    $0x2,%r15d
jg     12c2 <main+0x122>
test   %r13,%r13
je     12c2 <main+0x122>
test   %r12,%r12
je     12c2 <main+0x122>
test   %rax,%rax
je     12c2 <main+0x122>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     140d <main+0x26d>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     1416 <main+0x276>
mov    %r13,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %r13,%rsi
call   1040 <write_wrapper@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <write_wrapper@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1040 <write_wrapper@plt>
cmp    $0x2,%r15d
jne    134c <main+0x1ac>
mov    0x8(%r14),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %rbp,%rsi
jmp    135a <main+0x1ba>
mov    0x2d07(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xd6f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe6b(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xd65(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xd75(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xd9a(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xdb5(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%ecx
jmp    13f9 <main+0x259>
lea    0xe3b(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write_wrapper@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    139f <main+0x1ff>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write_wrapper@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1380 <main+0x1e0>
cmp    $0xffffffff,%eax
je     141f <main+0x27f>
mov    0x2c3d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <shutdown@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    13f2 <main+0x252>
lea    0x10(%rsp),%rbp
xchg   %ax,%ax
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write_wrapper@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     13d0 <main+0x230>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
je     1428 <main+0x288>
mov    %ecx,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd40(%rip),%rdi
jmp    142f <main+0x28f>
lea    0xd3e(%rip),%rdi
jmp    142f <main+0x28f>
lea    0xd47(%rip),%rdi
jmp    142f <main+0x28f>
lea    0xd4e(%rip),%rdi
call   10a0 <perror@plt>
jmp    1342 <main+0x1a2>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"QREXEC_REMOTE_DOMAIN\00"
@.str.1 = [24 x i8] c"QREXEC_REQUESTED_TARGET\00"
@.str.2 = [21 x i8] c"/var/run/qubesd.sock\00"
@AF_UNIX = global i32 0
@stderr = global i32 0
@.str.3 = [30 x i8] c"Usage: %s [service-argument]\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [33 x i8] c"Expected environment variables:\0A\00"
@.str.6 = [54 x i8] c" - QREXEC_REMOTE_DOMAIN - source domain for the call\0A\00"
@.str.7 = [57 x i8] c" - QREXEC_REQUESTED_TARGET - target domain for the call\0A\00"
@.str.8 = [100 x i8] c"Additionally, this program assumes being called with desired service name as argv[0] (use symlink)\0A\00"
@SOCK_STREAM = global i32 0
@.str.9 = [7 x i8] c"socket\00"
@.str.10 = [18 x i8] c"connect to qubesd\00"
@.str.11 = [2 x i8] zeroinitializer
@.str.12 = [16 x i8] c"read from stdin\00"
@SHUT_WR = global i32 0
@.str.13 = [17 x i8] c"read from qubesd\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca i32
%12 = alloca %struct.sockaddr_un
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %6
%14 = call i8* @getenv(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i8* %14, i8** %7
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i8* @strrchr(i8* %17, i8 signext 47)
store i8* %18, i8** %8
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 1
%21 = load i32, i32* @AF_UNIX
store i32 %21, i32* %20
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = load i8*, i8** %8
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %8
br label %27
27: ; preds = %24, %2
%28 = load i8*, i8** %6
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = load i8*, i8** %7
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %4
%38 = icmp sgt i32 %37, 2
br i1 %38, label %39, label %57
39: ; preds = %36, %33, %30, %27
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 0
%43 = load i8*, i8** %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.7, i64 0, i64 0))
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %141
57: ; preds = %36
%58 = load i32, i32* @AF_UNIX
%59 = load i32, i32* @SOCK_STREAM
%60 = call i32 @socket(i32 %58, i32 %59, i32 0)
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %141
65: ; preds = %57
%66 = load i32, i32* %9
%67 = bitcast %struct.sockaddr_un* %12 to %struct.sockaddr*
%68 = call i32 @connect(i32 %66, %struct.sockaddr* %67, i32 16)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %3
br label %141
72: ; preds = %65
%73 = load i32, i32* %9
%74 = load i8*, i8** %6
%75 = load i8*, i8** %6
%76 = call i32 @strlen(i8* %75)
%77 = add nsw i32 %76, 1
%78 = call i32 @write_wrapper(i32 %73, i8* %74, i32 %77)
%79 = load i32, i32* %9
%80 = load i8*, i8** %8
%81 = load i8*, i8** %8
%82 = call i32 @strlen(i8* %81)
%83 = add nsw i32 %82, 1
%84 = call i32 @write_wrapper(i32 %79, i8* %80, i32 %83)
%85 = load i32, i32* %9
%86 = load i8*, i8** %7
%87 = load i8*, i8** %7
%88 = call i32 @strlen(i8* %87)
%89 = add nsw i32 %88, 1
%90 = call i32 @write_wrapper(i32 %85, i8* %86, i32 %89)
%91 = load i32, i32* %4
%92 = icmp eq i32 %91, 2
br i1 %92, label %93, label %104
93: ; preds = %72
%94 = load i32, i32* %9
%95 = load i8**, i8*** %5
%96 = getelementptr inbounds i8*, i8** %95, i64 1
%97 = load i8*, i8** %96
%98 = load i8**, i8*** %5
%99 = getelementptr inbounds i8*, i8** %98, i64 1
%100 = load i8*, i8** %99
%101 = call i32 @strlen(i8* %100)
%102 = add nsw i32 %101, 1
%103 = call i32 @write_wrapper(i32 %94, i8* %97, i32 %102)
br label %107
104: ; preds = %72
%105 = load i32, i32* %9
%106 = call i32 @write_wrapper(i32 %105, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0), i32 1)
br label %107
107: ; preds = %104, %93
br label %108
108: ; preds = %112, %107
%109 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%110 = call i32 @read(i32 0, i8* %109, i32 4096)
store i32 %110, i32* %11
%111 = icmp sgt i32 %110, 0
br i1 %111, label %112, label %117
112: ; preds = %108
%113 = load i32, i32* %9
%114 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%115 = load i32, i32* %11
%116 = call i32 @write_wrapper(i32 %113, i8* %114, i32 %115)
br label %108
117: ; preds = %108
%118 = load i32, i32* %11
%119 = icmp eq i32 %118, -1
br i1 %119, label %120, label %122
120: ; preds = %117
%121 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.12, i64 0, i64 0))
store i32 1, i32* %3
br label %141
122: ; preds = %117
%123 = load i32, i32* %9
%124 = load i32, i32* @SHUT_WR
%125 = call i32 @shutdown(i32 %123, i32 %124)
br label %126
126: ; preds = %131, %122
%127 = load i32, i32* %9
%128 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%129 = call i32 @read(i32 %127, i8* %128, i32 4096)
store i32 %129, i32* %11
%130 = icmp sgt i32 %129, 0
br i1 %130, label %131, label %135
131: ; preds = %126
%132 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%133 = load i32, i32* %11
%134 = call i32 @write_wrapper(i32 1, i8* %132, i32 %133)
br label %126
135: ; preds = %126
%136 = load i32, i32* %11
%137 = icmp eq i32 %136, -1
br i1 %137, label %138, label %140
138: ; preds = %135
%139 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i64 0, i64 0))
store i32 1, i32* %3
br label %141
140: ; preds = %135
store i32 0, i32* %3
br label %141
141: ; preds = %140, %138, %120, %70, %63, %39
%142 = load i32, i32* %3
ret i32 %142
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"QREXEC_REMOTE_DOMAIN\00"
@.str.1 = [24 x i8] c"QREXEC_REQUESTED_TARGET\00"
@.str.2 = [21 x i8] c"/var/run/qubesd.sock\00"
@AF_UNIX = global i32 0
@stderr = global i32 0
@.str.3 = [30 x i8] c"Usage: %s [service-argument]\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [33 x i8] c"Expected environment variables:\0A\00"
@.str.6 = [54 x i8] c" - QREXEC_REMOTE_DOMAIN - source domain for the call\0A\00"
@.str.7 = [57 x i8] c" - QREXEC_REQUESTED_TARGET - target domain for the call\0A\00"
@.str.8 = [100 x i8] c"Additionally, this program assumes being called with desired service name as argv[0] (use symlink)\0A\00"
@SOCK_STREAM = global i32 0
@.str.9 = [7 x i8] c"socket\00"
@.str.10 = [18 x i8] c"connect to qubesd\00"
@.str.11 = [2 x i8] zeroinitializer
@.str.12 = [16 x i8] c"read from stdin\00"
@SHUT_WR = global i32 0
@.str.13 = [17 x i8] c"read from qubesd\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.sockaddr_un
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%7 = load i8*, i8** %1
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %7, i8 signext 47)
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%10 = bitcast %struct.sockaddr_un* %4 to i8*
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
%13 = load i32, i32* @AF_UNIX
store i32 %13, i32* %12
%14 = icmp eq i8* %8, null
%15 = getelementptr inbounds i8, i8* %8, i64 1
%16 = select i1 %14, i8* null, i8* %15
%17 = icmp eq i8* %5, null
%18 = icmp eq i8* %6, null
%19 = or i1 %17, %18
%20 = select i1 %19, i1 true, i1 %14
%21 = icmp sgt i32 %0, 2
%22 = or i1 %21, %20
br i1 %22, label %23, label %38
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0))
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.7, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.8, i64 0, i64 0))
br label %97
38: ; preds = %2
%39 = load i32, i32* @SOCK_STREAM
%40 = tail call i32 @socket(i32 %13, i32 %39, i32 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
br label %97
44: ; preds = %38
%45 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%46 = call i32 @connect(i32 %40, %struct.sockaddr* %45, i32 16)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0))
br label %97
50: ; preds = %44
%51 = call i32 @strlen(i8* %5)
%52 = add nsw i32 %51, 1
%53 = call i32 @write_wrapper(i32 %40, i8* %5, i32 %52)
%54 = call i32 @strlen(i8* %16)
%55 = add nsw i32 %54, 1
%56 = call i32 @write_wrapper(i32 %40, i8* %16, i32 %55)
%57 = call i32 @strlen(i8* %6)
%58 = add nsw i32 %57, 1
%59 = call i32 @write_wrapper(i32 %40, i8* %6, i32 %58)
%60 = icmp eq i32 %0, 2
br i1 %60, label %61, label %67
61: ; preds = %50
%62 = getelementptr inbounds i8*, i8** %1, i64 1
%63 = load i8*, i8** %62
%64 = call i32 @strlen(i8* %63)
%65 = add nsw i32 %64, 1
%66 = call i32 @write_wrapper(i32 %40, i8* %63, i32 %65)
br label %69
67: ; preds = %50
%68 = call i32 @write_wrapper(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0), i32 1)
br label %69
69: ; preds = %67, %61
%70 = call i32 @read(i32 0, i8* %9, i32 4096)
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %69, %72
%73 = phi i32 [ %75, %72 ], [ %70, %69 ]
%74 = call i32 @write_wrapper(i32 %40, i8* %9, i32 %73)
%75 = call i32 @read(i32 0, i8* %9, i32 4096)
%76 = icmp sgt i32 %75, 0
br i1 %76, label %72, label %77
77: ; preds = %72, %69
%78 = phi i32 [ %70, %69 ], [ %75, %72 ]
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %82
80: ; preds = %77
%81 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.12, i64 0, i64 0))
br label %97
82: ; preds = %77
%83 = load i32, i32* @SHUT_WR
%84 = call i32 @shutdown(i32 %40, i32 %83)
%85 = call i32 @read(i32 %40, i8* %9, i32 4096)
%86 = icmp sgt i32 %85, 0
br i1 %86, label %87, label %92
87: ; preds = %82, %87
%88 = phi i32 [ %90, %87 ], [ %85, %82 ]
%89 = call i32 @write_wrapper(i32 1, i8* %9, i32 %88)
%90 = call i32 @read(i32 %40, i8* %9, i32 4096)
%91 = icmp sgt i32 %90, 0
br i1 %91, label %87, label %92
92: ; preds = %87, %82
%93 = phi i32 [ %85, %82 ], [ %90, %87 ]
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %97
95: ; preds = %92
%96 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i64 0, i64 0))
br label %97
97: ; preds = %92, %95, %80, %48, %42, %23
%98 = phi i32 [ 1, %23 ], [ 1, %42 ], [ 1, %48 ], [ 1, %80 ], [ 1, %95 ], [ 0, %92 ]
ret i32 %98
}