#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 */
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strspn(char *, char *);
void trim(char **d, int n) {
  char *p;
  char *c;
  int lz;
  p = *d;
  lz = strspn(p, "0");
  if (lz == 0)
    return;
  c = calloc(n - lz + 1, sizeof(char));
  memcpy(c, p + lz, n - lz + 1);
  free(p);
  *d = c;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1040 <strspn@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    117c <trim+0x3c>
jmp    11c4 <trim+0x84>
mov    -0xc(%rbp),%edi
sub    -0x24(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x24(%rbp),%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    (%rdi),%r15
lea    0xea9(%rip),%rsi
mov    %r15,%rdi
call   1040 <strspn@plt>
test   %eax,%eax
je     1194 <trim+0x54>
mov    %eax,%ebx
sub    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
movslq %ebx,%rsi
add    %r15,%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,(%r14)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
define void @trim(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%8 = load i8**, i8*** %3
%9 = load i8*, i8** %8
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @strspn(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
br label %35
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* %7
%18 = sub nsw i32 %16, %17
%19 = add nsw i32 %18, 1
%20 = call i8* @calloc(i32 %19, i32 1)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = load i8*, i8** %5
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i32, i32* %4
%27 = load i32, i32* %7
%28 = sub nsw i32 %26, %27
%29 = add nsw i32 %28, 1
%30 = call i32 @memcpy(i8* %21, i8* %25, i32 %29)
%31 = load i8*, i8** %5
%32 = call i32 @free(i8* %31)
%33 = load i8*, i8** %6
%34 = load i8**, i8*** %3
store i8* %33, i8** %34
br label %35
35: ; preds = %15, %14
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
define void @trim(i8** nocapture %0, i32 %1) {
%3 = load i8*, i8** %0
%4 = tail call i32 @strspn(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = sub nsw i32 %1, %4
%8 = add nsw i32 %7, 1
%9 = tail call i8* @calloc(i32 %8, i32 1)
%10 = sext i32 %4 to i64
%11 = getelementptr inbounds i8, i8* %3, i64 %10
%12 = tail call i32 @memcpy(i8* %9, i8* %11, i32 %8)
%13 = tail call i32 @free(i8* %3)
store i8* %9, i8** %0
br label %14
14: ; preds = %2, %6
ret void
}