#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 creat(char *, int);
int getpid();
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
void put_pid(char *fname) {
  char *defname = "dhcparpd";
  char buf[512];
  int fd;
  if (fname == NULL) {
    fname = defname;
    snprintf(buf, 512, "/var/run/%s.pid", fname);
  } else {
    snprintf(buf, 512, "%s", fname);
  }
  fd = creat(buf, 0660);
  if (fd < 0)
    return;
  sprintf(buf, "%i\n", getpid());
  if (write(fd, buf, strlen(buf)) != (signed)strlen(buf)) {
    close(fd);
    return;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<put_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11be <put_pid+0x4e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x200,%esi
lea    0xe55(%rip),%rdx
call   1060 <snprintf@plt>
jmp    11da <put_pid+0x6a>
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x200,%esi
lea    0xe44(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x210(%rbp),%rdi
mov    $0x1b0,%esi
call   1080 <creat@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jge    1203 <put_pid+0x93>
jmp    12a6 <put_pid+0x136>
lea    -0x210(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x230(%rbp),%rdi
mov    %eax,%edx
lea    0xdf4(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x214(%rbp),%eax
mov    %eax,-0x224(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x224(%rbp),%edi
mov    -0x220(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %eax,-0x218(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x218(%rbp),%eax
cmp    %ecx,%eax
je     129b <put_pid+0x12b>
mov    -0x214(%rbp),%edi
call   1070 <close@plt>
jmp    12a6 <put_pid+0x136>
mov    -0x214(%rbp),%edi
call   1070 <close@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_pid>:
push   %r14
push   %rbx
sub    $0x208,%rsp
test   %rdi,%rdi
je     118b <put_pid+0x1b>
mov    %rdi,%rcx
lea    0xe90(%rip),%rdx
jmp    1199 <put_pid+0x29>
lea    0xe77(%rip),%rdx
lea    0xe67(%rip),%rcx
mov    %rsp,%rdi
mov    $0x200,%esi
call   1060 <snprintf@plt>
mov    %rsp,%rdi
mov    $0x1b0,%esi
call   1080 <creat@plt>
test   %eax,%eax
js     11f0 <put_pid+0x80>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe55(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"dhcparpd\00"
@.str.1 = [16 x i8] c"/var/run/%s.pid\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [4 x i8] c"%i\0A\00"
define void @put_pid(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [512 x i8]
%5 = alloca i32
store i8* %0, i8** %2
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %3
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i8*, i8** %3
store i8* %9, i8** %2
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%11 = load i8*, i8** %2
%12 = call i32 @snprintf(i8* %10, i32 512, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %17
13: ; preds = %1
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%15 = load i8*, i8** %2
%16 = call i32 @snprintf(i8* %14, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %13, %8
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%19 = call i32 @creat(i8* %18, i32 432)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
br label %42
23: ; preds = %17
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%25 = call i32 (...) @getpid()
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %5
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%30 = call i64 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i64 %30)
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%33 = call i64 @strlen(i8* %32)
%34 = trunc i64 %33 to i32
%35 = icmp ne i32 %31, %34
br i1 %35, label %36, label %39
36: ; preds = %23
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
br label %42
39: ; preds = %23
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39, %36, %22
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"dhcparpd\00"
@.str.1 = [16 x i8] c"/var/run/%s.pid\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [4 x i8] c"%i\0A\00"
define void @put_pid(i8* %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = call i32 @snprintf(i8* %3, i32 512, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %9
7: ; preds = %1
%8 = call i32 @snprintf(i8* %3, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %5
%10 = call i32 @creat(i8* %3, i32 432)
%11 = icmp slt i32 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %9
%13 = call i32 (...) @getpid()
%14 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = call i64 @strlen(i8* %3)
%16 = call i32 @write(i32 %10, i8* %3, i64 %15)
%17 = call i32 @close(i32 %10)
br label %18
18: ; preds = %12, %9
ret void
}