#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__ charWidth(int /*<<< orphan*/);
int stringWidth(char *string) {
  int width = 0;
  if (!string)
    string = "null";
  while (*string) {
    width += charWidth(*string++);
  }
  return width - 1;
}


x86 assembly (clang, -O0):
<stringWidth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1139 <stringWidth+0x29>
lea    0xecb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    113e <stringWidth+0x2e>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1174 <stringWidth+0x64>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%edi
call   1030 <charWidth@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
jmp    113e <stringWidth+0x2e>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringWidth>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
lea    0xee3(%rip),%rbx
cmovne %rdi,%rbx
mov    (%rbx),%al
test   %al,%al
je     114a <stringWidth+0x3a>
add    $0x1,%rbx
xor    %ebp,%ebp
nopl   (%rax)
movsbl %al,%edi
call   1030 <charWidth@plt>
add    %eax,%ebp
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1130 <stringWidth+0x20>
add    $0xffffffff,%ebp
jmp    114f <stringWidth+0x3f>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"null\00"
define i32 @stringWidth(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %7, label %6
6: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %2
br label %7
7: ; preds = %6, %1
br label %8
8: ; preds = %12, %7
%9 = load i8*, i8** %2
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %2
%15 = load i8, i8* %13
%16 = sext i8 %15 to i32
%17 = call i64 @charWidth(i32 %16)
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = add nsw i64 %19, %17
%21 = trunc i64 %20 to i32
store i32 %21, i32* %3
br label %8
22: ; preds = %8
%23 = load i32, i32* %3
%24 = sub nsw i32 %23, 1
ret i32 %24
}

LLVM IR (-O2):
@.str = [5 x i8] c"null\00"
define i32 @stringWidth(i8* %0) {
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %1, %6
%7 = phi i8 [ %15, %6 ], [ %4, %1 ]
%8 = phi i32 [ %14, %6 ], [ 0, %1 ]
%9 = phi i8* [ %10, %6 ], [ %3, %1 ]
%10 = getelementptr inbounds i8, i8* %9, i64 1
%11 = sext i8 %7 to i32
%12 = tail call i64 @charWidth(i32 %11)
%13 = trunc i64 %12 to i32
%14 = add i32 %8, %13
%15 = load i8, i8* %10
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %6
17: ; preds = %6
%18 = add i32 %14, -1
br label %19
19: ; preds = %17, %1
%20 = phi i32 [ -1, %1 ], [ %18, %17 ]
ret i32 %20
}