#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 */
typedef int pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int getpid();
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int bench_snprintf(int type, long count) {
  long l;
  pid_t pid;
  char buf[20 + 1];
  int nret;
  if (type == 1) {
    for (l = 0; l < count; l++) {
      pid = getpid();
      snprintf(buf, sizeof(buf), "%ld", pid);
    }
  } else if (type == 2) {
    for (l = 0; l < count; l++) {
      pid = getpid();
      if (pid != 123456) {
        memcpy(buf, "123456", 6);
        buf[6] = '\0';
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<bench_snprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    1197 <bench_snprintf+0x67>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    1192 <bench_snprintf+0x62>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x15,%esi
lea    0xe84(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1151 <bench_snprintf+0x21>
jmp    1207 <bench_snprintf+0xd7>
cmpl   $0x2,-0x4(%rbp)
jne    1202 <bench_snprintf+0xd2>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    11fd <bench_snprintf+0xcd>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1e240,-0x1c(%rbp)
je     11e7 <bench_snprintf+0xb7>
lea    -0x40(%rbp),%rdi
lea    0xe2b(%rip),%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
movb   $0x0,-0x3a(%rbp)
jmp    11ec <bench_snprintf+0xbc>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11a9 <bench_snprintf+0x79>
jmp    1202 <bench_snprintf+0xd2>
jmp    1207 <bench_snprintf+0xd7>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bench_snprintf>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
je     118b <bench_snprintf+0x5b>
cmp    $0x2,%edi
jne    11bf <bench_snprintf+0x8f>
test   %rbx,%rbx
jle    11bf <bench_snprintf+0x8f>
lea    0xeb2(%rip),%r14
mov    %rsp,%r15
jmp    1166 <bench_snprintf+0x36>
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbx
je     11bf <bench_snprintf+0x8f>
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    $0x1e240,%eax
je     1160 <bench_snprintf+0x30>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
movb   $0x0,0x6(%rsp)
jmp    1160 <bench_snprintf+0x30>
test   %rbx,%rbx
jle    11bf <bench_snprintf+0x8f>
lea    0xe69(%rip),%r14
mov    %rsp,%r15
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r15,%rdi
mov    $0x15,%esi
mov    %r14,%rdx
mov    %eax,%ecx
call   1040 <snprintf@plt>
add    $0xffffffffffffffff,%rbx
jne    11a0 <bench_snprintf+0x70>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%ld\00"
@.str.1 = [7 x i8] c"123456\00"
define i32 @bench_snprintf(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca [21 x i8]
%8 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %25
11: ; preds = %2
store i64 0, i64* %5
br label %12
12: ; preds = %21, %11
%13 = load i64, i64* %5
%14 = load i64, i64* %4
%15 = icmp slt i64 %13, %14
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = call i32 (...) @getpid()
store i32 %17, i32* %6
%18 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* %6
%20 = call i32 @snprintf(i8* %18, i32 21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %16
%22 = load i64, i64* %5
%23 = add nsw i64 %22, 1
store i64 %23, i64* %5
br label %12
24: ; preds = %12
br label %47
25: ; preds = %2
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %46
28: ; preds = %25
store i64 0, i64* %5
br label %29
29: ; preds = %42, %28
%30 = load i64, i64* %5
%31 = load i64, i64* %4
%32 = icmp slt i64 %30, %31
br i1 %32, label %33, label %45
33: ; preds = %29
%34 = call i32 (...) @getpid()
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 123456
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%39 = call i32 @memcpy(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%40 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 6
store i8 0, i8* %40
br label %41
41: ; preds = %37, %33
br label %42
42: ; preds = %41
%43 = load i64, i64* %5
%44 = add nsw i64 %43, 1
store i64 %44, i64* %5
br label %29
45: ; preds = %29
br label %46
46: ; preds = %45, %25
br label %47
47: ; preds = %46, %24
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
@.str.1 = [7 x i8] c"123456\00"
define i32 @bench_snprintf(i32 %0, i64 %1) {
%3 = alloca [21 x i8]
%4 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
switch i32 %0, label %26 [
i32 1, label %9
i32 2, label %5
]
5: ; preds = %2
%6 = icmp sgt i64 %1, 0
br i1 %6, label %7, label %26
7: ; preds = %5
%8 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 6
br label %17
9: ; preds = %2
%10 = icmp sgt i64 %1, 0
br i1 %10, label %11, label %26
11: ; preds = %9, %11
%12 = phi i64 [ %15, %11 ], [ 0, %9 ]
%13 = call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %4, i32 21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = add nuw nsw i64 %12, 1
%16 = icmp eq i64 %15, %1
br i1 %16, label %26, label %11
17: ; preds = %7, %23
%18 = phi i64 [ 0, %7 ], [ %24, %23 ]
%19 = call i32 (...) @getpid()
%20 = icmp eq i32 %19, 123456
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
store i8 0, i8* %8
br label %23
23: ; preds = %17, %21
%24 = add nuw nsw i64 %18, 1
%25 = icmp eq i64 %24, %1
br i1 %25, label %26, label %17
26: ; preds = %23, %11, %5, %9, %2
ret i32 0
}