#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 */
int /*<<< orphan*/ asprintf(char **, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
char *strtok(char *, char *);
int main(void) {
  char buf[] = "hello world ni hao";
  char *s = NULL;
  char *m = NULL;
  char buff[128] = {0};
  strncpy(buff, buf, strlen(buf));
  asprintf(&m, "%s", buf);
  s = m;
  printf("1:s: %s\n", m);
  for (s = strtok(s, " "); s; s = strtok(NULL, " ")) {
    printf("s:%s\n", s);
  }
  printf("2:s: %s\n", m);
  free(m);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe74(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe71(%rip),%ax
mov    %ax,-0x10(%rbp)
mov    0xe69(%rip),%al
mov    %al,-0xe(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xc0(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
lea    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <asprintf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xde9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127a <main+0x10a>
mov    -0x28(%rbp),%rsi
lea    0xdc5(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb3(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1246 <main+0xd6>
mov    -0x30(%rbp),%rsi
lea    0xda2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xb8,%rsp
movaps 0xe8f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x6f6168,0x1f(%rsp)
movq   $0x0,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x30(%rsp),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
lea    0xe35(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbx,%rdx
call   1070 <asprintf@plt>
mov    0x8(%rsp),%rbx
lea    0xe1f(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strtok@plt>
test   %rax,%rax
je     124a <main+0xea>
lea    0xe07(%rip),%r14
lea    0xdfe(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
jne    1230 <main+0xd0>
mov    0x8(%rsp),%rsi
lea    0xdd1(%rip),%rdi
call   1060 <printf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.buf = [19 x i8] c"hello world ni hao\00"
@.str = [3 x i8] c"%s\00"
@.str.1 = [9 x i8] c"1:s: %s\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [6 x i8] c"s:%s\0A\00"
@.str.4 = [9 x i8] c"2:s: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [19 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [128 x i8]
store i32 0, i32* %1
%6 = bitcast [19 x i8]* %2 to i8*
store i8* null, i8** %3
store i8* null, i8** %4
%7 = bitcast [128 x i8]* %5 to i8*
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
%12 = call i32 @strncpy(i8* %8, i8* %9, i32 %11)
%13 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%14 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %4
store i8* %15, i8** %3
%16 = load i8*, i8** %4
%17 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %3
%19 = call i8* @strtok(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %19, i8** %3
br label %20
20: ; preds = %26, %0
%21 = load i8*, i8** %3
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23
%27 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %27, i8** %3
br label %20
28: ; preds = %20
%29 = load i8*, i8** %4
%30 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [19 x i8] c"hello world ni hao\00"
@.str = [3 x i8] c"%s\00"
@.str.1 = [9 x i8] c"1:s: %s\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [6 x i8] c"s:%s\0A\00"
@.str.4 = [9 x i8] c"2:s: %s\0A\00"
define i32 @main() {
%1 = alloca [19 x i8]
%2 = alloca i8*
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [19 x i8], [19 x i8]* %1, i64 0, i64 0
%5 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = call i32 @strlen(i8* %4)
%8 = call i32 @strncpy(i8* %6, i8* %4, i32 %7)
%9 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%10 = load i8*, i8** %2
%11 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i8* @strtok(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %0, %14
%15 = phi i8* [ %17, %14 ], [ %12, %0 ]
%16 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%17 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %14
19: ; preds = %14, %0
%20 = load i8*, i8** %2
%21 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %2
%23 = call i32 @free(i8* %22)
ret i32 0
}