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

/* Variables and functions */
int /*<<< orphan*/ fatalf(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int osx_get_window_id(const char *app_name) {
  char command[1024];
  sprintf(command,
          "osascript -so -e 'tell app \"%s\" to id of window 1' 2> /dev/null",
          app_name);
  FILE *fp = popen(command, "r");
  if (fp == NULL) {
    fatalf("Error: failed to run command: %s", command);
  }
  int window_id;
  if (fscanf(fp, "%d", &window_id) != 1) {
    fatalf("Error: failed to parse window id: %s", command);
  }
  pclose(fp);
  return window_id;
}


x86 assembly (clang, -O0):
<osx_get_window_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe8f(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xebc(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    11b1 <osx_get_window_id+0x61>
lea    -0x410(%rbp),%rsi
lea    0xe96(%rip),%rdi
call   1030 <fatalf@plt>
mov    -0x418(%rbp),%rdi
lea    0xea4(%rip),%rsi
lea    -0x41c(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11e7 <osx_get_window_id+0x97>
lea    -0x410(%rbp),%rsi
lea    0xe84(%rip),%rdi
call   1030 <fatalf@plt>
mov    -0x418(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x41c(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<osx_get_window_id>:
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%rdx
lea    0xe9e(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xeca(%rip),%rsi
mov    %rbx,%rdi
call   1060 <popen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1197 <osx_get_window_id+0x47>
lea    0xeb5(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1030 <fatalf@plt>
lea    0xec5(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11c1 <osx_get_window_id+0x71>
lea    0xeaf(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1030 <fatalf@plt>
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    0xc(%rsp),%eax
add    $0x410,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [64 x i8] c"osascript -so -e 'tell app \22%s\22 to id of window 1' 2> /dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"Error: failed to run command: %s\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [37 x i8] c"Error: failed to parse window id: %s\00"
define i32 @osx_get_window_id(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %2
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%15 = call i32 @fatalf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %1
%17 = load i32*, i32** %4
%18 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%22 = call i32 @fatalf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %20, %16
%24 = load i32*, i32** %4
%25 = call i32 @pclose(i32* %24)
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
@.str = [64 x i8] c"osascript -so -e 'tell app \22%s\22 to id of window 1' 2> /dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"Error: failed to run command: %s\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [37 x i8] c"Error: failed to parse window id: %s\00"
define i32 @osx_get_window_id(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @fatalf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %10
10: ; preds = %8, %1
%11 = bitcast i32* %3 to i8*
%12 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%13 = icmp eq i32 %12, 1
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = call i32 @fatalf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %16
16: ; preds = %14, %10
%17 = call i32 @pclose(i32* %6)
%18 = load i32, i32* %3
ret i32 %18
}