#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 timeval {
  unsigned int tv_sec;
  unsigned int tv_usec;
};

/* Variables and functions */
unsigned int atoi(char const *);
char *getenv(char *);
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int snprintf(char *, int, char *, unsigned int, unsigned int, int,
             unsigned int);
char *_genuniqid(void) {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  unsigned int sec = tv.tv_sec;
  unsigned int usec = tv.tv_usec;
  unsigned int port = 0;
  const char *remote_port = getenv("REMOTE_PORT");
  if (remote_port != NULL) {
    port = atoi(remote_port);
  }
  char *uniqid = (char *)malloc(5 + 5 + 4 + 4 + 1);
  if (snprintf(uniqid, 5 + 5 + 4 + 4 + 1, "%05x%05x%04x%04x", usec % 0x100000,
               sec % 0x100000, getpid() % 0x10000,
               port % 0x10000) >= 5 + 5 + 4 + 4 + 1)
    uniqid[5 + 5 + 4 + 4] = '\0';
  ;
  return uniqid;
}


x86 assembly (clang, -O0):
<_genuniqid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe71(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11af <_genuniqid+0x4f>
mov    -0x20(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0x13,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%eax
and    $0xfffff,%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
and    $0xfffff,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    $0x10000,%esi
cltd
idiv   %esi
mov    %edx,%r9d
mov    -0x14(%rbp),%eax
and    $0xffff,%eax
mov    $0x13,%esi
lea    0xe00(%rip),%rdx
mov    %eax,(%rsp)
call   1050 <snprintf@plt>
cmp    $0x13,%eax
jl     1225 <_genuniqid+0xc5>
mov    -0x28(%rbp),%rax
movb   $0x0,0x12(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_genuniqid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %r15d,%r15d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x10(%rsp),%r14d
mov    0x14(%rsp),%ebp
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119f <_genuniqid+0x3f>
mov    %rax,%rdi
call   1080 <atoi@plt>
movzwl %ax,%r15d
mov    $0x13,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
and    $0xfffff,%ebp
and    $0xfffff,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffff0000,%ecx
sub    %ecx,%eax
mov    %r15d,(%rsp)
lea    0xe2e(%rip),%rdx
mov    %rbx,%rdi
mov    $0x13,%esi
mov    %ebp,%ecx
mov    %r14d,%r8d
mov    %eax,%r9d
call   1050 <snprintf@plt>
cmp    $0x13,%eax
jl     11fc <_genuniqid+0x9c>
movb   $0x0,0x12(%rbx)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [12 x i8] c"REMOTE_PORT\00"
@.str.1 = [17 x i8] c"%05x%05x%04x%04x\00"
define i8* @_genuniqid() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%9 = load i32, i32* %8
store i32 %9, i32* %2
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%11 = load i32, i32* %10
store i32 %11, i32* %3
store i32 0, i32* %4
%12 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i8*, i8** %5
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %4
br label %18
18: ; preds = %15, %0
%19 = call i64 @malloc(i32 19)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = load i32, i32* %3
%23 = urem i32 %22, 1048576
%24 = load i32, i32* %2
%25 = urem i32 %24, 1048576
%26 = call i32 (...) @getpid()
%27 = srem i32 %26, 65536
%28 = load i32, i32* %4
%29 = urem i32 %28, 65536
%30 = call i32 @snprintf(i8* %21, i32 19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %25, i32 %27, i32 %29)
%31 = icmp sge i32 %30, 19
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 18
store i8 0, i8* %34
br label %35
35: ; preds = %32, %18
%36 = load i8*, i8** %6
ret i8* %36
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [12 x i8] c"REMOTE_PORT\00"
@.str.1 = [17 x i8] c"%05x%05x%04x%04x\00"
define i8* @_genuniqid() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%7 = load i32, i32* %6
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = tail call i32 @atoi(i8* %8)
%12 = and i32 %11, 65535
br label %13
13: ; preds = %10, %0
%14 = phi i32 [ %12, %10 ], [ 0, %0 ]
%15 = tail call i64 @malloc(i32 19)
%16 = inttoptr i64 %15 to i8*
%17 = and i32 %7, 1048575
%18 = and i32 %5, 1048575
%19 = tail call i32 (...) @getpid()
%20 = srem i32 %19, 65536
%21 = tail call i32 @snprintf(i8* %16, i32 19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18, i32 %20, i32 %14)
%22 = icmp sgt i32 %21, 18
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = getelementptr inbounds i8, i8* %16, i64 18
store i8 0, i8* %24
br label %25
25: ; preds = %23, %13
ret i8* %16
}