#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 {
  int tv_sec;
  int tv_usec;
};
typedef int ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
scalar_t__ errno;
char *getenv(char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int const, char *, long long, long long, char const *,
             long long, char const *);
int write(int, char *, int);
int write_index(int index_fd, off_t data_offset, off_t data_len,
                const char *sha1_hex) {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  const int buf_size = 256;
  char buf[buf_size];
  const char *remote_addr = getenv("HTTP_X_FORWARDED_FOR");
  if (remote_addr == NULL) {
    remote_addr = getenv("REMOTE_ADDR");
  }
  if (remote_addr == NULL) {
    remote_addr = "";
  }
  int rowlen = snprintf(
      buf, buf_size, "%lld %lld {\"ip\":\"%s\",\"t\":%lld,\"sha1\":\"%s\"}\n",
      (long long)data_offset, (long long)data_len, remote_addr,
      (long long)(tv.tv_sec * 1000 + tv.tv_usec / 1000), sha1_hex);
  errno = 0;
  ssize_t amt_written = write(index_fd, buf, rowlen);
  int ok = (amt_written == rowlen);
  if (!ok) {
    perror("Failed to write index");
  }
  return ok;
}


x86 assembly (clang, -O0):
<write_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movl   $0x100,-0x2c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
lea    0xe74(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11b0 <write_index+0x60>
lea    0xe6e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11c6 <write_index+0x76>
lea    0xe8a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x40(%rbp),%r9
imul   $0x3e8,-0x28(%rbp),%eax
mov    %eax,-0x154(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x3e8,%esi
cltd
idiv   %esi
mov    %eax,%edx
mov    -0x154(%rbp),%eax
add    %edx,%eax
movslq %eax,%r10
mov    -0x20(%rbp),%rax
lea    -0x150(%rbp),%rdi
mov    $0x100,%esi
lea    0xe13(%rip),%rdx
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1050 <snprintf@plt>
mov    %eax,-0x44(%rbp)
mov    0x2dba(%rip),%rax
movq   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x44(%rbp),%edx
lea    -0x150(%rbp),%rsi
call   1040 <write@plt>
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    1269 <write_index+0x119>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4c(%rbp),%eax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_index>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
mov    %edi,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    %rsp,0x8(%rsp)
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1199 <write_index+0x49>
lea    0xe81(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xea9(%rip),%r9
cmovne %rax,%r9
movslq (%rsp),%rax
imul   $0x3e8,%rax,%rax
movslq 0x4(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%rbx
add    %rax,%rbx
lea    0xe49(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %r13,%rcx
mov    %r12,%r8
push   %r15
push   %rbx
call   1050 <snprintf@plt>
add    $0x10,%rsp
mov    %eax,%ebx
mov    0x2de0(%rip),%rax
movq   $0x0,(%rax)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%ebp
cmp    %ebx,%eax
jne    1238 <write_index+0xe8>
xor    %eax,%eax
cmp    %ebx,%ebp
sete   %al
mov    0x8(%rsp),%rsp
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
jmp    121a <write_index+0xca>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [21 x i8] c"HTTP_X_FORWARDED_FOR\00"
@.str.1 = [12 x i8] c"REMOTE_ADDR\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [44 x i8] c"%lld %lld {\22ip\22:\22%s\22,\22t\22:%lld,\22sha1\22:\22%s\22}\0A\00"
@errno = global i64 0
@.str.4 = [22 x i8] c"Failed to write index\00"
define i32 @write_index(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i8* %3, i8** %8
%16 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
store i32 256, i32* %10
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %11
%18 = alloca i8, i64 256
%19 = call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %12
%20 = load i8*, i8** %12
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* %23, i8** %12
br label %24
24: ; preds = %22, %4
%25 = load i8*, i8** %12
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %24
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8** %12
br label %28
28: ; preds = %27, %24
%29 = load i64, i64* %6
%30 = load i64, i64* %7
%31 = load i8*, i8** %12
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%33 = load i32, i32* %32
%34 = mul nsw i32 %33, 1000
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%36 = load i32, i32* %35
%37 = sdiv i32 %36, 1000
%38 = add nsw i32 %34, %37
%39 = sext i32 %38 to i64
%40 = load i8*, i8** %8
%41 = call i32 @snprintf(i8* %18, i32 256, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64 %29, i64 %30, i8* %31, i64 %39, i8* %40)
store i32 %41, i32* %13
store i64 0, i64* @errno
%42 = load i32, i32* %5
%43 = load i32, i32* %13
%44 = call i32 @write(i32 %42, i8* %18, i32 %43)
store i32 %44, i32* %14
%45 = load i32, i32* %14
%46 = load i32, i32* %13
%47 = icmp eq i32 %45, %46
%48 = zext i1 %47 to i32
store i32 %48, i32* %15
%49 = load i32, i32* %15
%50 = icmp ne i32 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %28
%52 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %28
%54 = load i32, i32* %15
%55 = load i8*, i8** %11
ret i32 %54
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [21 x i8] c"HTTP_X_FORWARDED_FOR\00"
@.str.1 = [12 x i8] c"REMOTE_ADDR\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [44 x i8] c"%lld %lld {\22ip\22:\22%s\22,\22t\22:%lld,\22sha1\22:\22%s\22}\0A\00"
@errno = global i64 0
@.str.4 = [22 x i8] c"Failed to write index\00"
define i32 @write_index(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %5 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%8 = tail call i8* @llvm.stacksave()
%9 = alloca [256 x i8]
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %4
%16 = phi i8* [ %14, %13 ], [ %11, %4 ]
%17 = icmp eq i8* %16, null
%18 = select i1 %17, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %16
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%20 = load i32, i32* %19
%21 = mul nsw i32 %20, 1000
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%23 = load i32, i32* %22
%24 = sdiv i32 %23, 1000
%25 = add nsw i32 %24, %21
%26 = sext i32 %25 to i64
%27 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64 %1, i64 %2, i8* %18, i64 %26, i8* %3)
store i64 0, i64* @errno
%28 = call i32 @write(i32 %0, i8* %10, i32 %27)
%29 = icmp eq i32 %28, %27
br i1 %29, label %32, label %30
30: ; preds = %15
%31 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %15
%33 = zext i1 %29 to i32
ret i32 %33
}