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

/* Variables and functions */
scalar_t__ rawmemchr(char *, int /*<<< orphan*/);
void count_args(char *argv[], int *argc, size_t *arglen) {
  int i = 0;
  while (argv[i])
    i++;
  *argc = i;
  if (!i) {
    *arglen = 0;
    return;
  }
  *arglen = (size_t)rawmemchr(argv[i - 1], 0) - (size_t)argv[0] + 1;
}


x86 assembly (clang, -O0):
<count_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     114c <count_args+0x3c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112b <count_args+0x1b>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jne    116f <count_args+0x5f>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
jmp    119f <count_args+0x8f>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
xor    %esi,%esi
call   1030 <rawmemchr@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
sub    %rax,%rcx
add    $0x1,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_args>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
movabs $0xfffffffe00000000,%rax
ff ff ff
xor    %ecx,%ecx
movabs $0x100000000,%rdx
add    %rdx,%rax
cmpq   $0x0,(%rbx,%rcx,8)
lea    0x1(%rcx),%rcx
jne    1130 <count_args+0x20>
lea    -0x1(%rcx),%edx
mov    %edx,(%rsi)
cmp    $0x1,%ecx
jne    114c <count_args+0x3c>
xor    %eax,%eax
jmp    1168 <count_args+0x58>
sar    $0x1d,%rax
mov    (%rbx,%rax,1),%rdi
xor    %esi,%esi
call   1030 <rawmemchr@plt>
mov    (%rbx),%rcx
neg    %rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define void @count_args(i8** nocapture %0, i32* nocapture writeonly %1, i64* nocapture writeonly %2) {
br label %4
4: ; preds = %4, %3
%5 = phi i64 [ %9, %4 ], [ 0, %3 ]
%6 = getelementptr inbounds i8*, i8** %0, i64 %5
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
%9 = add nuw i64 %5, 1
br i1 %8, label %10, label %4
10: ; preds = %4
%11 = trunc i64 %5 to i32
store i32 %11, i32* %1
%12 = icmp eq i32 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %10
%14 = shl i64 %5, 32
%15 = add i64 %14, -4294967296
%16 = ashr exact i64 %15, 32
%17 = getelementptr inbounds i8*, i8** %0, i64 %16
%18 = load i8*, i8** %17
%19 = tail call i64 @rawmemchr(i8* %18, i32 0)
%20 = load i8*, i8** %0
%21 = ptrtoint i8* %20 to i64
%22 = add i64 %19, 1
%23 = sub i64 %22, %21
br label %24
24: ; preds = %10, %13
%25 = phi i64 [ %23, %13 ], [ 0, %10 ]
store i64 %25, i64* %2
ret void
}