#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 buf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ buf_append(struct buf *, char const);
int /*<<< orphan*/ buf_finish(struct buf *);
struct buf *buf_new();
struct buf *buf_new_from_str(const char *str) {
  struct buf *b;
  const char *p;
  b = buf_new();
  for (p = str; *p != '\0'; p++)
    buf_append(b, *p);
  buf_finish(b);
  return (b);
}


x86 assembly (clang, -O0):
<buf_new_from_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <buf_new@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1180 <buf_new_from_str+0x50>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <buf_append@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114f <buf_new_from_str+0x1f>
mov    -0x10(%rbp),%rdi
call   1030 <buf_finish@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_new_from_str>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <buf_new@plt>
mov    %rax,%r14
mov    (%rbx),%al
test   %al,%al
je     1166 <buf_new_from_str+0x36>
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
movsbl %al,%esi
mov    %r14,%rdi
call   1050 <buf_append@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1150 <buf_new_from_str+0x20>
mov    %r14,%rdi
call   1030 <buf_finish@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.buf = type { i32 }
define %struct.buf* @buf_new_from_str(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.buf*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call %struct.buf* (...) @buf_new()
store %struct.buf* %5, %struct.buf** %3
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %17, %1
%8 = load i8*, i8** %4
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %7
%13 = load %struct.buf*, %struct.buf** %3
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = call i32 @buf_append(%struct.buf* %13, i8 signext %15)
br label %17
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %4
br label %7
20: ; preds = %7
%21 = load %struct.buf*, %struct.buf** %3
%22 = call i32 @buf_finish(%struct.buf* %21)
%23 = load %struct.buf*, %struct.buf** %3
ret %struct.buf* %23
}

LLVM IR (-O2):
%struct.buf = type { i32 }
define %struct.buf* @buf_new_from_str(i8* nocapture %0) {
%2 = tail call %struct.buf* (...) @buf_new()
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %12, label %5
5: ; preds = %1, %5
%6 = phi i8 [ %10, %5 ], [ %3, %1 ]
%7 = phi i8* [ %9, %5 ], [ %0, %1 ]
%8 = tail call i32 @buf_append(%struct.buf* %2, i8 signext %6)
%9 = getelementptr inbounds i8, i8* %7, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %5
12: ; preds = %5, %1
%13 = tail call i32 @buf_finish(%struct.buf* %2)
ret %struct.buf* %2
}