#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 /*<<< orphan*/ printf(char *, char *, char *, char *, int);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
char *who(int /*<<< orphan*/);
int /*<<< orphan*/ write_safe(int, char *, int /*<<< orphan*/);
void write_to_fd(int fd, char *name, int i, int newline) {
  int n;
  char buf[16];
  n = snprintf(buf, sizeof buf, "hello %d", i);
  printf("%s writing %s to %s via fd %d\n", who(0), buf, name, fd);
  if (newline) {
    buf[n++] = '\n';
    buf[n] = '\0';
  }
  write_safe(fd, buf, strlen(buf));
}


x86 assembly (clang, -O0):
<write_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xe88(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1060 <who@plt>
mov    %rax,%rsi
lea    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x4(%rbp),%r8d
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     11c7 <write_to_fd+0x77>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cltq
movb   $0xa,-0x30(%rbp,%rax,1)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
mov    -0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1070 <write_safe@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_fd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %edx,%ecx
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0xe93(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x10,%esi
call   1050 <snprintf@plt>
mov    %eax,%r15d
xor    %edi,%edi
call   1060 <who@plt>
lea    0xe7b(%rip),%rdi
mov    %rax,%rsi
mov    %r12,%rdx
mov    %rbp,%rcx
mov    %ebx,%r8d
call   1040 <printf@plt>
test   %r14d,%r14d
je     11ad <write_to_fd+0x5d>
movslq %r15d,%rax
movw   $0xa,(%rsp,%rax,1)
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <write_safe@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"hello %d\00"
@.str.1 = [31 x i8] c"%s writing %s to %s via fd %d\0A\00"
define void @write_to_fd(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [16 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%12 = load i32, i32* %7
%13 = call i32 @snprintf(i8* %11, i32 16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %9
%14 = call i8* @who(i32 0)
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%16 = load i8*, i8** %6
%17 = load i32, i32* %5
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15, i8* %16, i32 %17)
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %4
%22 = load i32, i32* %9
%23 = add nsw i32 %22, 1
store i32 %23, i32* %9
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 %24
store i8 10, i8* %25
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %21, %4
%30 = load i32, i32* %5
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write_safe(i32 %30, i8* %31, i32 %33)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"hello %d\00"
@.str.1 = [31 x i8] c"%s writing %s to %s via fd %d\0A\00"
define void @write_to_fd(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
%8 = call i8* @who(i32 0)
%9 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %6, i8* %1, i32 %0)
%10 = icmp eq i32 %3, 0
br i1 %10, label %17, label %11
11: ; preds = %4
%12 = add nsw i32 %7, 1
%13 = sext i32 %7 to i64
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %13
store i8 10, i8* %14
%15 = sext i32 %12 to i64
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %11, %4
%18 = call i32 @strlen(i8* %6)
%19 = call i32 @write_safe(i32 %0, i8* %6, i32 %18)
ret void
}