#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*/ gds_swap2bytes(unsigned char *);
scalar_t__ malloc(short);
short strlen(char *);
int /*<<< orphan*/ write(int, ...);
void gds_write_sname(int fd, char *s) {
  static short int count, len, i, token;
  static int pad;
  static char *ps;
  len = strlen(s);
  pad = (len % 2) != 0;
  if (pad)
    len++;
  ps = (char *)malloc(len);
  if (pad) {
    for (i = 0; i < len - 1; i++)
      ps[i] = s[i];
    ps[len - 1] = 0;
  } else {
    for (i = 0; i < len; i++)
      ps[i] = s[i];
  }
  count = 4 + len;
  gds_swap2bytes((unsigned char *)&count);
  write(fd, &count, 2);
  token = 0x1206;
  gds_swap2bytes((unsigned char *)&token);
  write(fd, &token, 2);
  write(fd, ps, len);
}


x86 assembly (clang, -O0):
<gds_write_sname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %ax,0x2eeb(%rip)
movswl 0x2ee4(%rip),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,0x2ed1(%rip)
cmpl   $0x0,0x2eca(%rip)
je     119e <gds_write_sname+0x5e>
mov    0x2eb7(%rip),%ax
add    $0x1,%ax
mov    %ax,0x2eac(%rip)
movswl 0x2ea5(%rip),%edi
call   1060 <malloc@plt>
mov    %rax,0x2ea7(%rip)
cmpl   $0x0,0x2e98(%rip)
je     1235 <gds_write_sname+0xf5>
movw   $0x0,0x2e85(%rip)
movswl 0x2e7e(%rip),%eax
movswl 0x2e75(%rip),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1218 <gds_write_sname+0xd8>
mov    -0x10(%rbp),%rax
movswq 0x2e60(%rip),%rcx
mov    (%rax,%rcx,1),%dl
mov    0x2e62(%rip),%rax
movswq 0x2e4e(%rip),%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2e44(%rip),%ax
add    $0x1,%ax
mov    %ax,0x2e39(%rip)
jmp    11c7 <gds_write_sname+0x87>
mov    0x2e39(%rip),%rax
movswl 0x2e24(%rip),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1291 <gds_write_sname+0x151>
movw   $0x0,0x2e0e(%rip)
movswl 0x2e07(%rip),%eax
movswl 0x2dfe(%rip),%ecx
cmp    %ecx,%eax
jge    128c <gds_write_sname+0x14c>
mov    -0x10(%rbp),%rax
movswq 0x2dec(%rip),%rcx
mov    (%rax,%rcx,1),%dl
mov    0x2dee(%rip),%rax
movswq 0x2dda(%rip),%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2dd0(%rip),%ax
add    $0x1,%ax
mov    %ax,0x2dc5(%rip)
jmp    123e <gds_write_sname+0xfe>
jmp    1291 <gds_write_sname+0x151>
movswl 0x2db2(%rip),%eax
add    $0x4,%eax
mov    %ax,0x2da6(%rip)
lea    0x2d9f(%rip),%rdi
call   1050 <gds_swap2bytes@plt>
mov    -0x4(%rbp),%edi
lea    0x2d90(%rip),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <write@plt>
movw   $0x1206,0x2d81(%rip)
06 12
lea    0x2d7a(%rip),%rdi
call   1050 <gds_swap2bytes@plt>
mov    -0x4(%rbp),%edi
lea    0x2d6b(%rip),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
mov    0x2d5f(%rip),%rsi
movswl 0x2d4a(%rip),%edx
mov    $0x0,%al
call   1030 <write@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gds_write_sname>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %ax,0x2ef1(%rip)
mov    %eax,%ecx
and    $0x1,%ecx
mov    %ecx,0x2eec(%rip)
je     1170 <gds_write_sname+0x30>
add    $0x1,%eax
mov    %ax,0x2eda(%rip)
movswl %ax,%edi
call   1060 <malloc@plt>
mov    %rax,0x2ed9(%rip)
cmpl   $0x0,0x2eca(%rip)
movzwl 0x2ebd(%rip),%ecx
je     11c2 <gds_write_sname+0x82>
cmp    $0x2,%cx
jl     11eb <gds_write_sname+0xab>
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
movswq %cx,%rdx
movzbl 0x0(%rbp,%rdx,1),%ebx
mov    %bl,(%rax,%rdx,1)
add    $0x1,%ecx
movswl %cx,%esi
movswl 0x2e91(%rip),%edx
add    $0xffffffff,%edx
cmp    %esi,%edx
jg     11a0 <gds_write_sname+0x60>
jmp    11f1 <gds_write_sname+0xb1>
test   %cx,%cx
jle    11ff <gds_write_sname+0xbf>
xor    %edx,%edx
nopl   0x0(%rax)
movzbl 0x0(%rbp,%rdx,1),%ecx
mov    %cl,(%rax,%rdx,1)
add    $0x1,%rdx
movswq 0x2e66(%rip),%rcx
cmp    %rcx,%rdx
jl     11d0 <gds_write_sname+0x90>
jmp    11ff <gds_write_sname+0xbf>
movswl %cx,%edx
add    $0xffffffff,%edx
movslq %edx,%rcx
movb   $0x0,(%rax,%rcx,1)
movzwl 0x2e4b(%rip),%ecx
add    $0x4,%ecx
mov    %cx,0x2e3f(%rip)
lea    0x2e38(%rip),%rbx
mov    %rbx,%rdi
call   1050 <gds_swap2bytes@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <write@plt>
movw   $0x1206,0x2e19(%rip)
06 12
lea    0x2e12(%rip),%rbx
mov    %rbx,%rdi
call   1050 <gds_swap2bytes@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    0x2dfd(%rip),%rsi
movswl 0x2de8(%rip),%edx
mov    %r14d,%edi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <write@plt>

