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

/* Variables and functions */
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ mvwinnstr(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                             char *, int);
scalar_t__ strlen(char *);
int gap_before_cursor(WINDOW *window, int width, int y, int x) {
  char *str = (char *)calloc((size_t)width, sizeof(char));
  mvwinnstr(window, y, 0, str, x - 1);
  int i = (int)(strlen(str) - 1), distance = 1;
  while (i) {
    if (str[i] != ' ') {
      break;
    }
    distance++;
    i--;
  }
  return distance;
}


x86 assembly (clang, -O0):
<gap_before_cursor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movslq -0xc(%rbp),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    -0x14(%rbp),%r8d
sub    $0x1,%r8d
xor    %edx,%edx
call   1040 <mvwinnstr@plt>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%rax
mov    %eax,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c3 <gap_before_cursor+0x93>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
je     11ac <gap_before_cursor+0x7c>
jmp    11c3 <gap_before_cursor+0x93>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1188 <gap_before_cursor+0x58>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gap_before_cursor>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rdi,%r12
movslq %esi,%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
add    $0xffffffff,%ebp
mov    %r12,%rdi
mov    %r15d,%esi
xor    %edx,%edx
mov    %rax,%rcx
mov    %ebp,%r8d
call   1040 <mvwinnstr@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
je     1194 <gap_before_cursor+0x64>
mov    $0x1,%ebx
nop
movslq %ecx,%rdx
cmpb   $0x20,(%r14,%rdx,1)
jne    1194 <gap_before_cursor+0x64>
add    $0x1,%ebx
add    $0xffffffff,%ecx
jne    1180 <gap_before_cursor+0x50>
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gap_before_cursor(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = call i64 @calloc(i64 %13, i32 1)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %9
%16 = load i32*, i32** %5
%17 = load i32, i32* %7
%18 = load i8*, i8** %9
%19 = load i32, i32* %8
%20 = sub nsw i32 %19, 1
%21 = call i32 @mvwinnstr(i32* %16, i32 %17, i32 0, i8* %18, i32 %20)
%22 = load i8*, i8** %9
%23 = call i64 @strlen(i8* %22)
%24 = sub nsw i64 %23, 1
%25 = trunc i64 %24 to i32
store i32 %25, i32* %10
store i32 1, i32* %11
br label %26
26: ; preds = %38, %4
%27 = load i32, i32* %10
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = load i8*, i8** %9
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 32
br i1 %36, label %37, label %38
37: ; preds = %29
br label %43
38: ; preds = %29
%39 = load i32, i32* %11
%40 = add nsw i32 %39, 1
store i32 %40, i32* %11
%41 = load i32, i32* %10
%42 = add nsw i32 %41, -1
store i32 %42, i32* %10
br label %26
43: ; preds = %37, %26
%44 = load i32, i32* %11
ret i32 %44
}

LLVM IR (-O2):
define i32 @gap_before_cursor(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = sext i32 %1 to i64
%6 = tail call i64 @calloc(i64 %5, i32 1)
%7 = inttoptr i64 %6 to i8*
%8 = add nsw i32 %3, -1
%9 = tail call i32 @mvwinnstr(i32* %0, i32 %2, i32 0, i8* %7, i32 %8)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %7)
%11 = trunc i64 %10 to i32
%12 = add i32 %11, -1
%13 = icmp eq i32 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %4, %21
%15 = phi i32 [ %23, %21 ], [ %12, %4 ]
%16 = phi i32 [ %22, %21 ], [ 1, %4 ]
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 32
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = add nuw nsw i32 %16, 1
%23 = add i32 %15, -1
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %14
25: ; preds = %21, %14, %4
%26 = phi i32 [ 1, %4 ], [ %16, %14 ], [ %11, %21 ]
ret i32 %26
}