#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*/ close(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int sprintf(char *, char *, char const *, char const *);
int wrap_line(int, char *, int);
int add_header(int fd, const char *name, const char *value) {
  int rc;
  char buff[1024];
  if (name)
    rc = sprintf(buff, "%s: %s\n", name, value);
  else
    memcpy(buff, "\n", rc = 1);
  rc = wrap_line(fd, buff, rc);
  if (rc < 0) {
    close(fd);
    return -1;
  }
  return rc;
}


x86 assembly (clang, -O0):
<add_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1184 <add_header+0x44>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe89(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    11a3 <add_header+0x63>
lea    -0x420(%rbp),%rdi
movl   $0x1,-0x1c(%rbp)
lea    0xe6d(%rip),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <wrap_line@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d6 <add_header+0x96>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <add_header+0x9c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_header>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
test   %rsi,%rsi
je     1169 <add_header+0x29>
mov    %rdx,%rcx
mov    %rsi,%rdx
lea    0xea3(%rip),%rsi
mov    %rsp,%rdi
call   1060 <sprintf@plt>
mov    %eax,%ebp
jmp    1182 <add_header+0x42>
lea    0xe96(%rip),%rsi
mov    %rsp,%rdi
mov    $0x1,%ebp
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <wrap_line@plt>
test   %eax,%eax
jns    119e <add_header+0x5e>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @add_header(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [1024 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = load i8*, i8** %7
%16 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15)
store i32 %16, i32* %8
br label %20
17: ; preds = %3
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
store i32 1, i32* %8
%19 = call i32 @memcpy(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %20
20: ; preds = %17, %12
%21 = load i32, i32* %5
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%23 = load i32, i32* %8
%24 = call i32 @wrap_line(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %4
br label %32
30: ; preds = %20
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %32
32: ; preds = %30, %27
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @add_header(i32 %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = icmp eq i8* %1, null
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
br label %11
9: ; preds = %3
%10 = call i32 @memcpy(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %11
11: ; preds = %9, %7
%12 = phi i32 [ %8, %7 ], [ 1, %9 ]
%13 = call i32 @wrap_line(i32 %0, i8* %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @close(i32 %0)
br label %17
17: ; preds = %11, %15
%18 = phi i32 [ -1, %15 ], [ %13, %11 ]
ret i32 %18
}