#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 my_putenv(char *, char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ pthread_self();
int rand();
int /*<<< orphan*/ sprintf(char *, char *, int);
void *thr_fun(void *arg) {
  int i;
  for (i = 0; i < 100; ++i) {
    int n = rand() % 20;
    char key[128] = {0};
    sprintf(key, "demo_%03d", n);
    char value[128] = {0};
    sprintf(value, "test_%02d", n);
    printf("[%lu] test key %s, value %s\n", pthread_self(), key, value);
    int ret = 0;
    ret = my_putenv(key, value);
    if (ret == 0)
      printf("[%lu] %s: %s\n", pthread_self(), key, value);
    else
      printf("[%lu] %s insert failed, ret %d\n", pthread_self(), key, ret);
  }
  return 0;
}


x86 assembly (clang, -O0):
<thr_fun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x64,-0xc(%rbp)
jge    129a <thr_fun+0x13a>
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe4a(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe2b(%rip),%rsi
call   1070 <sprintf@plt>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    -0x90(%rbp),%rdx
lea    -0x110(%rbp),%rcx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x114(%rbp)
lea    -0x90(%rbp),%rdi
lea    -0x110(%rbp),%rsi
call   1050 <my_putenv@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jne    1263 <thr_fun+0x103>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    -0x90(%rbp),%rdx
lea    -0x110(%rbp),%rcx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1287 <thr_fun+0x127>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    -0x90(%rbp),%rdx
mov    -0x114(%rbp),%ecx
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    128c <thr_fun+0x12c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1176 <thr_fun+0x16>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thr_fun>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    $0x64,%r14d
lea    0x80(%rsp),%rbx
lea    0xe94(%rip),%r15
mov    %rsp,%r12
lea    0xe94(%rip),%r13
jmp    11af <thr_fun+0x5f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xea8(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0xffffffff,%r14d
je     12b5 <thr_fun+0x165>
xor    %eax,%eax
call   1070 <rand@plt>
movslq %eax,%rbp
imul   $0x66666667,%rbp,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
shl    $0x2,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
mov    %rbx,%rdi
lea    0xdde(%rip),%rsi
mov    %ebp,%edx
call   1060 <sprintf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <sprintf@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
mov    %r13,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <my_putenv@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <pthread_self@plt>
test   %ebp,%ebp
jne    1190 <thr_fun+0x40>
lea    0xd95(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0xffffffff,%r14d
jne    11af <thr_fun+0x5f>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"demo_%03d\00"
@.str.1 = [10 x i8] c"test_%02d\00"
@.str.2 = [29 x i8] c"[%lu] test key %s, value %s\0A\00"
@.str.3 = [14 x i8] c"[%lu] %s: %s\0A\00"
@.str.4 = [32 x i8] c"[%lu] %s insert failed, ret %d\0A\00"
define i8* @thr_fun(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca [128 x i8]
%7 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %8
8: ; preds = %42, %1
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 100
br i1 %10, label %11, label %45
11: ; preds = %8
%12 = call i32 (...) @rand()
%13 = srem i32 %12, 20
store i32 %13, i32* %4
%14 = bitcast [128 x i8]* %5 to i8*
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = bitcast [128 x i8]* %6 to i8*
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%20 = load i32, i32* %4
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 (...) @pthread_self()
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%25 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %22, i8* %23, i8* %24)
store i32 0, i32* %7
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%28 = call i32 @my_putenv(i8* %26, i8* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %11
%32 = call i32 (...) @pthread_self()
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%35 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %32, i8* %33, i8* %34)
br label %41
36: ; preds = %11
%37 = call i32 (...) @pthread_self()
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%39 = load i32, i32* %7
%40 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %37, i8* %38, i32 %39)
br label %41
41: ; preds = %36, %31
br label %42
42: ; preds = %41
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %8
45: ; preds = %8
ret i8* null
}

LLVM IR (-O2):
@.str = [10 x i8] c"demo_%03d\00"
@.str.1 = [10 x i8] c"test_%02d\00"
@.str.2 = [29 x i8] c"[%lu] test key %s, value %s\0A\00"
@.str.3 = [14 x i8] c"[%lu] %s: %s\0A\00"
@.str.4 = [32 x i8] c"[%lu] %s insert failed, ret %d\0A\00"
define noalias i8* @thr_fun(i8* nocapture readnone %0) {
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
br label %6
6: ; preds = %1, %21
%7 = phi i32 [ 0, %1 ], [ %22, %21 ]
%8 = call i32 (...) @rand()
%9 = srem i32 %8, 20
%10 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%12 = call i32 (...) @pthread_self()
%13 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %12, i8* %4, i8* %5)
%14 = call i32 @my_putenv(i8* %4, i8* %5)
%15 = icmp eq i32 %14, 0
%16 = call i32 (...) @pthread_self()
br i1 %15, label %17, label %19
17: ; preds = %6
%18 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %16, i8* %4, i8* %5)
br label %21
19: ; preds = %6
%20 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %16, i8* %4, i32 %14)
br label %21
21: ; preds = %19, %17
%22 = add nuw nsw i32 %7, 1
%23 = icmp eq i32 %22, 100
br i1 %23, label %24, label %6
24: ; preds = %21
ret i8* null
}