LLVM IR (-O0):
@gds_write_sname.count = internal global i16 0
@gds_write_sname.len = internal global i16 0
@gds_write_sname.i = internal global i16 0
@gds_write_sname.token = internal global i16 0
@gds_write_sname.pad = internal global i32 0
@gds_write_sname.ps = internal global i8* null
define void @gds_write_sname(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i8*, i8** %4
%6 = call signext i16 @strlen(i8* %5)
store i16 %6, i16* @gds_write_sname.len
%7 = load i16, i16* @gds_write_sname.len
%8 = sext i16 %7 to i32
%9 = srem i32 %8, 2
%10 = icmp ne i32 %9, 0
%11 = zext i1 %10 to i32
store i32 %11, i32* @gds_write_sname.pad
%12 = load i32, i32* @gds_write_sname.pad
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i16, i16* @gds_write_sname.len
%16 = add i16 %15, 1
store i16 %16, i16* @gds_write_sname.len
br label %17
17: ; preds = %14, %2
%18 = load i16, i16* @gds_write_sname.len
%19 = call i64 @malloc(i16 signext %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** @gds_write_sname.ps
%21 = load i32, i32* @gds_write_sname.pad
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %51
23: ; preds = %17
store i16 0, i16* @gds_write_sname.i
br label %24
24: ; preds = %41, %23
%25 = load i16, i16* @gds_write_sname.i
%26 = sext i16 %25 to i32
%27 = load i16, i16* @gds_write_sname.len
%28 = sext i16 %27 to i32
%29 = sub nsw i32 %28, 1
%30 = icmp slt i32 %26, %29
br i1 %30, label %31, label %44
31: ; preds = %24
%32 = load i8*, i8** %4
%33 = load i16, i16* @gds_write_sname.i
%34 = sext i16 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** @gds_write_sname.ps
%38 = load i16, i16* @gds_write_sname.i
%39 = sext i16 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
br label %41
41: ; preds = %31
%42 = load i16, i16* @gds_write_sname.i
%43 = add i16 %42, 1
store i16 %43, i16* @gds_write_sname.i
br label %24
44: ; preds = %24
%45 = load i8*, i8** @gds_write_sname.ps
%46 = load i16, i16* @gds_write_sname.len
%47 = sext i16 %46 to i32
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %45, i64 %49
store i8 0, i8* %50
br label %72
51: ; preds = %17
store i16 0, i16* @gds_write_sname.i
br label %52
52: ; preds = %68, %51
%53 = load i16, i16* @gds_write_sname.i
%54 = sext i16 %53 to i32
%55 = load i16, i16* @gds_write_sname.len
%56 = sext i16 %55 to i32
%57 = icmp slt i32 %54, %56
br i1 %57, label %58, label %71
58: ; preds = %52
%59 = load i8*, i8** %4
%60 = load i16, i16* @gds_write_sname.i
%61 = sext i16 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = load i8, i8* %62
%64 = load i8*, i8** @gds_write_sname.ps
%65 = load i16, i16* @gds_write_sname.i
%66 = sext i16 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 %63, i8* %67
br label %68
68: ; preds = %58
%69 = load i16, i16* @gds_write_sname.i
%70 = add i16 %69, 1
store i16 %70, i16* @gds_write_sname.i
br label %52
71: ; preds = %52
br label %72
72: ; preds = %71, %44
%73 = load i16, i16* @gds_write_sname.len
%74 = sext i16 %73 to i32
%75 = add nsw i32 4, %74
%76 = trunc i32 %75 to i16
store i16 %76, i16* @gds_write_sname.count
%77 = call i32 @gds_swap2bytes(i8* bitcast (i16* @gds_write_sname.count to i8*))
%78 = load i32, i32* %3
%79 = call i32 (i32, ...) @write(i32 %78, i16* @gds_write_sname.count, i32 2)
store i16 4614, i16* @gds_write_sname.token
%80 = call i32 @gds_swap2bytes(i8* bitcast (i16* @gds_write_sname.token to i8*))
%81 = load i32, i32* %3
%82 = call i32 (i32, ...) @write(i32 %81, i16* @gds_write_sname.token, i32 2)
%83 = load i32, i32* %3
%84 = load i8*, i8** @gds_write_sname.ps
%85 = load i16, i16* @gds_write_sname.len
%86 = sext i16 %85 to i32
%87 = call i32 (i32, ...) @write(i32 %83, i8* %84, i32 %86)
ret void
}

LLVM IR (-O2):
@gds_write_sname.count = internal global i16 0
@gds_write_sname.len = internal unnamed_addr global i16 0
@gds_write_sname.token = internal global i16 0
@gds_write_sname.pad = internal unnamed_addr global i32 0
@gds_write_sname.ps = internal unnamed_addr global i8* null
define void @gds_write_sname(i32 %0, i8* %1) {
%3 = tail call signext i16 @strlen(i8* %1)
store i16 %3, i16* @gds_write_sname.len
%4 = and i16 %3, 1
%5 = icmp eq i16 %4, 0
%6 = zext i16 %4 to i32
store i32 %6, i32* @gds_write_sname.pad
br i1 %5, label %9, label %7
7: ; preds = %2
%8 = add i16 %3, 1
store i16 %8, i16* @gds_write_sname.len
br label %9
9: ; preds = %7, %2
%10 = phi i16 [ %8, %7 ], [ %3, %2 ]
%11 = tail call i64 @malloc(i16 signext %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** @gds_write_sname.ps
%13 = load i32, i32* @gds_write_sname.pad
%14 = icmp eq i32 %13, 0
%15 = load i16, i16* @gds_write_sname.len
br i1 %14, label %20, label %16
16: ; preds = %9
%17 = sext i16 %15 to i32
%18 = add nsw i32 %17, -1
%19 = icmp sgt i16 %15, 1
br i1 %19, label %22, label %34
20: ; preds = %9
%21 = icmp sgt i16 %15, 0
br i1 %21, label %39, label %48
22: ; preds = %16, %22
%23 = phi i16 [ %28, %22 ], [ 0, %16 ]
%24 = sext i16 %23 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = load i8, i8* %25
%27 = getelementptr inbounds i8, i8* %12, i64 %24
store i8 %26, i8* %27
%28 = add i16 %23, 1
%29 = sext i16 %28 to i32
%30 = load i16, i16* @gds_write_sname.len
%31 = sext i16 %30 to i32
%32 = add nsw i32 %31, -1
%33 = icmp sgt i32 %32, %29
br i1 %33, label %22, label %34
34: ; preds = %22, %16
%35 = phi i32 [ %18, %16 ], [ %32, %22 ]
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %12, i64 %36
store i8 0, i8* %37
%38 = load i16, i16* @gds_write_sname.len
br label %48
39: ; preds = %20, %39
%40 = phi i64 [ %44, %39 ], [ 0, %20 ]
%41 = getelementptr inbounds i8, i8* %1, i64 %40
%42 = load i8, i8* %41
%43 = getelementptr inbounds i8, i8* %12, i64 %40
store i8 %42, i8* %43
%44 = add nuw nsw i64 %40, 1
%45 = load i16, i16* @gds_write_sname.len
%46 = sext i16 %45 to i64
%47 = icmp slt i64 %44, %46
br i1 %47, label %39, label %48
48: ; preds = %39, %20, %34
%49 = phi i16 [ %15, %20 ], [ %38, %34 ], [ %45, %39 ]
%50 = add i16 %49, 4
store i16 %50, i16* @gds_write_sname.count
%51 = tail call i32 @gds_swap2bytes(i8* bitcast (i16* @gds_write_sname.count to i8*))
%52 = tail call i32 (i32, ...) @write(i32 %0, i16* @gds_write_sname.count, i32 2)
store i16 4614, i16* @gds_write_sname.token
%53 = tail call i32 @gds_swap2bytes(i8* bitcast (i16* @gds_write_sname.token to i8*))
%54 = tail call i32 (i32, ...) @write(i32 %0, i16* @gds_write_sname.token, i32 2)
%55 = load i8*, i8** @gds_write_sname.ps
%56 = load i16, i16* @gds_write_sname.len
%57 = sext i16 %56 to i32
%58 = tail call i32 (i32, ...) @write(i32 %0, i8* %55, i32 %57)
ret void
}