#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;
  }
  sck = socket(PF_UNIX, SOCK_DGRAM, 0);
  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    12d0 <main+0x150>
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    12d0 <main+0x150>
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    12d0 <main+0x150>
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)
movq   $0x8,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
lea    0xe00(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
cmp    $0x0,%rax
jle    12c9 <main+0x149>
lea    0xde7(%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   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jne    120a <main+0x8a>
lea    0xe6e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1226 <main+0xa6>
add    $0x1,%rax
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe25(%rip),%rsi
mov    %ebp,%edx
call   1090 <sprintf@plt>
mov    0x8(%rsp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
test   %rax,%rax
je     1240 <main+0xc0>
lea    0xe3a(%rip),%rdi
jmp    122d <main+0xad>
lea    0xe58(%rip),%rdi
call   1040 <puts@plt>
lea    0xe64(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    1237 <main+0xb7>
lea    0xe2c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
lea    0xdce(%rip),%rsi
lea    0x8(%rsp),%r8
mov    $0x8,%r9d
mov    %eax,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
test   %rax,%rax
jle    1237 <main+0xb7>
lea    0xdad(%rip),%rdi
call   1040 <puts@plt>
jmp    1237 <main+0xb7>

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 = [4 x i8] c"sig\00"
@.str.7 = [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 %53
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 %53
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 %53
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
store i64 8, i64* %9
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%47 = load i64, i64* %9
%48 = call i64 @sendto(i32 %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %46, i64 %47)
%49 = icmp sgt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %41
%51 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %52
52: ; preds = %50, %41
store i32 0, i32* %3
br label %53
53: ; preds = %52, %39, %21, %14
%54 = load i32, i32* %3
ret i32 %54
}

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.6 = [4 x i8] c"sig\00"
@str = [16 x i8] c"message sent ok\00"
@str.8 = [23 x i8] c"not in an xrdp session\00"
@str.9 = [16 x i8] c"display not set\00"
@str.10 = [24 x i8] c"xrdp disconnect utility\00"
@str.11 = [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.10, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.11, i64 0, i64 0))
br label %40
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.9, i64 0, i64 0))
br label %40
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.8, i64 0, i64 0))
br label %40
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 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%36 = call i64 @sendto(i32 %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %35, i64 8)
%37 = icmp sgt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %31, %38, %29, %14, %8
%41 = phi i32 [ 0, %8 ], [ 1, %14 ], [ 1, %29 ], [ 0, %38 ], [ 0, %31 ]
ret i32 %41
}