#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__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  int size = 16;
  int length = 0;
  char *buf = NULL;
  if (argc != 3) {
    printf("usage : dynstat arg1 arg2\n");
    return -1;
  }
  printf("%s\n", argv[1]);
  buf = (char *)calloc(sizeof(char) * (size + 1), 1);
  if (buf == NULL) {
    printf("could not allocate space for buffer\n");
    return -1;
  }
  strncpy(buf, argv[2], size);
  buf[size] = '\0';
  if (strcmp(buf, argv[2]) != 0) {
    printf("arg2 length exceed the limit of %d", size);
    free(buf);
    return -1;
  }
  printf("buffer : %s\n", buf);
  strncpy(buf, "testdubuffer", size);
  length = strlen(buf);
  if (length != 0) {
    printf("buffer : %s\n", buf);
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x10,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b0 <main+0x50>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <main+0x165>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1207 <main+0xa7>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <main+0x165>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1268 <main+0x108>
mov    -0x14(%rbp),%esi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <main+0x165>
mov    -0x20(%rbp),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xde8(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12b5 <main+0x155>
mov    -0x20(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11de <main+0x6e>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1050 <puts@plt>
mov    $0x11,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     11e7 <main+0x77>
mov    %rax,%rbx
mov    0x10(%rbp),%rsi
mov    %rax,%rdi
mov    $0x10,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x10(%rbx)
mov    0x10(%rbp),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1201 <main+0x91>
lea    0xe38(%rip),%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11f3 <main+0x83>
lea    0xe7c(%rip),%rdi
jmp    11ee <main+0x7e>
lea    0xe4f(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1b(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe15(%rip),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
je     1245 <main+0xd5>
lea    0xde8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11f8 <main+0x88>

LLVM IR (-O0):
@.str = [27 x i8] c"usage : dynstat arg1 arg2\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [37 x i8] c"could not allocate space for buffer\0A\00"
@.str.3 = [35 x i8] c"arg2 length exceed the limit of %d\00"
@.str.4 = [13 x i8] c"buffer : %s\0A\00"
@.str.5 = [13 x i8] c"testdubuffer\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 16, i32* %6
store i32 0, i32* %7
store i8* null, i8** %8
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = mul i64 1, %20
%22 = trunc i64 %21 to i32
%23 = call i64 @calloc(i32 %22, i32 1)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %13
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
29: ; preds = %13
%30 = load i8*, i8** %8
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = load i32, i32* %6
%35 = call i32 @strncpy(i8* %30, i8* %33, i32 %34)
%36 = load i8*, i8** %8
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %8
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %40, i8* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %29
%47 = load i32, i32* %6
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = load i8*, i8** %8
%50 = call i32 @free(i8* %49)
store i32 -1, i32* %3
br label %67
51: ; preds = %29
%52 = load i8*, i8** %8
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %52)
%54 = load i8*, i8** %8
%55 = load i32, i32* %6
%56 = call i32 @strncpy(i8* %54, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %55)
%57 = load i8*, i8** %8
%58 = call i32 @strlen(i8* %57)
store i32 %58, i32* %7
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %51
%62 = load i8*, i8** %8
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %61, %51
%65 = load i8*, i8** %8
%66 = call i32 @free(i8* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %64, %46, %27, %11
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str.3 = [35 x i8] c"arg2 length exceed the limit of %d\00"
@.str.4 = [13 x i8] c"buffer : %s\0A\00"
@.str.5 = [13 x i8] c"testdubuffer\00"
@str = [36 x i8] c"could not allocate space for buffer\00"
@str.6 = [26 x i8] c"usage : dynstat arg1 arg2\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
br label %35
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @puts(i8* dereferenceable(1) %8)
%10 = tail call i64 @calloc(i32 17, i32 1)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %35
15: ; preds = %6
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @strncpy(i8* %11, i8* %17, i32 16)
%19 = getelementptr inbounds i8, i8* %11, i64 16
store i8 0, i8* %19
%20 = load i8*, i8** %16
%21 = tail call i64 @strcmp(i8* %11, i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %15
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 16)
%25 = tail call i32 @free(i8* %11)
br label %35
26: ; preds = %15
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%28 = tail call i32 @strncpy(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 16)
%29 = tail call i32 @strlen(i8* %11)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %11)
br label %33
33: ; preds = %31, %26
%34 = tail call i32 @free(i8* %11)
br label %35
35: ; preds = %33, %23, %13, %4
%36 = phi i32 [ -1, %4 ], [ -1, %13 ], [ -1, %23 ], [ 0, %33 ]
ret i32 %36
}