#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 session {
  int dummy;
};
struct listnode {
  int dummy;
};

/* Variables and functions */
int get_list_length(struct listnode *);
scalar_t__ get_number(struct session *, char *);
int /*<<< orphan*/ pop_call();
int /*<<< orphan*/ push_call(char *, struct session *, struct listnode *,
                             char *);
int get_list_index(struct session *ses, struct listnode *node, char *arg) {
  int index, size;
  push_call("get_list_index(%p,%p,%p)", ses, node, arg);
  index = (int)get_number(ses, arg);
  size = get_list_length(node);
  if (size == 0 && (index == -1 || index == 1)) {
    pop_call();
    return 0;
  }
  if (index < 0) {
    index = size + 1 + index;
  }
  if (size == 0 || index < 1 || index > size) {
    pop_call();
    return -1;
  }
  pop_call();
  return index;
}


x86 assembly (clang, -O0):
<get_list_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe99(%rip),%rdi
call   1040 <push_call@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <get_number@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <get_list_length@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11b9 <get_list_index+0x79>
cmpl   $0xffffffff,-0x24(%rbp)
je     11a6 <get_list_index+0x66>
cmpl   $0x1,-0x24(%rbp)
jne    11b9 <get_list_index+0x79>
mov    $0x0,%al
call   1060 <pop_call@plt>
movl   $0x0,-0x4(%rbp)
jmp    120f <get_list_index+0xcf>
cmpl   $0x0,-0x24(%rbp)
jge    11cf <get_list_index+0x8f>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11ef <get_list_index+0xaf>
cmpl   $0x1,-0x24(%rbp)
jl     11ef <get_list_index+0xaf>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    1202 <get_list_index+0xc2>
mov    $0x0,%al
call   1060 <pop_call@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <get_list_index+0xcf>
mov    $0x0,%al
call   1060 <pop_call@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_list_index>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xea9(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <push_call@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <get_number@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1050 <get_list_length@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11b0 <get_list_index+0x70>
lea    0x1(%rbx),%eax
mov    %r14d,%ebp
sar    $0x1f,%ebp
and    %eax,%ebp
add    %r14d,%ebp
xor    %eax,%eax
call   1060 <pop_call@plt>
cmp    %ebx,%ebp
mov    $0xffffffff,%eax
mov    %ebp,%ecx
cmovg  %eax,%ecx
test   %ebp,%ebp
cmovle %eax,%ecx
cmp    $0x1,%ebx
sbb    %ebx,%ebx
or     %ecx,%ebx
jmp    11c5 <get_list_index+0x85>
cmp    $0x1,%r14d
je     11bc <get_list_index+0x7c>
cmp    $0xffffffff,%r14d
jne    1181 <get_list_index+0x41>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <pop_call@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.session = type { i32 }
%struct.listnode = type { i32 }
@.str = [25 x i8] c"get_list_index(%p,%p,%p)\00"
define i32 @get_list_index(%struct.session* %0, %struct.listnode* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.session*
%6 = alloca %struct.listnode*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store %struct.session* %0, %struct.session** %5
store %struct.listnode* %1, %struct.listnode** %6
store i8* %2, i8** %7
%10 = load %struct.session*, %struct.session** %5
%11 = load %struct.listnode*, %struct.listnode** %6
%12 = load i8*, i8** %7
%13 = call i32 @push_call(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %struct.session* %10, %struct.listnode* %11, i8* %12)
%14 = load %struct.session*, %struct.session** %5
%15 = load i8*, i8** %7
%16 = call i64 @get_number(%struct.session* %14, i8* %15)
%17 = trunc i64 %16 to i32
store i32 %17, i32* %8
%18 = load %struct.listnode*, %struct.listnode** %6
%19 = call i32 @get_list_length(%struct.listnode* %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %3
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %25, %22
%29 = call i32 (...) @pop_call()
store i32 0, i32* %4
br label %53
30: ; preds = %25, %3
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
%36 = load i32, i32* %8
%37 = add nsw i32 %35, %36
store i32 %37, i32* %8
br label %38
38: ; preds = %33, %30
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, 0
br i1 %40, label %48, label %41
41: ; preds = %38
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 1
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = load i32, i32* %8
%46 = load i32, i32* %9
%47 = icmp sgt i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44, %41, %38
%49 = call i32 (...) @pop_call()
store i32 -1, i32* %4
br label %53
50: ; preds = %44
%51 = call i32 (...) @pop_call()
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %53
53: ; preds = %50, %48, %28
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
%struct.session = type { i32 }
%struct.listnode = type { i32 }
@.str = [25 x i8] c"get_list_index(%p,%p,%p)\00"
define i32 @get_list_index(%struct.session* %0, %struct.listnode* %1, i8* %2) {
%4 = tail call i32 @push_call(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %struct.session* %0, %struct.listnode* %1, i8* %2)
%5 = tail call i64 @get_number(%struct.session* %0, i8* %2)
%6 = trunc i64 %5 to i32
%7 = tail call i32 @get_list_length(%struct.listnode* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
switch i32 %6, label %12 [
i32 -1, label %10
i32 1, label %10
]
10: ; preds = %9, %9
%11 = tail call i32 (...) @pop_call()
br label %23
12: ; preds = %9, %3
%13 = icmp slt i32 %6, 0
%14 = add nsw i32 %7, 1
%15 = select i1 %13, i32 %14, i32 0
%16 = add nsw i32 %15, %6
%17 = icmp slt i32 %16, 1
%18 = select i1 %8, i1 true, i1 %17
%19 = icmp sgt i32 %16, %7
%20 = select i1 %18, i1 true, i1 %19
%21 = tail call i32 (...) @pop_call()
%22 = select i1 %20, i32 -1, i32 %16
br label %23
23: ; preds = %12, %10
%24 = phi i32 [ 0, %10 ], [ %22, %12 ]
ret i32 %24
}