#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 node {
  int dummy;
};
struct list {
  int dummy;
};

/* Variables and functions */
struct node *list_first(struct list *);
struct node *node_next(struct node *);
struct node *list_find(struct list *list, int num) {
  struct node *n = list_first(list);
  while (n) {
    if (!num)
      return n;
    num--;
    n = node_next(n);
  }
  return n;
}


x86 assembly (clang, -O0):
<list_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <list_first@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1179 <list_find+0x59>
cmpl   $0x0,-0x14(%rbp)
jne    115e <list_find+0x3e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1181 <list_find+0x61>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <node_next@plt>
mov    %rax,-0x20(%rbp)
jmp    113c <list_find+0x1c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_find>:
push   %rbx
mov    %esi,%ebx
call   1030 <list_first@plt>
test   %rax,%rax
je     1157 <list_find+0x37>
test   %ebx,%ebx
je     1157 <list_find+0x37>
add    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%rdi
call   1040 <node_next@plt>
sub    $0x1,%ebx
setb   %cl
test   %rax,%rax
je     1157 <list_find+0x37>
test   %cl,%cl
je     1140 <list_find+0x20>
pop    %rbx
ret

LLVM IR (-O0):
%struct.node = type { i32 }
%struct.list = type { i32 }
define %struct.node* @list_find(%struct.list* %0, i32 %1) {
%3 = alloca %struct.node*
%4 = alloca %struct.list*
%5 = alloca i32
%6 = alloca %struct.node*
store %struct.list* %0, %struct.list** %4
store i32 %1, i32* %5
%7 = load %struct.list*, %struct.list** %4
%8 = call %struct.node* @list_first(%struct.list* %7)
store %struct.node* %8, %struct.node** %6
br label %9
9: ; preds = %17, %2
%10 = load %struct.node*, %struct.node** %6
%11 = icmp ne %struct.node* %10, null
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = load %struct.node*, %struct.node** %6
store %struct.node* %16, %struct.node** %3
br label %24
17: ; preds = %12
%18 = load i32, i32* %5
%19 = add nsw i32 %18, -1
store i32 %19, i32* %5
%20 = load %struct.node*, %struct.node** %6
%21 = call %struct.node* @node_next(%struct.node* %20)
store %struct.node* %21, %struct.node** %6
br label %9
22: ; preds = %9
%23 = load %struct.node*, %struct.node** %6
store %struct.node* %23, %struct.node** %3
br label %24
24: ; preds = %22, %15
%25 = load %struct.node*, %struct.node** %3
ret %struct.node* %25
}

LLVM IR (-O2):
%struct.node = type { i32 }
%struct.list = type { i32 }
define %struct.node* @list_find(%struct.list* %0, i32 %1) {
%3 = tail call %struct.node* @list_first(%struct.list* %0)
%4 = icmp eq %struct.node* %3, null
%5 = icmp eq i32 %1, 0
%6 = or i1 %4, %5
br i1 %6, label %15, label %7
7: ; preds = %2, %7
%8 = phi %struct.node* [ %11, %7 ], [ %3, %2 ]
%9 = phi i32 [ %10, %7 ], [ %1, %2 ]
%10 = add nsw i32 %9, -1
%11 = tail call %struct.node* @node_next(%struct.node* %8)
%12 = icmp eq %struct.node* %11, null
%13 = icmp eq i32 %10, 0
%14 = select i1 %12, i1 true, i1 %13
br i1 %14, label %15, label %7
15: ; preds = %7, %2
%16 = phi %struct.node* [ %3, %2 ], [ %11, %7 ]
ret %struct.node* %16
}