#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ close(int);
int /*<<< orphan*/ convert_mouseloc(char *, int *, int *);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int read(int, char *, int);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void mouseloc(int *x, int *y) {
  char buf[4096];
  int fds[2];
  int n_bytes;
  pid_t childpid;
  pipe(fds);
  ;
  if ((childpid = fork()) == -1) {
    perror("fork");
    *x = -1;
    *y = -1;
    return;
  }
  if (childpid == 0) {
    if (close(fds[0]) < 0) {
      perror("close");
    }
    if (dup2(fds[1], STDOUT_FILENO) < 0) {
      perror("dup2");
    }
    execlp("xdotool", "xdotool", "getmouselocation", NULL);
    exit(0);
  } else {
    if (close(fds[1]) < 0) {
      perror("parent close");
    };
    if (waitpid(childpid, NULL, 0) < 0) {
      perror("wait");
    };
    if ((n_bytes = read(fds[0], buf, 4096)) < 0) {
      perror("mouse read");
      *x = -1;
      *y = -1;
      return;
    };
    convert_mouseloc(buf, x, y);
  }
}


x86 assembly (clang, -O0):
<mouseloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1018(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1020(%rbp)
cmp    $0xffffffff,%eax
jne    11fa <mouseloc+0x5a>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    131a <mouseloc+0x17a>
cmpl   $0x0,-0x1020(%rbp)
jne    1273 <mouseloc+0xd3>
mov    -0x1018(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1228 <mouseloc+0x88>
lea    0xe07(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1014(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    1252 <mouseloc+0xb2>
lea    0xdb8(%rip),%rdi
call   1090 <perror@plt>
lea    0xdb1(%rip),%rsi
lea    0xdb2(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10b0 <execlp@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x1014(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1294 <mouseloc+0xf4>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1020(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0x0,%rax
jge    12bb <mouseloc+0x11b>
lea    0xd7a(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1018(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x101c(%rbp)
cmp    $0x0,%eax
jge    1306 <mouseloc+0x166>
lea    0xd4d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    131a <mouseloc+0x17a>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1070 <convert_mouseloc@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mouseloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1222 <mouseloc+0x82>
mov    %eax,%ebp
test   %eax,%eax
je     1262 <mouseloc+0xc2>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
test   %rax,%rax
js     122b <mouseloc+0x8b>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %rax,%rax
js     1239 <mouseloc+0x99>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1247 <mouseloc+0xa7>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <convert_mouseloc@plt>
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
jmp    124e <mouseloc+0xae>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
jmp    11df <mouseloc+0x3f>
lea    0xdf0(%rip),%rdi
call   1090 <perror@plt>
jmp    11ef <mouseloc+0x4f>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,(%rbx)
movl   $0xffffffff,(%r14)
jmp    1216 <mouseloc+0x76>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
test   %rax,%rax
jns    127c <mouseloc+0xdc>
lea    0xdb3(%rip),%rdi
call   1090 <perror@plt>
mov    0xc(%rsp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    129f <mouseloc+0xff>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
lea    0xd64(%rip),%rdi
lea    0xd65(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   10b0 <execlp@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [6 x i8] c"close\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [5 x i8] c"dup2\00"
@.str.3 = [8 x i8] c"xdotool\00"
@.str.4 = [17 x i8] c"getmouselocation\00"
@.str.5 = [13 x i8] c"parent close\00"
@.str.6 = [5 x i8] c"wait\00"
@.str.7 = [11 x i8] c"mouse read\00"
define void @mouseloc(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i32 (...) @fork()
store i32 %11, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = load i32*, i32** %3
store i32 -1, i32* %15
%16 = load i32*, i32** %4
store i32 -1, i32* %16
br label %66
17: ; preds = %2
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%22 = load i32, i32* %21
%23 = call i64 @close(i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%29 = load i32, i32* %28
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i64 @dup2(i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32* null)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %17
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i64 @close(i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %43, %38
%46 = load i32, i32* %8
%47 = call i64 @waitpid(i32 %46, i32* null, i32 0)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%53 = load i32, i32* %52
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 4096)
store i32 %55, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%59 = load i32*, i32** %3
store i32 -1, i32* %59
%60 = load i32*, i32** %4
store i32 -1, i32* %60
br label %66
61: ; preds = %51
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%63 = load i32*, i32** %3
%64 = load i32*, i32** %4
%65 = call i32 @convert_mouseloc(i8* %62, i32* %63, i32* %64)
br label %66
66: ; preds = %13, %57, %61
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [6 x i8] c"close\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [5 x i8] c"dup2\00"
@.str.3 = [8 x i8] c"xdotool\00"
@.str.4 = [17 x i8] c"getmouselocation\00"
@.str.5 = [13 x i8] c"parent close\00"
@.str.6 = [5 x i8] c"wait\00"
@.str.7 = [11 x i8] c"mouse read\00"
define void @mouseloc(i32* %0, i32* %1) {
%3 = alloca [4096 x i8]
%4 = alloca [2 x i32]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast [2 x i32]* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
switch i32 %9, label %29 [
i32 -1, label %10
i32 0, label %12
]
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %0
store i32 -1, i32* %1
br label %49
12: ; preds = %2
%13 = load i32, i32* %7
%14 = call i64 @close(i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i64 @dup2(i32 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32* null)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %2
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i64 @close(i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = call i64 @waitpid(i32 %9, i32* null, i32 0)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = load i32, i32* %7
%43 = call i32 @read(i32 %42, i8* %5, i32 4096)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %0
store i32 -1, i32* %1
br label %49
47: ; preds = %41
%48 = call i32 @convert_mouseloc(i8* %5, i32* %0, i32* %1)
br label %49
49: ; preds = %47, %45, %10
ret void
}