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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ access(int /*<<< orphan*/, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                  size_t);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(int /*<<< orphan*/, char *, int);
int strtol(char *, char **, int);
int main(int argc, char **argv) {
  int sck;
  int dis;
  struct sockaddr_un sa;
  size_t len;
  char *p;
  char *display;
  if (argc != 1) {
    printf("xrdp disconnect utility\n");
    printf("run with no parameters to disconnect you xrdp session\n");
    return 0;
  }
  display = getenv("DISPLAY");
  if (display == 0) {
    printf("display not set\n");
    return 1;
  }
  dis = strtol(display + 1, &p, 10);
  memset(&sa, 0, sizeof(sa));
  sa.sun_family = AF_UNIX;
  sprintf(sa.sun_path, "/tmp/.xrdp/xrdp_disconnect_display_%d", dis);
  if (access(sa.sun_path, F_OK) != 0) {
    printf("not in an xrdp session\n");
    return 1;
  }
  if ((sck = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
    printf("socket open error\n");
    return 1;
  }
  len = sizeof(sa);
  if (sendto(sck, "sig", 4, 0, (struct sockaddr *)&sa, len) > 0) {
    printf("message sent ok\n");
  }
  return 0;
}


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)
cmpl   $0x1,-0x8(%rbp)
je     11c4 <main+0x44>
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f1 <main+0x171>
lea    0xe85(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f7 <main+0x77>
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f1 <main+0x171>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%edx
lea    0xe30(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     1271 <main+0xf1>
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f1 <main+0x171>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12ae <main+0x12e>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f1 <main+0x171>
movq   $0x8,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
lea    0xdf2(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
cmp    $0x0,%rax
jle    12ea <main+0x16a>
lea    0xdd9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jne    1203 <main+0x83>
lea    0xe6f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     121f <main+0x9f>
add    $0x1,%rax
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0xe2b(%rip),%rsi
mov    %ebx,%edx
call   1090 <sprintf@plt>
mov    (%rsp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
test   %rax,%rax
je     1228 <main+0xa8>
lea    0xe53(%rip),%rdi
jmp    127f <main+0xff>
lea    0xe71(%rip),%rdi
call   1040 <puts@plt>
lea    0xe7d(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    1289 <main+0x109>
lea    0xe45(%rip),%rdi
jmp    127f <main+0xff>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1278 <main+0xf8>
lea    0xde0(%rip),%rsi
mov    %rsp,%r8
mov    $0x8,%r9d
mov    %eax,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
test   %rax,%rax
jle    1289 <main+0x109>
lea    0xdc1(%rip),%rdi
call   1040 <puts@plt>
jmp    1289 <main+0x109>
lea    0xdc3(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"xrdp disconnect utility\0A\00"
@.str.1 = [55 x i8] c"run with no parameters to disconnect you xrdp session\0A\00"
@.str.2 = [8 x i8] c"DISPLAY\00"
@.str.3 = [17 x i8] c"display not set\0A\00"
@AF_UNIX = global i32 0
@.str.4 = [38 x i8] c"/tmp/.xrdp/xrdp_disconnect_display_%d\00"
@F_OK = global i32 0
@.str.5 = [24 x i8] c"not in an xrdp session\0A\00"
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.6 = [19 x i8] c"socket open error\0A\00"
@.str.7 = [4 x i8] c"sig\00"
@.str.8 = [17 x i8] c"message sent ok\0A\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 %struct.sockaddr_un
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %57
17: ; preds = %2
%18 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* %18, i8** %11
%19 = load i8*, i8** %11
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %57
23: ; preds = %17
%24 = load i8*, i8** %11
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = call i32 @strtol(i8* %25, i8** %10, i32 10)
store i32 %26, i32* %7
%27 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 8)
%28 = load i32, i32* @AF_UNIX
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* %7
%33 = call i32 @sprintf(i32 %31, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @F_OK
%37 = call i64 @access(i32 %35, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %23
%40 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %57
41: ; preds = %23
%42 = load i32, i32* @PF_UNIX
%43 = load i32, i32* @SOCK_DGRAM
%44 = call i32 @socket(i32 %42, i32 %43, i32 0)
store i32 %44, i32* %6
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %57
48: ; preds = %41
store i64 8, i64* %9
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%51 = load i64, i64* %9
%52 = call i64 @sendto(i32 %49, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %50, i64 %51)
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %56
56: ; preds = %54, %48
store i32 0, i32* %3
br label %57
57: ; preds = %56, %46, %39, %21, %14
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str.2 = [8 x i8] c"DISPLAY\00"
@AF_UNIX = global i32 0
@.str.4 = [38 x i8] c"/tmp/.xrdp/xrdp_disconnect_display_%d\00"
@F_OK = global i32 0
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.7 = [4 x i8] c"sig\00"
@str = [16 x i8] c"message sent ok\00"
@str.9 = [18 x i8] c"socket open error\00"
@str.10 = [23 x i8] c"not in an xrdp session\00"
@str.11 = [16 x i8] c"display not set\00"
@str.12 = [24 x i8] c"xrdp disconnect utility\00"
@str.13 = [54 x i8] c"run with no parameters to disconnect you xrdp session\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca i8*
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = icmp eq i32 %0, 1
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.13, i64 0, i64 0))
br label %44
11: ; preds = %2
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.11, i64 0, i64 0))
br label %44
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %12, i64 1
%18 = call i32 @strtol(i8* %17, i8** %4, i32 10)
%19 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @sprintf(i32 %23, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%25 = load i32, i32* %22
%26 = load i32, i32* @F_OK
%27 = call i64 @access(i32 %25, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %16
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
br label %44
31: ; preds = %16
%32 = load i32, i32* @PF_UNIX
%33 = load i32, i32* @SOCK_DGRAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
br label %44
38: ; preds = %31
%39 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%40 = call i64 @sendto(i32 %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %39, i64 8)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %44
44: ; preds = %38, %42, %36, %29, %14, %8
%45 = phi i32 [ 0, %8 ], [ 1, %14 ], [ 1, %29 ], [ 1, %36 ], [ 0, %42 ], [ 0, %38 ]
ret i32 %45
}